Skip to content

Commit 757f1a1

Browse files
committed
Add commas for thousand seperator and fix text box rendering outside area
1 parent 2587cf0 commit 757f1a1

24 files changed

+456
-417
lines changed

groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLFileTextCounterApplication.groovy

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.example.javafxtextcountergroovy
22

33
import javafx.application.Application
44
import javafx.fxml.FXMLLoader
5+
import javafx.scene.Parent
56
import javafx.scene.Scene
67
import javafx.stage.Stage
78

@@ -11,11 +12,11 @@ class GroovyFXMLFileTextCounterApplication extends Application {
1112
@Override
1213
void start(Stage stage) throws IOException {
1314
FXMLLoader fxmlLoader = new FXMLLoader(
14-
com.example.javafxtextcountergroovy.GroovyFXMLFileTextCounterApplication.class
15-
.getResource("fxml-file-text-counter-groovy.fxml"))// load FXML file resource
15+
GroovyFXMLFileTextCounterApplication.class.getResource("fxml-file-text-counter-groovy.fxml"))// load FXML file
16+
// resource
1617
Scene scene = new Scene(fxmlLoader.load() as Parent, 480, 320)// set application size to 480x320
17-
stage.setTitle("Text Counter from a File")// set title of stage
18-
stage.setScene(scene)// set stage to scene
18+
stage.setTitle("Text Counter from a File")// set the title of stage
19+
stage.setScene(scene)// set the stage to a scene
1920
stage.show()// show the stage
2021
}
2122

groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLFileTextCounterController.groovy

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class GroovyFXMLFileTextCounterController {
2828
throw new RuntimeException(e)// throw runtime exception
2929
}
3030
}
31-
int chars = textContent.length()// get length of opened text file in characters
32-
int words = textContent.trim().split("\\s+").length// get length of opened text file in words
33-
int lines = textContent.trim().split("\\r?\\n").length// get length of opened text file in lines
34-
textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines)// update text
35-
// counter output
31+
int chars = textContent.length()// get the length of opened text file in characters
32+
int words = textContent.trim().split("\\s+").length// get the length of opened text file in words
33+
int lines = textContent.trim().split("\\r?\\n").length// get the length of opened text file in lines
34+
textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: " + String.format("%,d", words)
35+
+ "\nLines: " + String.format("%,d", lines))// update text counter output
3636
}
3737
}

groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLTextCounterApplication.groovy

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,26 @@ package com.example.javafxtextcountergroovy
22

33
import javafx.application.Application
44
import javafx.fxml.FXMLLoader
5+
import javafx.scene.Parent
56
import javafx.scene.Scene
67
import javafx.stage.Stage
78

89
import java.io.IOException
910

10-
class GroovyFXMLTextCounterApplication extends Application {
11+
class GroovyFXMLTextCounterApplication extends Application {
1112
@Override
1213
void start(Stage stage) throws IOException {
13-
FXMLLoader fxmlLoader = new FXMLLoader(
14-
com.example.javafxtextcountergroovy.GroovyFXMLTextCounterApplication.class
15-
.getResource("fxml-text-counter-groovy.fxml"))// load FXML file resource
14+
FXMLLoader fxmlLoader = new FXMLLoader(GroovyFXMLTextCounterApplication.class.getResource("fxml-text-counter-groovy.fxml"))// load
15+
// FXML
16+
// file
17+
// resource
1618
Scene scene = new Scene(fxmlLoader.load() as Parent, 480, 320)// set application size to 480x320
17-
stage.setTitle("Text Counter")// set title of stage
18-
stage.setScene(scene)// set stage to scene
19+
stage.setTitle("Text Counter")// set the title of stage
20+
stage.setScene(scene)// set the stage to a scene
1921
stage.show()// show the stage
2022
}
2123

2224
static void main(String[] args) {
23-
launch(GroovyFXMLTextCounterApplication.class, args)// launch the application by running the program
25+
launch()// launch the application by running the program
2426
}
2527
}

groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLTextCounterController.groovy

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import javafx.scene.control.Label
66
import javafx.scene.control.TextArea
77

88
class GroovyFXMLTextCounterController {
9-
TextArea textContent// text box area
10-
Button updateText// button to update entered text from text box
11-
Label textCountResult// text counter output result
9+
public TextArea textContent// text box area
10+
public Button updateText// button to update entered text from text box
11+
public Label textCountResult// text counter output result
1212

13-
void calculateTextCount(ActionEvent actionEvent) {// update text count result when button is clicked
14-
int chars = textContent.getLength()// get length of text area in characters
15-
int words = textContent.getText().trim().split("\\s+").length// get length of text area in words
16-
int lines = textContent.getText().split("\\r?\\n").length// get length of text area in lines
17-
textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines)// update text
18-
// counter output
13+
void calculateTextCount(ActionEvent actionEvent) {// update text counter result when button is clicked
14+
int chars = textContent.getLength()// get the length of text area in characters
15+
int words = textContent.getText().trim().split("\\s+").length// get the length of text area in words
16+
int lines = textContent.getText().split("\\r?\\n").length// get the length of text area in lines
17+
textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: " + String.format("%,d", words)
18+
+ "\nLines: " + String.format("%,d", lines))// update text counter output
1919
}
2020
}

groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFileTextCounterJavaFX.groovy

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.example.javafxtextcountergroovy
22

3-
import java.io.*
4-
53
import javafx.application.Application
64
import javafx.scene.Scene
75
import javafx.scene.control.Button
@@ -13,17 +11,15 @@ import javafx.stage.Stage
1311
class GroovyFileTextCounterJavaFX extends Application {
1412

1513
static void main(String[] args) {
16-
launch(GroovyFileTextCounterJavaFX.class, args)// launch the application by running the program
14+
launch(args)// launch the application by running the program
1715
}
1816

1917
void start(Stage primaryStage) throws FileNotFoundException {
2018
FlowPane root = new FlowPane()
2119
Scene scene = new Scene(root, 480, 320)// set the application size to 480x320
2220
Button selectFile = new Button("Select file")// select the file from the dialog box
23-
2421
primaryStage.setScene(scene)// set the scene
2522
primaryStage.setTitle("Text Counter Application")// set the title of stage
26-
2723
Label textCountResult = new Label("Characters: 0\nWords: 0\nLines: 0")// set the default text counter output
2824
selectFile.setOnAction((event) -> {
2925
String textContent = ""// initialize empty text content string
@@ -44,12 +40,12 @@ class GroovyFileTextCounterJavaFX extends Application {
4440
throw new RuntimeException(e)// throw runtime exception
4541
}
4642
}
47-
int chars = textContent.length()// get length of opened text file in characters
48-
int words = textContent.trim().split("\\s+").length// get length of opened text file in words
49-
int lines = textContent.trim().split("\\r?\\n").length// get length of opened text file in lines
50-
textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines)// update text
51-
// counter
52-
// output
43+
int chars = textContent.length()// get the length of opened text file in characters
44+
int words = textContent.trim().split("\\s+").length// get the length of opened text file in words
45+
int lines = textContent.trim().split("\\r?\\n").length// get the length of opened text file in lines
46+
textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: "
47+
+ String.format("%,d", words) + "\nLines: " + String.format("%,d", lines))// update text counter
48+
// output
5349
})
5450
root.getChildren().addAll(selectFile, textCountResult)// add all components to the stage
5551
primaryStage.show()// show the stage

groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyTextCounterJavaFX.groovy

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import javafx.stage.Stage
1111
class GroovyTextCounterJavaFX extends Application {
1212

1313
static void main(String[] args) {
14-
launch(GroovyTextCounterJavaFX.class, args)// launch the application by running the program
14+
launch(args)// launch the application by running the program
1515
}
1616

1717
void start(Stage primaryStage) {
@@ -23,12 +23,12 @@ class GroovyTextCounterJavaFX extends Application {
2323
Button updateText = new Button("Calculate text count")
2424
Label textCountResult = new Label("Characters: 0\nWords: 0\nLines: 0")// set to default text counter output
2525
updateText.setOnAction((event) -> {// calculate text count when button is clicked
26-
int chars = textContent.getLength()// get length of text area in characters
27-
int words = textContent.getText().trim().split("\\s+").length// get length of text area in words
28-
int lines = textContent.getText().split("\\r?\\n").length// get length of text area in lines
29-
textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines)// update text
30-
// counter
31-
// output
26+
int chars = textContent.getLength()// get the length of text area in characters
27+
int words = textContent.getText().trim().split("\\s+").length// get the length of text area in words
28+
int lines = textContent.getText().split("\\r?\\n").length// get the length of text area in lines
29+
textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: "
30+
+ String.format("%,d", words) + "\nLines: " + String.format("%,d", lines))// update text counter
31+
// output
3232
})
3333
root.getChildren().addAll(textContent, updateText, textCountResult)// add all components to the stage
3434
primaryStage.show()// show the stage

groovy/javaswing-textcounter-groovy/src/fileTextCounterGroovy.groovy

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import javax.swing.*
2+
import javax.swing.filechooser.FileNameExtensionFilter
23
import java.awt.*
34
import java.awt.event.ActionEvent
45
import java.awt.event.ActionListener
@@ -7,7 +8,7 @@ class fileTextCounterGroovy {
78
JButton selectFile = new JButton("Select file")
89
JLabel textCountResult
910

10-
fileTextCounterGroovy() {
11+
fileTextCounterGroovy() {
1112
Frame textCount = new JFrame("File Text Counter Application")// initialize frame and set title of frame
1213
textCount.setLayout(new FlowLayout())
1314
textCount.setSize(480, 320)// set application size to 480x320
@@ -17,29 +18,32 @@ class fileTextCounterGroovy {
1718
textCount.setVisible(true)// make the frame visible
1819
selectFile.addActionListener(new ActionListener() {
1920
@Override
20-
void actionPerformed(ActionEvent e) {// when button is clicked
21-
JFileChooser fileChooser = new JFileChooser()//create a new file chooser
22-
int fileResult = fileChooser.showOpenDialog(textCount)//show file dialog
21+
void actionPerformed(ActionEvent e) {// when button is clicked
22+
JFileChooser fileChooser = new JFileChooser()// create a new file chooser
23+
fileChooser.setFileFilter(new FileNameExtensionFilter("Text files", "txt"))
24+
int fileResult = fileChooser.showOpenDialog(textCount)// show file dialog
2325
final String textContent
2426
try {
2527
textContent = getTextContent(fileResult, fileChooser)
2628
} catch (IOException ex) {
2729
throw new RuntimeException(ex)
2830
}
29-
int chars = textContent.length()// get length of text area in characters
30-
int words = textContent.trim().split("\\s+").length// get length of text area in words
31-
int lines = textContent.split("\\r?\\n").length// get length of text area in lines
32-
textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines)// update text count output
31+
int chars = textContent.length()// get the length of text area in characters
32+
int words = textContent.trim().split("\\s+").length// get the length of text area in words
33+
int lines = textContent.split("\\r?\\n").length// get the length of text area in lines
34+
textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: "
35+
+ String.format("%,d", words) + "\nLines: " + String.format("%,d", lines))// update text
36+
// counter output
3337
}
3438

3539
private static String getTextContent(int fileResult, JFileChooser fileChooser) throws IOException {
36-
String textContent = ""//initialize empty text content string
40+
String textContent = ""// initialize empty text content string
3741
if (fileResult == JFileChooser.APPROVE_OPTION) {
38-
String filePath = fileChooser.getSelectedFile().getPath()//get the file path
42+
String filePath = fileChooser.getSelectedFile().getPath()// get the file path
3943
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
4044
String line
4145
while ((line = reader.readLine()) != null) {
42-
textContent += line + "\n"//read text from a file every line
46+
textContent += line + "\n"// read text from a file every line
4347
}
4448
}
4549
}
@@ -48,10 +52,10 @@ class fileTextCounterGroovy {
4852
})
4953
}
5054

51-
static void main(String[] args) {
55+
static void main(String[] args) {
5256
SwingUtilities.invokeLater(new Runnable() {
5357
@Override
54-
void run() {
58+
void run() {
5559
new fileTextCounterGroovy()// launch the application by running the program
5660
}
5761
})

groovy/javaswing-textcounter-groovy/src/textCounterGroovy.groovy

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import java.awt.*
33
import java.awt.event.ActionEvent
44
import java.awt.event.ActionListener
55

6-
class textCounterGroovy {
6+
class textCounterGroovy {
77
JTextArea textContent
88
JButton updateText
99
JLabel textCountResult
1010

11-
textCounterGroovy() {
11+
textCounterGroovy() {
1212
Frame textCount = new JFrame("Text Counter Application")// initialize frame and set title of frame
1313
textCount.setLayout(new FlowLayout())
1414
textCount.setSize(480, 320)// set application size to 480x320
15-
textContent = new JTextArea(10, 80)// create text area with 10 rows and 80 columns
15+
textContent = new JTextArea(10, 40)// create text area with 10 rows and 40 columns
1616
textCount.add(textContent)// add components to the frame
1717
updateText = new JButton("Calculate text count")
1818
textCount.add(updateText)
@@ -21,19 +21,21 @@ import java.awt.event.ActionListener
2121
textCount.setVisible(true)// make the frame visible
2222
updateText.addActionListener(new ActionListener() {
2323
@Override
24-
void actionPerformed(ActionEvent e) {// when button is clicked
25-
int chars = textContent.getText().length()// get length of text area in characters
26-
int words = textContent.getText().trim().split("\\s+").length// get length of text area in words
27-
int lines = textContent.getText().split("\\r?\\n").length// get length of text area in lines
28-
textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines)// update text count output
24+
void actionPerformed(ActionEvent e) {// when the button is clicked
25+
int chars = textContent.getText().length()// get the length of text area in characters
26+
int words = textContent.getText().trim().split("\\s+").length// get the length of text area in words
27+
int lines = textContent.getText().split("\\r?\\n").length// get the length of text area in lines
28+
textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: "
29+
+ String.format("%,d", words) + "\nLines: " + String.format("%,d", lines))// update text
30+
// counter output
2931
}
3032
})
3133
}
3234

33-
static void main(String[] args) {
35+
static void main(String[] args) {
3436
SwingUtilities.invokeLater(new Runnable() {
3537
@Override
36-
void run() {
38+
void run() {
3739
new textCounterGroovy()// launch the application by running the program
3840
}
3941
})

java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLFileTextCounterApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public void start(Stage stage) throws IOException {
1414
FXMLFileTextCounterApplication.class.getResource("fxml-file-text-counter.fxml"));// load FXML file
1515
// resource
1616
Scene scene = new Scene(fxmlLoader.load(), 480, 320);// set application size to 480x320
17-
stage.setTitle("Text Counter from a File");// set title of stage
18-
stage.setScene(scene);// set stage to scene
17+
stage.setTitle("Text Counter from a File");// set the title of stage
18+
stage.setScene(scene);// set the stage to a scene
1919
stage.show();// show the stage
2020
}
2121

java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLFileTextCounterController.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public void calculateTextCount(ActionEvent actionEvent) {
2929
throw new RuntimeException(e);// throw runtime exception
3030
}
3131
}
32-
int chars = textContent.length();// get length of opened text file in characters
33-
int words = textContent.trim().split("\\s+").length;// get length of opened text file in words
34-
int lines = textContent.trim().split("\\r?\\n").length;// get length of opened text file in lines
35-
textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines);// update text
36-
// counter output
32+
int chars = textContent.length();// get the length of opened text file in characters
33+
int words = textContent.trim().split("\\s+").length;// get the length of opened text file in words
34+
int lines = textContent.trim().split("\\r?\\n").length;// get the length of opened text file in lines
35+
textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: " + String.format("%,d", words)
36+
+ "\nLines: " + String.format("%,d", lines));// update text counter output
3737
}
3838
}

0 commit comments

Comments
 (0)