diff --git a/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLFileTextCounterApplication.groovy b/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLFileTextCounterApplication.groovy index 1e3c077..d29d73b 100644 --- a/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLFileTextCounterApplication.groovy +++ b/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLFileTextCounterApplication.groovy @@ -2,6 +2,7 @@ package com.example.javafxtextcountergroovy import javafx.application.Application import javafx.fxml.FXMLLoader +import javafx.scene.Parent import javafx.scene.Scene import javafx.stage.Stage @@ -11,11 +12,11 @@ class GroovyFXMLFileTextCounterApplication extends Application { @Override void start(Stage stage) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader( - com.example.javafxtextcountergroovy.GroovyFXMLFileTextCounterApplication.class - .getResource("fxml-file-text-counter-groovy.fxml"))// load FXML file resource + GroovyFXMLFileTextCounterApplication.class.getResource("fxml-file-text-counter-groovy.fxml"))// load FXML file + // resource Scene scene = new Scene(fxmlLoader.load() as Parent, 480, 320)// set application size to 480x320 - stage.setTitle("Text Counter from a File")// set title of stage - stage.setScene(scene)// set stage to scene + stage.setTitle("Text Counter from a File")// set the title of stage + stage.setScene(scene)// set the stage to a scene stage.show()// show the stage } diff --git a/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLFileTextCounterController.groovy b/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLFileTextCounterController.groovy index cc18efa..0689a3d 100644 --- a/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLFileTextCounterController.groovy +++ b/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLFileTextCounterController.groovy @@ -28,10 +28,10 @@ class GroovyFXMLFileTextCounterController { throw new RuntimeException(e)// throw runtime exception } } - int chars = textContent.length()// get length of opened text file in characters - int words = textContent.trim().split("\\s+").length// get length of opened text file in words - int lines = textContent.trim().split("\\r?\\n").length// get length of opened text file in lines - textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines)// update text - // counter output + int chars = textContent.length()// get the length of opened text file in characters + int words = textContent.trim().split("\\s+").length// get the length of opened text file in words + int lines = textContent.trim().split("\\r?\\n").length// get the length of opened text file in lines + textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: " + String.format("%,d", words) + + "\nLines: " + String.format("%,d", lines))// update text counter output } } diff --git a/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLTextCounterApplication.groovy b/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLTextCounterApplication.groovy index 9db25d5..4023d6d 100644 --- a/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLTextCounterApplication.groovy +++ b/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLTextCounterApplication.groovy @@ -2,24 +2,26 @@ package com.example.javafxtextcountergroovy import javafx.application.Application import javafx.fxml.FXMLLoader +import javafx.scene.Parent import javafx.scene.Scene import javafx.stage.Stage import java.io.IOException - class GroovyFXMLTextCounterApplication extends Application { +class GroovyFXMLTextCounterApplication extends Application { @Override void start(Stage stage) throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader( - com.example.javafxtextcountergroovy.GroovyFXMLTextCounterApplication.class - .getResource("fxml-text-counter-groovy.fxml"))// load FXML file resource + FXMLLoader fxmlLoader = new FXMLLoader(GroovyFXMLTextCounterApplication.class.getResource("fxml-text-counter-groovy.fxml"))// load + // FXML + // file + // resource Scene scene = new Scene(fxmlLoader.load() as Parent, 480, 320)// set application size to 480x320 - stage.setTitle("Text Counter")// set title of stage - stage.setScene(scene)// set stage to scene + stage.setTitle("Text Counter")// set the title of stage + stage.setScene(scene)// set the stage to a scene stage.show()// show the stage } static void main(String[] args) { - launch(GroovyFXMLTextCounterApplication.class, args)// launch the application by running the program + launch()// launch the application by running the program } } \ No newline at end of file diff --git a/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLTextCounterController.groovy b/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLTextCounterController.groovy index a9b8a24..e439751 100644 --- a/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLTextCounterController.groovy +++ b/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFXMLTextCounterController.groovy @@ -6,15 +6,15 @@ import javafx.scene.control.Label import javafx.scene.control.TextArea class GroovyFXMLTextCounterController { - TextArea textContent// text box area - Button updateText// button to update entered text from text box - Label textCountResult// text counter output result + public TextArea textContent// text box area + public Button updateText// button to update entered text from text box + public Label textCountResult// text counter output result - void calculateTextCount(ActionEvent actionEvent) {// update text count result when button is clicked - int chars = textContent.getLength()// get length of text area in characters - int words = textContent.getText().trim().split("\\s+").length// get length of text area in words - int lines = textContent.getText().split("\\r?\\n").length// get length of text area in lines - textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines)// update text - // counter output + void calculateTextCount(ActionEvent actionEvent) {// update text counter result when button is clicked + int chars = textContent.getLength()// get the length of text area in characters + int words = textContent.getText().trim().split("\\s+").length// get the length of text area in words + int lines = textContent.getText().split("\\r?\\n").length// get the length of text area in lines + textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: " + String.format("%,d", words) + + "\nLines: " + String.format("%,d", lines))// update text counter output } } \ No newline at end of file diff --git a/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFileTextCounterJavaFX.groovy b/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFileTextCounterJavaFX.groovy index 52eb1bf..bee82f6 100644 --- a/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFileTextCounterJavaFX.groovy +++ b/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyFileTextCounterJavaFX.groovy @@ -1,7 +1,5 @@ package com.example.javafxtextcountergroovy -import java.io.* - import javafx.application.Application import javafx.scene.Scene import javafx.scene.control.Button @@ -13,17 +11,15 @@ import javafx.stage.Stage class GroovyFileTextCounterJavaFX extends Application { static void main(String[] args) { - launch(GroovyFileTextCounterJavaFX.class, args)// launch the application by running the program + launch(args)// launch the application by running the program } void start(Stage primaryStage) throws FileNotFoundException { FlowPane root = new FlowPane() Scene scene = new Scene(root, 480, 320)// set the application size to 480x320 Button selectFile = new Button("Select file")// select the file from the dialog box - primaryStage.setScene(scene)// set the scene primaryStage.setTitle("Text Counter Application")// set the title of stage - Label textCountResult = new Label("Characters: 0\nWords: 0\nLines: 0")// set the default text counter output selectFile.setOnAction((event) -> { String textContent = ""// initialize empty text content string @@ -44,12 +40,12 @@ class GroovyFileTextCounterJavaFX extends Application { throw new RuntimeException(e)// throw runtime exception } } - int chars = textContent.length()// get length of opened text file in characters - int words = textContent.trim().split("\\s+").length// get length of opened text file in words - int lines = textContent.trim().split("\\r?\\n").length// get length of opened text file in lines - textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines)// update text - // counter - // output + int chars = textContent.length()// get the length of opened text file in characters + int words = textContent.trim().split("\\s+").length// get the length of opened text file in words + int lines = textContent.trim().split("\\r?\\n").length// get the length of opened text file in lines + textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: " + + String.format("%,d", words) + "\nLines: " + String.format("%,d", lines))// update text counter + // output }) root.getChildren().addAll(selectFile, textCountResult)// add all components to the stage primaryStage.show()// show the stage diff --git a/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyTextCounterJavaFX.groovy b/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyTextCounterJavaFX.groovy index 8f5c138..00b1eb0 100644 --- a/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyTextCounterJavaFX.groovy +++ b/groovy/javafx-textcounter-groovy/src/main/groovy/com/example/javafxtextcountergroovy/GroovyTextCounterJavaFX.groovy @@ -11,7 +11,7 @@ import javafx.stage.Stage class GroovyTextCounterJavaFX extends Application { static void main(String[] args) { - launch(GroovyTextCounterJavaFX.class, args)// launch the application by running the program + launch(args)// launch the application by running the program } void start(Stage primaryStage) { @@ -23,12 +23,12 @@ class GroovyTextCounterJavaFX extends Application { Button updateText = new Button("Calculate text count") Label textCountResult = new Label("Characters: 0\nWords: 0\nLines: 0")// set to default text counter output updateText.setOnAction((event) -> {// calculate text count when button is clicked - int chars = textContent.getLength()// get length of text area in characters - int words = textContent.getText().trim().split("\\s+").length// get length of text area in words - int lines = textContent.getText().split("\\r?\\n").length// get length of text area in lines - textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines)// update text - // counter - // output + int chars = textContent.getLength()// get the length of text area in characters + int words = textContent.getText().trim().split("\\s+").length// get the length of text area in words + int lines = textContent.getText().split("\\r?\\n").length// get the length of text area in lines + textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: " + + String.format("%,d", words) + "\nLines: " + String.format("%,d", lines))// update text counter + // output }) root.getChildren().addAll(textContent, updateText, textCountResult)// add all components to the stage primaryStage.show()// show the stage diff --git a/groovy/javaswing-textcounter-groovy/src/fileTextCounterGroovy.groovy b/groovy/javaswing-textcounter-groovy/src/fileTextCounterGroovy.groovy index 5e861ea..52e9732 100644 --- a/groovy/javaswing-textcounter-groovy/src/fileTextCounterGroovy.groovy +++ b/groovy/javaswing-textcounter-groovy/src/fileTextCounterGroovy.groovy @@ -1,4 +1,5 @@ import javax.swing.* +import javax.swing.filechooser.FileNameExtensionFilter import java.awt.* import java.awt.event.ActionEvent import java.awt.event.ActionListener @@ -7,7 +8,7 @@ class fileTextCounterGroovy { JButton selectFile = new JButton("Select file") JLabel textCountResult - fileTextCounterGroovy() { + fileTextCounterGroovy() { Frame textCount = new JFrame("File Text Counter Application")// initialize frame and set title of frame textCount.setLayout(new FlowLayout()) textCount.setSize(480, 320)// set application size to 480x320 @@ -17,29 +18,32 @@ class fileTextCounterGroovy { textCount.setVisible(true)// make the frame visible selectFile.addActionListener(new ActionListener() { @Override - void actionPerformed(ActionEvent e) {// when button is clicked - JFileChooser fileChooser = new JFileChooser()//create a new file chooser - int fileResult = fileChooser.showOpenDialog(textCount)//show file dialog + void actionPerformed(ActionEvent e) {// when button is clicked + JFileChooser fileChooser = new JFileChooser()// create a new file chooser + fileChooser.setFileFilter(new FileNameExtensionFilter("Text files", "txt")) + int fileResult = fileChooser.showOpenDialog(textCount)// show file dialog final String textContent try { textContent = getTextContent(fileResult, fileChooser) } catch (IOException ex) { throw new RuntimeException(ex) } - int chars = textContent.length()// get length of text area in characters - int words = textContent.trim().split("\\s+").length// get length of text area in words - int lines = textContent.split("\\r?\\n").length// get length of text area in lines - textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines)// update text count output + int chars = textContent.length()// get the length of text area in characters + int words = textContent.trim().split("\\s+").length// get the length of text area in words + int lines = textContent.split("\\r?\\n").length// get the length of text area in lines + textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: " + + String.format("%,d", words) + "\nLines: " + String.format("%,d", lines))// update text + // counter output } private static String getTextContent(int fileResult, JFileChooser fileChooser) throws IOException { - String textContent = ""//initialize empty text content string + String textContent = ""// initialize empty text content string if (fileResult == JFileChooser.APPROVE_OPTION) { - String filePath = fileChooser.getSelectedFile().getPath()//get the file path + String filePath = fileChooser.getSelectedFile().getPath()// get the file path try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { String line while ((line = reader.readLine()) != null) { - textContent += line + "\n"//read text from a file every line + textContent += line + "\n"// read text from a file every line } } } @@ -48,10 +52,10 @@ class fileTextCounterGroovy { }) } - static void main(String[] args) { + static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override - void run() { + void run() { new fileTextCounterGroovy()// launch the application by running the program } }) diff --git a/groovy/javaswing-textcounter-groovy/src/textCounterGroovy.groovy b/groovy/javaswing-textcounter-groovy/src/textCounterGroovy.groovy index 17792e1..d80b2b3 100644 --- a/groovy/javaswing-textcounter-groovy/src/textCounterGroovy.groovy +++ b/groovy/javaswing-textcounter-groovy/src/textCounterGroovy.groovy @@ -3,16 +3,16 @@ import java.awt.* import java.awt.event.ActionEvent import java.awt.event.ActionListener - class textCounterGroovy { +class textCounterGroovy { JTextArea textContent JButton updateText JLabel textCountResult - textCounterGroovy() { + textCounterGroovy() { Frame textCount = new JFrame("Text Counter Application")// initialize frame and set title of frame textCount.setLayout(new FlowLayout()) textCount.setSize(480, 320)// set application size to 480x320 - textContent = new JTextArea(10, 80)// create text area with 10 rows and 80 columns + textContent = new JTextArea(10, 40)// create text area with 10 rows and 40 columns textCount.add(textContent)// add components to the frame updateText = new JButton("Calculate text count") textCount.add(updateText) @@ -21,19 +21,21 @@ import java.awt.event.ActionListener textCount.setVisible(true)// make the frame visible updateText.addActionListener(new ActionListener() { @Override - void actionPerformed(ActionEvent e) {// when button is clicked - int chars = textContent.getText().length()// get length of text area in characters - int words = textContent.getText().trim().split("\\s+").length// get length of text area in words - int lines = textContent.getText().split("\\r?\\n").length// get length of text area in lines - textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines)// update text count output + void actionPerformed(ActionEvent e) {// when the button is clicked + int chars = textContent.getText().length()// get the length of text area in characters + int words = textContent.getText().trim().split("\\s+").length// get the length of text area in words + int lines = textContent.getText().split("\\r?\\n").length// get the length of text area in lines + textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: " + + String.format("%,d", words) + "\nLines: " + String.format("%,d", lines))// update text + // counter output } }) } - static void main(String[] args) { + static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override - void run() { + void run() { new textCounterGroovy()// launch the application by running the program } }) diff --git a/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLFileTextCounterApplication.java b/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLFileTextCounterApplication.java index b44bfc1..26b59c2 100644 --- a/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLFileTextCounterApplication.java +++ b/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLFileTextCounterApplication.java @@ -14,8 +14,8 @@ public void start(Stage stage) throws IOException { FXMLFileTextCounterApplication.class.getResource("fxml-file-text-counter.fxml"));// load FXML file // resource Scene scene = new Scene(fxmlLoader.load(), 480, 320);// set application size to 480x320 - stage.setTitle("Text Counter from a File");// set title of stage - stage.setScene(scene);// set stage to scene + stage.setTitle("Text Counter from a File");// set the title of stage + stage.setScene(scene);// set the stage to a scene stage.show();// show the stage } diff --git a/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLFileTextCounterController.java b/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLFileTextCounterController.java index 6f39df4..712b519 100644 --- a/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLFileTextCounterController.java +++ b/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLFileTextCounterController.java @@ -29,10 +29,10 @@ public void calculateTextCount(ActionEvent actionEvent) { throw new RuntimeException(e);// throw runtime exception } } - int chars = textContent.length();// get length of opened text file in characters - int words = textContent.trim().split("\\s+").length;// get length of opened text file in words - int lines = textContent.trim().split("\\r?\\n").length;// get length of opened text file in lines - textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines);// update text - // counter output + int chars = textContent.length();// get the length of opened text file in characters + int words = textContent.trim().split("\\s+").length;// get the length of opened text file in words + int lines = textContent.trim().split("\\r?\\n").length;// get the length of opened text file in lines + textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: " + String.format("%,d", words) + + "\nLines: " + String.format("%,d", lines));// update text counter output } } diff --git a/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLTextCounterApplication.java b/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLTextCounterApplication.java index 24b64fe..69205ae 100644 --- a/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLTextCounterApplication.java +++ b/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLTextCounterApplication.java @@ -15,8 +15,8 @@ public void start(Stage stage) throws IOException { // file // resource Scene scene = new Scene(fxmlLoader.load(), 480, 320);// set application size to 480x320 - stage.setTitle("Text Counter");// set title of stage - stage.setScene(scene);// set stage to scene + stage.setTitle("Text Counter");// set the title of stage + stage.setScene(scene);// set the stage to a scene stage.show();// show the stage } diff --git a/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLTextCounterController.java b/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLTextCounterController.java index d83f943..3687921 100644 --- a/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLTextCounterController.java +++ b/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FXMLTextCounterController.java @@ -10,11 +10,11 @@ public class FXMLTextCounterController { public Button updateText;// button to update entered text from text box public Label textCountResult;// text counter output result - public void calculateTextCount(ActionEvent actionEvent) {// update text count result when button is clicked - int chars = textContent.getLength();// get length of text area in characters - int words = textContent.getText().trim().split("\\s+").length;// get length of text area in words - int lines = textContent.getText().split("\\r?\\n").length;// get length of text area in lines - textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines);// update text count - // output + public void calculateTextCount(ActionEvent actionEvent) {// update a text count result when button is clicked + int chars = textContent.getLength();// get the length of text area in characters + int words = textContent.getText().trim().split("\\s+").length;// get the length of text area in words + int lines = textContent.getText().split("\\r?\\n").length;// get the length of text area in lines + textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: " + String.format("%,d", words) + + "\nLines: " + String.format("%,d", lines));// update text counter output } } \ No newline at end of file diff --git a/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FileTextCounterJavaFX.java b/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FileTextCounterJavaFX.java index acff01d..952d6cb 100644 --- a/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FileTextCounterJavaFX.java +++ b/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/FileTextCounterJavaFX.java @@ -19,10 +19,8 @@ public void start(Stage primaryStage) throws FileNotFoundException { FlowPane root = new FlowPane(); Scene scene = new Scene(root, 480, 320);// set the application size to 480x320 Button selectFile = new Button("Select file");// select the file from the dialog box - primaryStage.setScene(scene);// set the scene primaryStage.setTitle("Text Counter Application");// set the title of stage - Label textCountResult = new Label("Characters: 0\nWords: 0\nLines: 0");// set the default text counter output selectFile.setOnAction((event) -> { String textContent = "";// initialize empty text content string @@ -43,12 +41,12 @@ public void start(Stage primaryStage) throws FileNotFoundException { throw new RuntimeException(e);// throw runtime exception } } - int chars = textContent.length();// get length of opened text file in characters - int words = textContent.trim().split("\\s+").length;// get length of opened text file in words - int lines = textContent.trim().split("\\r?\\n").length;// get length of opened text file in lines - textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines);// update text - // counter - // output + int chars = textContent.length();// get the length of opened text file in characters + int words = textContent.trim().split("\\s+").length;// get the length of opened text file in words + int lines = textContent.trim().split("\\r?\\n").length;// get the length of opened text file in lines + textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: " + + String.format("%,d", words) + "\nLines: " + String.format("%,d", lines));// update text counter + // output }); root.getChildren().addAll(selectFile, textCountResult);// add all components to the stage primaryStage.show();// show the stage diff --git a/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/TextCounterJavaFX.java b/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/TextCounterJavaFX.java index d060679..c12f87b 100644 --- a/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/TextCounterJavaFX.java +++ b/java/javafx-textcounter/src/main/java/com/example/javafxtextcounter/TextCounterJavaFX.java @@ -23,12 +23,12 @@ public void start(Stage primaryStage) { Button updateText = new Button("Calculate text count"); Label textCountResult = new Label("Characters: 0\nWords: 0\nLines: 0");// set to default text counter output updateText.setOnAction((event) -> {// calculate text count when button is clicked - int chars = textContent.getLength();// get length of text area in characters - int words = textContent.getText().trim().split("\\s+").length;// get length of text area in words - int lines = textContent.getText().split("\\r?\\n").length;// get length of text area in lines - textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines);// update text - // counter - // output + int chars = textContent.getLength();// get the length of text area in characters + int words = textContent.getText().trim().split("\\s+").length;// get the length of text area in words + int lines = textContent.getText().split("\\r?\\n").length;// get the length of text area in lines + textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: " + + String.format("%,d", words) + "\nLines: " + String.format("%,d", lines));// update text counter + // output }); root.getChildren().addAll(textContent, updateText, textCountResult);// add all components to the stage primaryStage.show();// show the stage diff --git a/java/javaswing-textcounter/src/fileTextCounter.java b/java/javaswing-textcounter/src/fileTextCounter.java index d9b0db5..4cfc6b1 100644 --- a/java/javaswing-textcounter/src/fileTextCounter.java +++ b/java/javaswing-textcounter/src/fileTextCounter.java @@ -3,6 +3,7 @@ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import javax.swing.filechooser.FileNameExtensionFilter; public class fileTextCounter { JButton selectFile = new JButton("Select file"); @@ -20,6 +21,7 @@ public fileTextCounter() { @Override public void actionPerformed(ActionEvent e) {// when button is clicked JFileChooser fileChooser = new JFileChooser();// create a new file chooser + fileChooser.setFileFilter(new FileNameExtensionFilter("Text files", "txt")); int fileResult = fileChooser.showOpenDialog(textCount);// show file dialog final String textContent; try { @@ -27,13 +29,12 @@ public void actionPerformed(ActionEvent e) {// when button is clicked } catch (IOException ex) { throw new RuntimeException(ex); } - int chars = textContent.length();// get length of text area in characters - int words = textContent.trim().split("\\s+").length;// get length of text area in words - int lines = textContent.split("\\r?\\n").length;// get length of text area in lines - textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines);// update - // text - // count - // output + int chars = textContent.length();// get the length of text area in characters + int words = textContent.trim().split("\\s+").length;// get the length of text area in words + int lines = textContent.split("\\r?\\n").length;// get the length of text area in lines + textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: " + + String.format("%,d", words) + "\nLines: " + String.format("%,d", lines));// update text + // counter output } private static String getTextContent(int fileResult, JFileChooser fileChooser) throws IOException { diff --git a/java/javaswing-textcounter/src/textCounter.java b/java/javaswing-textcounter/src/textCounter.java index 93d2b8a..aad0161 100644 --- a/java/javaswing-textcounter/src/textCounter.java +++ b/java/javaswing-textcounter/src/textCounter.java @@ -12,7 +12,7 @@ public textCounter() { Frame textCount = new JFrame("Text Counter Application");// initialize frame and set title of frame textCount.setLayout(new FlowLayout()); textCount.setSize(480, 320);// set application size to 480x320 - textContent = new JTextArea(10, 80);// create text area with 10 rows and 80 columns + textContent = new JTextArea(10, 40);// create text area with 10 rows and 40 columns textCount.add(textContent);// add components to the frame updateText = new JButton("Calculate text count"); textCount.add(updateText); @@ -21,14 +21,13 @@ public textCounter() { textCount.setVisible(true);// make the frame visible updateText.addActionListener(new ActionListener() { @Override - public void actionPerformed(ActionEvent e) {// when button is clicked - int chars = textContent.getText().length();// get length of text area in characters - int words = textContent.getText().trim().split("\\s+").length;// get length of text area in words - int lines = textContent.getText().split("\\r?\\n").length;// get length of text area in lines - textCountResult.setText("Characters: " + chars + "\nWords: " + words + "\nLines: " + lines);// update - // text - // count - // output + public void actionPerformed(ActionEvent e) {// when the button is clicked + int chars = textContent.getText().length();// get the length of text area in characters + int words = textContent.getText().trim().split("\\s+").length;// get the length of text area in words + int lines = textContent.getText().split("\\r?\\n").length;// get the length of text area in lines + textCountResult.setText("Characters: " + String.format("%,d", chars) + "\nWords: " + + String.format("%,d", words) + "\nLines: " + String.format("%,d", lines));// update text + // counter output } }); } diff --git a/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFXMLFileTextCounterApplication.kt b/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFXMLFileTextCounterApplication.kt index eba73e0..c03326d 100644 --- a/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFXMLFileTextCounterApplication.kt +++ b/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFXMLFileTextCounterApplication.kt @@ -7,25 +7,26 @@ import javafx.scene.Scene import javafx.stage.Stage class KotlinFXMLFileTextCounterApplication : Application() { - @Throws(IOException::class) - override fun start(stage: Stage) { - val fxmlLoader = - FXMLLoader( - KotlinFXMLFileTextCounterApplication::class - .java - .getResource("kotlin-fxml-file-text-counter.fxml")) // load FXML file resource - val scene = Scene(fxmlLoader.load(), 480.0, 320.0) // set application size to 480x320 - stage.title = "Text Counter from a File" // set title of stage - stage.setScene(scene) // set stage to scene - stage.show() // show the stage - } + @Throws(IOException::class) + override fun start(stage: Stage) { + val fxmlLoader = + FXMLLoader( + KotlinFXMLFileTextCounterApplication::class.java.getResource( + "kotlin-fxml-file-text-counter.fxml" + ) + ) // load FXML file resource + val scene = Scene(fxmlLoader.load(), 480.0, 320.0) // set application size to 480x320 + stage.title = "Text Counter from a File" // set the title of stage + stage.scene = scene // set the stage to a scene + stage.show() // show the stage + } - companion object { - @JvmStatic - fun main(args: Array) { - launch( - KotlinFXMLFileTextCounterApplication::class - .java) // launch the application by running the program + companion object { + @JvmStatic + fun main(args: Array) { + launch( + KotlinFXMLFileTextCounterApplication::class.java + ) // launch the application by running the program + } } - } } diff --git a/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFXMLFileTextCounterController.kt b/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFXMLFileTextCounterController.kt index 0321222..d667ea7 100644 --- a/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFXMLFileTextCounterController.kt +++ b/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFXMLFileTextCounterController.kt @@ -9,49 +9,54 @@ import javafx.scene.control.Label import javafx.stage.FileChooser class KotlinFXMLFileTextCounterController { - var selectFile: Button? = null // button to select file - var textCountResult: Label? = null // text counter output result - var textContent = "" // initialize empty text content string + var selectFile: Button? = null // button to select file + var textCountResult: Label? = null // text counter output result + var textContent: String = "" // initialize empty text content string - fun calculateTextCount(actionEvent: ActionEvent?) { - var textContent = "" // initialize empty text content string - val fileChooser = FileChooser() // create a new file chooser - fileChooser.title = "Select a file" - fileChooser.extensionFilters.addAll( - FileChooser.ExtensionFilter("Text Files", "*.txt")) // choose .txt text files - val fileSelected = fileChooser.showOpenDialog(null) // show file dialog - if (fileSelected != null) { - try { - BufferedReader(FileReader(fileSelected)).use { reader -> - var line: String - while (reader.readLine().also { line = it } != null) { - textContent += line + "\n" // read text from file every line - } + fun calculateTextCount(actionEvent: ActionEvent?) { + var textContent = "" // initialize empty text content string + val fileChooser = FileChooser() // create a new file chooser + fileChooser.title = "Select a file" + fileChooser.extensionFilters.addAll( + FileChooser.ExtensionFilter("Text Files", "*.txt") + ) // choose .txt text files + val fileSelected = fileChooser.showOpenDialog(null) // show file dialog + if (fileSelected != null) { + try { + BufferedReader(FileReader(fileSelected)).use { reader -> + var line: String + while ((reader.readLine().also { line = it }) != null) { + textContent += line + "\n" // read text from file every line + } + } + } catch (e: IOException) { + throw RuntimeException(e) // throw runtime exception + } } - } catch (e: IOException) { - throw RuntimeException(e) // throw runtime exception - } + val chars = textContent.length // get the length of opened text file in characters + val words = + textContent + .trim { it <= ' ' } + .split("\\s+".toRegex()) + .dropLastWhile { it.isEmpty() } + .toTypedArray() + .size // get the length of opened text file in words + val lines = + textContent + .trim { it <= ' ' } + .split("\\r?\\n".toRegex()) + .dropLastWhile { it.isEmpty() } + .toTypedArray() + .size // get the length of opened text file in lines + textCountResult!!.text = + """ + Characters: ${String.format("%,d", chars)} + Words: ${String.format("%,d", words)} + Lines: ${String.format("%,d", lines)} + """.trimIndent() // update text counter output } - val chars = textContent.length // get length of opened text file in characters - val words = - textContent - .trim { it <= ' ' } - .split("\\s+".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray() - .size // get length of opened text file in words - val lines = - textContent - .trim { it <= ' ' } - .split("\\r?\\n".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray() - .size // get length of opened text file in lines - textCountResult!!.text = - "Characters: $chars\nWords: $words\nLines: $lines" // update text counter output - } - companion object { - var reader: BufferedReader? = null // initialize reader - } + companion object { + var reader: BufferedReader? = null // initialize reader + } } diff --git a/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFXMLTextCounterApplication.kt b/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFXMLTextCounterApplication.kt index 951b040..cfc8f05 100644 --- a/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFXMLTextCounterApplication.kt +++ b/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFXMLTextCounterApplication.kt @@ -7,25 +7,26 @@ import javafx.scene.Scene import javafx.stage.Stage class KotlinFXMLTextCounterApplication : Application() { - @Throws(IOException::class) - override fun start(stage: Stage) { - val fxmlLoader = - FXMLLoader( - KotlinFXMLTextCounterApplication::class - .java - .getResource("kotlin-fxml-text-counter.fxml")) // load FXML file resource - val scene = Scene(fxmlLoader.load(), 480.0, 320.0) // set application size to 480x320 - stage.title = "Text Counter" // set title of stage - stage.setScene(scene) // set stage to scene - stage.show() // show the stage - } + @Throws(IOException::class) + override fun start(stage: Stage) { + val fxmlLoader = + FXMLLoader( + KotlinFXMLTextCounterApplication::class.java.getResource( + "kotlin-fxml-text-counter.fxml" + ) + ) // load FXML file resource + val scene = Scene(fxmlLoader.load(), 480.0, 320.0) // set application size to 480x320 + stage.title = "Text Counter" // set the title of stage + stage.scene = scene // set the stage to a scene + stage.show() // show the stage + } - companion object { - @JvmStatic - fun main(args: Array) { - launch( - KotlinFXMLTextCounterApplication::class - .java) // launch the application by running the program + companion object { + @JvmStatic + fun main(args: Array) { + launch( + KotlinFXMLTextCounterApplication::class.java + ) // launch the application by running the program + } } - } } diff --git a/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFXMLTextCounterController.kt b/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFXMLTextCounterController.kt index 2380966..a06b984 100644 --- a/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFXMLTextCounterController.kt +++ b/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFXMLTextCounterController.kt @@ -6,30 +6,34 @@ import javafx.scene.control.Label import javafx.scene.control.TextArea class KotlinFXMLTextCounterController { - var textContent: TextArea? = null // text box area - var updateText: Button? = null // button to update entered text from text box - var textCountResult: Label? = null // text counter output result + var textContent: TextArea? = null // text box area + var updateText: Button? = null // button to update entered text from text box + var textCountResult: Label? = null // text counter output result - fun calculateTextCount( - actionEvent: ActionEvent? - ) { // update text count result when button is clicked - val chars = textContent!!.length // get length of text area in characters - val words = - textContent!! - .text - .trim { it <= ' ' } - .split("\\s+".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray() - .size // get length of text area in words - val lines = - textContent!! - .text - .split("\\r?\\n".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray() - .size // get length of text area in lines - textCountResult!!.text = "Characters: $chars\nWords: $words\nLines: $lines" // update text - // counter output - } + fun calculateTextCount( + actionEvent: ActionEvent? + ) { // update a text count result when button is clicked + val chars = textContent!!.length // get the length of text area in characters + val words = + textContent!! + .text + .trim { it <= ' ' } + .split("\\s+".toRegex()) + .dropLastWhile { it.isEmpty() } + .toTypedArray() + .size // get the length of text area in words + val lines = + textContent!! + .text + .split("\\r?\\n".toRegex()) + .dropLastWhile { it.isEmpty() } + .toTypedArray() + .size // get the length of text area in lines + textCountResult!!.text = + """ + Characters: ${String.format("%,d", chars)} + Words: ${String.format("%,d", words)} + Lines: ${String.format("%,d", lines)} + """.trimIndent() // update text counter output + } } diff --git a/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFileTextCounterJavaFX.kt b/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFileTextCounterJavaFX.kt index 6715d76..d74e080 100644 --- a/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFileTextCounterJavaFX.kt +++ b/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinFileTextCounterJavaFX.kt @@ -1,9 +1,6 @@ package com.example.javafxtextcounterkotlin -import java.io.BufferedReader -import java.io.FileNotFoundException -import java.io.FileReader -import java.io.IOException +import java.io.* import javafx.application.Application import javafx.event.ActionEvent import javafx.event.EventHandler @@ -15,61 +12,68 @@ import javafx.stage.FileChooser import javafx.stage.Stage class KotlinFileTextCounterJavaFX : Application() { - @Throws(FileNotFoundException::class) - override fun start(primaryStage: Stage) { - val root = FlowPane() - val scene = Scene(root, 480.0, 320.0) // set the application size to 480x320 - val selectFile = Button("Select file") // select the file from the dialog box - primaryStage.setScene(scene) // set the scene - primaryStage.title = "Text Counter Application" // set the title of stage - val textCountResult = - Label("Characters: 0\nWords: 0\nLines: 0") // set the default text counter output - selectFile.onAction = EventHandler { event: ActionEvent? -> - var textContent = "" // initialize empty text content string - val fileChooser = FileChooser() // create a new file chooser - fileChooser.title = "Select a file" - fileChooser.extensionFilters.addAll( - FileChooser.ExtensionFilter("Text Files", "*.txt")) // choose .txt text files - val fileSelected = fileChooser.showOpenDialog(null) // show file dialog - if (fileSelected != null) { - try { - BufferedReader(FileReader(fileSelected)).use { reader -> - var line: String - while (reader.readLine().also { line = it } != null) { - textContent += line + "\n" // read text from a file every line + @Throws(FileNotFoundException::class) + override fun start(primaryStage: Stage) { + val root = FlowPane() + val scene = Scene(root, 480.0, 320.0) // set the application size to 480x320 + val selectFile = Button("Select file") // select the file from the dialog box + primaryStage.scene = scene // set the scene + primaryStage.title = "Text Counter Application" // set the title of stage + val textCountResult = + Label("Characters: 0\nWords: 0\nLines: 0") // set the default text counter output + selectFile.onAction = EventHandler { event: ActionEvent? -> + var textContent = "" // initialize empty text content string + val fileChooser = FileChooser() // create a new file chooser + fileChooser.title = "Select a file" + fileChooser.extensionFilters.addAll( + FileChooser.ExtensionFilter("Text Files", "*.txt") + ) // choose .txt text files + val fileSelected = fileChooser.showOpenDialog(null) // show file dialog + if (fileSelected != null) { + try { + BufferedReader(FileReader(fileSelected)).use { reader -> + var line: String + while ((reader.readLine().also { line = it }) != null) { + textContent += line + "\n" // read text from a file every line + } + } + } catch (e: IOException) { + throw RuntimeException(e) // throw runtime exception + } } - } - } catch (e: IOException) { - throw RuntimeException(e) // throw runtime exception + val chars = textContent.length // get the length of opened text file in characters + val words = + textContent + .trim { it <= ' ' } + .split("\\s+".toRegex()) + .dropLastWhile { it.isEmpty() } + .toTypedArray() + .size // get the length of opened text file in words + val lines = + textContent + .trim { it <= ' ' } + .split("\\r?\\n".toRegex()) + .dropLastWhile { it.isEmpty() } + .toTypedArray() + .size // get the length of opened text file in lines + textCountResult.text = + "Characters: " + + String.format("%,d", chars) + + "\nWords: " + + String.format("%,d", words) + + "\nLines: " + + String.format("%,d", lines) // update text counter output } - } - val chars = textContent.length // get length of opened text file in characters - val words = - textContent - .trim { it <= ' ' } - .split("\\s+".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray() - .size // get length of opened text file in words - val lines = - textContent - .trim { it <= ' ' } - .split("\\r?\\n".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray() - .size // get length of opened text file in lines - textCountResult.text = - "Characters: $chars\nWords: $words\nLines: $lines" // update text counter output + root.children.addAll(selectFile, textCountResult) // add all components to the stage + primaryStage.show() // show the stage } - root.children.addAll(selectFile, textCountResult) // add all components to the stage - primaryStage.show() // show the stage - } - companion object { - @JvmStatic - fun main(args: Array) { - launch( - KotlinFileTextCounterJavaFX::class.java) // launch the application by running the program + companion object { + @JvmStatic + fun main(args: Array) { + launch( + KotlinFileTextCounterJavaFX::class.java + ) // launch the application by running the program + } } - } } diff --git a/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinTextCounterJavaFX.kt b/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinTextCounterJavaFX.kt index 3642c5f..0177a8a 100644 --- a/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinTextCounterJavaFX.kt +++ b/kotlin/javafx-textcounter-kotlin/src/main/kotlin/com/example/javafxtextcounterkotlin/KotlinTextCounterJavaFX.kt @@ -11,46 +11,55 @@ import javafx.scene.layout.FlowPane import javafx.stage.Stage class KotlinTextCounterJavaFX : Application() { - override fun start(primaryStage: Stage) { - val root = FlowPane() - val scene = Scene(root, 480.0, 320.0) // set the application size to 480x320 - primaryStage.setScene(scene) // set the scene - primaryStage.title = "Text Counter Application" // set title of stage - val textContent = TextArea() // text box area - val updateText = Button("Calculate text count") - val textCountResult = - Label("Characters: 0\nWords: 0\nLines: 0") // set to default text counter output - updateText.onAction = - EventHandler { event: ActionEvent? -> // calculate text count when button is clicked - val chars = textContent.length // get length of text - // area in characters - val words = - textContent.text - .trim { it <= ' ' } - .split("\\s+".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray() - .size // get length of text - // area in words - val lines = - textContent.text - .split("\\r?\\n".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray() - .size // get length of text - // area in lines - textCountResult.text = - "Characters: $chars\nWords: $words\nLines: $lines" // update text count output + override fun start(primaryStage: Stage) { + val root = FlowPane() + val scene = Scene(root, 480.0, 320.0) // set the application size to 480x320 + primaryStage.scene = scene // set the scene + primaryStage.title = "Text Counter Application" // set title of stage + val textContent = TextArea() // text box area + val updateText = Button("Calculate text count") + val textCountResult = + Label("Characters: 0\nWords: 0\nLines: 0") // set to default text counter output + updateText.onAction = + EventHandler { event: ActionEvent? -> // calculate text count when button is clicked + val chars = textContent.length // get the length of text area in characters + val words = + textContent + .text + .trim { it <= ' ' } + .split("\\s+".toRegex()) + .dropLastWhile { it.isEmpty() } + .toTypedArray() + .size // get the length of text area in words + val lines = + textContent + .text + .split("\\r?\\n".toRegex()) + .dropLastWhile { it.isEmpty() } + .toTypedArray() + .size // get the length of text area in lines + textCountResult.text = + "Characters: " + + String.format("%,d", chars) + + "\nWords: " + + String.format("%,d", words) + + "\nLines: " + + String.format("%,d", lines) // update text counter output + } + root.children.addAll( + textContent, + updateText, + textCountResult + ) // add all components to the stage + primaryStage.show() // show the stage } - root.children.addAll( - textContent, updateText, textCountResult) // add all components to the stage - primaryStage.show() // show the stage - } - companion object { - @JvmStatic - fun main(args: Array) { - launch(KotlinTextCounterJavaFX::class.java) // launch the application by running the program + companion object { + @JvmStatic + fun main(args: Array) { + launch( + KotlinTextCounterJavaFX::class.java + ) // launch the application by running the program + } } - } } diff --git a/kotlin/javaswing-textcounter-kotlin/src/main/kotlin/fileTextCounterKotlin.kt b/kotlin/javaswing-textcounter-kotlin/src/main/kotlin/fileTextCounterKotlin.kt index c8aa52d..47b638b 100644 --- a/kotlin/javaswing-textcounter-kotlin/src/main/kotlin/fileTextCounterKotlin.kt +++ b/kotlin/javaswing-textcounter-kotlin/src/main/kotlin/fileTextCounterKotlin.kt @@ -6,74 +6,80 @@ import java.io.BufferedReader import java.io.FileReader import java.io.IOException import javax.swing.* +import javax.swing.filechooser.FileNameExtensionFilter class fileTextCounterKotlin { - private var selectFile = JButton("Select file") - private var textCountResult: JLabel + var selectFile: JButton = JButton("Select file") + var textCountResult: JLabel - init { - val textCount: Frame = - JFrame("File Text Counter Application") // initialize frame and set title of frame - textCount.setLayout(FlowLayout()) - textCount.setSize(480, 320) // set application size to 480x320 - textCount.add(selectFile) // add components to the frame - textCountResult = - JLabel("Characters: 0\nWords: 0\nLines: 0") // set the default text counter output - textCount.add(textCountResult) - textCount.isVisible = true // make the frame visible - selectFile.addActionListener( - object : ActionListener { - override fun actionPerformed(e: ActionEvent) { // when button is clicked - val fileChooser = JFileChooser() // create a new file chooser - val fileResult = fileChooser.showOpenDialog(textCount) // show file dialog - val textContent: String - textContent = - try { - getTextContent(fileResult, fileChooser) - } catch (ex: IOException) { - throw RuntimeException(ex) - } - val chars = textContent.length // get length of text area in characters - val words = - textContent - .trim { it <= ' ' } - .split("\\s+".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray() - .size // get length of text area in words - val lines = - textContent - .split("\\r?\\n".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray() - .size // get length of text area in lines - textCountResult.setText( - "Characters: $chars\nWords: $words\nLines: $lines") // update text count output - } + init { + val textCount: Frame = + JFrame("File Text Counter Application") // initialize frame and set title of frame + textCount.layout = FlowLayout() + textCount.setSize(480, 320) // set application size to 480x320 + textCount.add(selectFile) // add components to the frame + textCountResult = + JLabel("Characters: 0\nWords: 0\nLines: 0") // set the default text counter output + textCount.add(textCountResult) + textCount.isVisible = true // make the frame visible + selectFile.addActionListener( + object : ActionListener { + override fun actionPerformed(e: ActionEvent) { // when button is clicked + val fileChooser = JFileChooser() // create a new file chooser + fileChooser.fileFilter = FileNameExtensionFilter("Text files", "txt") + val fileResult = fileChooser.showOpenDialog(textCount) // show file dialog + val textContent: String + try { + textContent = getTextContent(fileResult, fileChooser) + } catch (ex: IOException) { + throw RuntimeException(ex) + } + val chars = textContent.length // get the length of text area in characters + val words = + textContent + .trim { it <= ' ' } + .split("\\s+".toRegex()) + .dropLastWhile { it.isEmpty() } + .toTypedArray() + .size // get the length of text area in words + val lines = + textContent + .split("\\r?\\n".toRegex()) + .dropLastWhile { it.isEmpty() } + .toTypedArray() + .size // get the length of text area in lines + textCountResult.text = + """ + Characters: ${String.format("%,d", chars)} + Words: ${String.format("%,d", words)} + Lines: ${String.format("%,d", lines)} + """.trimIndent() // update text counter output + } - @Throws(IOException::class) - private fun getTextContent(fileResult: Int, fileChooser: JFileChooser): String { - var textContent = "" // initialize empty text content string - if (fileResult == JFileChooser.APPROVE_OPTION) { - val filePath = fileChooser.selectedFile.path // get the file path - BufferedReader(FileReader(filePath)).use { reader -> - var line: String - while (reader.readLine().also { line = it } != null) { - textContent += line + "\n" // read text from a file every line + @Throws(IOException::class) + private fun getTextContent(fileResult: Int, fileChooser: JFileChooser): String { + var textContent = "" // initialize empty text content string + if (fileResult == JFileChooser.APPROVE_OPTION) { + val filePath = fileChooser.selectedFile.path // get the file path + BufferedReader(FileReader(filePath)).use { reader -> + var line: String + while ((reader.readLine().also { line = it }) != null) { + textContent += line + "\n" // read text from a file every line + } + } + } + return textContent + } } - } - } - return textContent - } - }) - } + ) + } - companion object { - @JvmStatic - fun main(args: Array) { - SwingUtilities.invokeLater { - fileTextCounterKotlin() // launch the application by running the program - } + companion object { + @JvmStatic + fun main(args: Array) { + SwingUtilities.invokeLater { + fileTextCounterKotlin() // launch the application by running the program + } + } } - } } diff --git a/kotlin/javaswing-textcounter-kotlin/src/main/kotlin/textCounterKotlin.kt b/kotlin/javaswing-textcounter-kotlin/src/main/kotlin/textCounterKotlin.kt index 0ff46f2..5db2119 100644 --- a/kotlin/javaswing-textcounter-kotlin/src/main/kotlin/textCounterKotlin.kt +++ b/kotlin/javaswing-textcounter-kotlin/src/main/kotlin/textCounterKotlin.kt @@ -3,52 +3,58 @@ import java.awt.Frame import javax.swing.* class textCounterKotlin { - private var textContent: JTextArea - private var updateText: JButton - private var textCountResult: JLabel + var textContent: JTextArea + var updateText: JButton + var textCountResult: JLabel - init { - val textCount: Frame = - JFrame("Text Counter Application") // initialize frame and set title of frame - textCount.setLayout(FlowLayout()) - textCount.setSize(480, 320) // set application size to 480x320 - textContent = JTextArea(10, 80) // create text area with 10 rows and 80 columns - textCount.add(textContent) // add components to the frame - updateText = JButton("Calculate text count") - textCount.add(updateText) - textCountResult = - JLabel("Characters: 0\nWords: 0\nLines: 0") // set the default text counter output - textCount.add(textCountResult) - textCount.isVisible = true // make the frame visible - updateText.addActionListener { - // when button is clicked - val chars = textContent.getText().length // get length of text area in characters - val words = - textContent - .getText() - .trim { it <= ' ' } - .split("\\s+".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray() - .size // get length of text area in words - val lines = - textContent - .getText() - .split("\\r?\\n".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray() - .size // get length of text area in lines - textCountResult.setText( - "Characters: $chars\nWords: $words\nLines: $lines") // update text counter output + init { + val textCount: Frame = + JFrame("Text Counter Application") // initialize frame and set title of frame + textCount.layout = FlowLayout() + textCount.setSize(480, 320) // set application size to 480x320 + textContent = JTextArea(10, 40) // create text area with 10 rows and 40 columns + textCount.add(textContent) // add components to the frame + updateText = JButton("Calculate text count") + textCount.add(updateText) + textCountResult = + JLabel("Characters: 0\nWords: 0\nLines: 0") // set the default text counter output + textCount.add(textCountResult) + textCount.isVisible = true // make the frame visible + updateText.addActionListener { + // when the button is clicked + val chars = textContent.text.length // get the length of text area in characters + val words = + textContent + .text + .trim { it <= ' ' } + .split("\\s+".toRegex()) + .dropLastWhile { it.isEmpty() } + .toTypedArray() + .size // get the length of text area in words + val lines = + textContent + .text + .split("\\r?\\n".toRegex()) + .dropLastWhile { it.isEmpty() } + .toTypedArray() + .size // get the length of text area in lines + textCountResult.text = + """ + Characters: ${String.format("%,d", chars)} + Words: + """.trimIndent() + + String.format("%,d", words) + + "\nLines: " + + String.format("%,d", lines) // update text counter output + } } - } - companion object { - @JvmStatic - fun main(args: Array) { - SwingUtilities.invokeLater { - textCounterKotlin() // launch the application by running the program - } + companion object { + @JvmStatic + fun main(args: Array) { + SwingUtilities.invokeLater { + textCounterKotlin() // launch the application by running the program + } + } } - } }