Skip to content

Commit

Permalink
Add commas for thousand seperator and fix text box rendering outside …
Browse files Browse the repository at this point in the history
…area
  • Loading branch information
lulunac27a committed Mar 11, 2024
1 parent 2587cf0 commit 757f1a1
Show file tree
Hide file tree
Showing 24 changed files with 456 additions and 417 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.example.javafxtextcountergroovy

import java.io.*

import javafx.application.Application
import javafx.scene.Scene
import javafx.scene.control.Button
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
}
}
}
Expand All @@ -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
}
})
Expand Down
22 changes: 12 additions & 10 deletions groovy/javaswing-textcounter-groovy/src/textCounterGroovy.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Loading

0 comments on commit 757f1a1

Please sign in to comment.