Skip to content

Commit

Permalink
Add annotations and fix errors while setting the text counter output …
Browse files Browse the repository at this point in the history
…in Groovy
  • Loading branch information
lulunac27a committed Mar 17, 2024
1 parent 59223c8 commit 151397d
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.javafxtextcountergroovy

import groovy.transform.CompileStatic
import javafx.application.Application
import javafx.fxml.FXMLLoader
import javafx.scene.Parent
Expand All @@ -8,6 +9,7 @@ import javafx.stage.Stage

import java.io.IOException

@CompileStatic
class GroovyFXMLFileTextCounterApplication extends Application {

static void main(String[] args) {
Expand All @@ -16,8 +18,8 @@ class GroovyFXMLFileTextCounterApplication extends Application {
@Override
void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(
GroovyFXMLFileTextCounterApplication.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.title = 'Text Counter from a File'// set the title of stage
stage.scene = scene// set the stage to a scene
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.example.javafxtextcountergroovy

import javafx.event.ActionEvent
import javafx.fxml.FXML
import javafx.scene.control.Button
import javafx.scene.control.Label
import javafx.stage.FileChooser

class GroovyFXMLFileTextCounterController {

@FXML
Button selectFile// button to select file
@FXML
Label textCountResult// text counter output result
static BufferedReader reader = null// initialize reader
String textContent = ''// initialize empty text content string

@FXML
void calculateTextCount(ActionEvent actionEvent) {
String textContent = ''// initialize empty text content string
FileChooser fileChooser = new FileChooser()// create a new file chooser
Expand All @@ -32,8 +36,7 @@ class GroovyFXMLFileTextCounterController {
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.text = 'Characters: ' + String.format('%,d', chars) + '\nWords: ' + String.format('%,d', words)
+ '\nLines: ' + String.format('%,d', lines)// update text counter output
textCountResult.text = '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,5 +1,6 @@
package com.example.javafxtextcountergroovy

import groovy.transform.CompileStatic
import javafx.application.Application
import javafx.fxml.FXMLLoader
import javafx.scene.Parent
Expand All @@ -8,17 +9,18 @@ import javafx.stage.Stage

import java.io.IOException

@CompileStatic
class GroovyFXMLTextCounterApplication extends Application {

static void main(String[] args) {
launch()// launch the application by running the program
}
@Override
void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(GroovyFXMLTextCounterApplication.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.title = 'Text Counter'// set the title of stage
stage.scene = scene// set the stage to a scene
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package com.example.javafxtextcountergroovy

import javafx.event.ActionEvent
import javafx.fxml.FXML
import javafx.scene.control.Button
import javafx.scene.control.Label
import javafx.scene.control.TextArea

class GroovyFXMLTextCounterController {

@FXML
public TextArea textContent// text box area
@FXML
public Button updateText// button to update entered text from text box
@FXML
public Label textCountResult// text counter output result

@FXML
void calculateTextCount(ActionEvent actionEvent) { // update text counter result when button is clicked
int chars = textContent.length// get the length of text area in characters
int words = textContent.text.trim().split('\\s+').length// get the length of text area in words
int lines = textContent.text.split('\\r?\\n').length// 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
textCountResult.text = '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 @@ -43,9 +43,7 @@ class GroovyFileTextCounterJavaFX extends Application {
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
textCountResult.setText('Characters: ' + String.format('%,d', chars) + '\nWords: ' + String.format('%,d', words) + '\nLines: ' + String.format('%,d', lines))// update text counter output
}
root.children.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 @@ -22,14 +22,12 @@ class GroovyTextCounterJavaFX extends Application {
TextArea textContent = new TextArea()// text box area
Button updateText = new Button('Calculate text count')
Label textCountResult = new Label('Characters: 0\nWords: 0\nLines: 0')// set to default text counter output
updateText.onAction((event) -> { // calculate text count when button is clicked
updateText.onAction = (event) -> { // calculate text count when button is clicked
int chars = textContent.length// get the length of text area in characters
int words = textContent.text.trim().split('\\s+').length// get the length of text area in words
int lines = textContent.text.split('\\r?\\n').length// 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
})
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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ class fileTextCounterGroovy {
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.text = 'Characters: ' + String.format('%,d', chars) + '\nWords: '
+ String.format('%,d', words) + '\nLines: ' + String.format('%,d', lines)// update text
// counter output
textCountResult.text = '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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ class textCounterGroovy {
int chars = textContent.text.length// get the length of text area in characters
int words = textContent.text.trim().split('\\s+').length// get the length of text area in words
int lines = textContent.text.split('\\r?\\n').length// 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
textCountResult.text = 'Characters: ' + String.format('%,d', chars) + '\nWords: ' + String.format('%,d', words) + '\nLines: ' + String.format('%,d', lines)// update text counter output
}

})
Expand Down

0 comments on commit 151397d

Please sign in to comment.