Skip to content

Commit

Permalink
Format Java code
Browse files Browse the repository at this point in the history
  • Loading branch information
lulunac27a committed Nov 8, 2024
1 parent 6abfd0c commit 70fadbc
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 228 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module com.example.javafxtextcountergroovy {
requires javafx.controls;
requires javafx.fxml;
requires org.apache.groovy;
requires javafx.controls;
requires javafx.fxml;
requires org.apache.groovy;

opens com.example.javafxtextcountergroovy to javafx.fxml;

opens com.example.javafxtextcountergroovy to javafx.fxml;
exports com.example.javafxtextcountergroovy;
}
exports com.example.javafxtextcountergroovy;
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package com.example.javafxtextcounter;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class FXMLFileTextCounterApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(
FXMLFileTextCounterApplication.class.getResource("fxml-file-text-counter.fxml"));// load FXML file
// resource
Scene scene = new Scene(fxmlLoader.load(), 480, 400);// set application size to 480x400
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
}

public static void main(String[] args) {
launch();// launch the application by running the program
}
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(
FXMLFileTextCounterApplication.class.getResource(
"fxml-file-text-counter.fxml")); // load FXML file resource
Scene scene = new Scene(fxmlLoader.load(), 480, 400); // set application size to 480x400
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
}

public static void main(String[] args) {
launch(); // launch the application by running the program
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,40 @@
import javafx.stage.FileChooser;

public class FXMLFileTextCounterController {
public Button selectFile;// button to select file
public Label textCountResult;// text counter output result
static BufferedReader reader = null;// initialize reader
String textContent = "";// initialize empty text content string

public void calculateTextCount(ActionEvent actionEvent) {
String textContent = "";// initialize empty text content string
FileChooser fileChooser = new FileChooser();// create a new file chooser
fileChooser.setTitle("Select a file");
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Text Files", "*.txt"));// choose .txt
// text files
File fileSelected = fileChooser.showOpenDialog(null);// show file dialog
if (fileSelected != null) {
try (BufferedReader reader = new BufferedReader(new FileReader(fileSelected))) {
String line;
while ((line = reader.readLine()) != null) {
textContent += line + "\n";// read text from file every line
}
} catch (IOException e) {
throw new RuntimeException(e);// throw runtime exception
}
public Button selectFile; // button to select file
public Label textCountResult; // text counter output result
static BufferedReader reader = null; // initialize reader
String textContent = ""; // initialize empty text content string

public void calculateTextCount(ActionEvent actionEvent) {
String textContent = ""; // initialize empty text content string
FileChooser fileChooser = new FileChooser(); // create a new file chooser
fileChooser.setTitle("Select a file");
fileChooser
.getExtensionFilters()
.addAll(new FileChooser.ExtensionFilter("Text Files", "*.txt")); // choose .txt text files
File fileSelected = fileChooser.showOpenDialog(null); // show file dialog
if (fileSelected != null) {
try (
BufferedReader reader = new BufferedReader(new FileReader(fileSelected))) {
String line;
while ((line = reader.readLine()) != null) {
textContent += line + "\n"; // read text from file every line
}
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
} catch (IOException e) {
throw new RuntimeException(e); // throw runtime exception
}
}
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
@@ -1,26 +1,24 @@
package com.example.javafxtextcounter;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class FXMLTextCounterApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(FXMLTextCounterApplication.class.getResource("fxml-text-counter.fxml"));// load
// FXML
// file
// resource
Scene scene = new Scene(fxmlLoader.load(), 480, 400);// set application size to 480x400
stage.setTitle("Text Counter");// set the title of stage
stage.setScene(scene);// set the stage to a scene
stage.show();// show the stage
}

public static void main(String[] args) {
launch();// launch the application by running the program
}
}
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(
FXMLTextCounterApplication.class.getResource("fxml-text-counter.fxml")); // load FXML file resource
Scene scene = new Scene(fxmlLoader.load(), 480, 400); // set application size to 480x400
stage.setTitle("Text Counter"); // set the title of stage
stage.setScene(scene); // set the stage to a scene
stage.show(); // show the stage
}

public static void main(String[] args) {
launch(); // launch the application by running the program
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@
import javafx.scene.control.TextArea;

public class FXMLTextCounterController {
public TextArea textContent;// text box area
public Button updateText;// button to update entered text from text box
public Label textCountResult;// text counter output result

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
}
}
public TextArea textContent; // text box area
public Button updateText; // button to update entered text from text box
public Label textCountResult; // text counter output result

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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,50 @@

public class FileTextCounterJavaFX extends Application {

public static void main(String[] args) {
launch(args);// launch the application by running the program
}
public static void main(String[] args) {
launch(args); // launch the application by running the program
}

public void start(Stage primaryStage) throws FileNotFoundException {
FlowPane root = new FlowPane();
Scene scene = new Scene(root, 480, 400);// set the application size to 480x400
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
FileChooser fileChooser = new FileChooser();// create a new file chooser
fileChooser.setTitle("Select a file");
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Text Files", "*.txt"));// choose
// .txt
// text
// files
File fileSelected = fileChooser.showOpenDialog(null);// show file dialog
if (fileSelected != null) {
try (BufferedReader reader = new BufferedReader(new FileReader(fileSelected))) {
String line;
while ((line = reader.readLine()) != null) {
textContent += line + "\n";// read text from a file every line
}
} catch (IOException e) {
throw new RuntimeException(e);// throw runtime exception
}
}
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
}
public void start(Stage primaryStage) throws FileNotFoundException {
FlowPane root = new FlowPane();
Scene scene = new Scene(root, 480, 400); // set the application size to 480x400
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
FileChooser fileChooser = new FileChooser(); // create a new file chooser
fileChooser.setTitle("Select a file");
fileChooser
.getExtensionFilters()
.addAll(new FileChooser.ExtensionFilter("Text Files", "*.txt")); // choose .txt
// text files
File fileSelected = fileChooser.showOpenDialog(null); // show file dialog
if (fileSelected != null) {
try (
BufferedReader reader = new BufferedReader(
new FileReader(fileSelected))) {
String line;
while ((line = reader.readLine()) != null) {
textContent += line + "\n"; // read text from a file every line
}
} catch (IOException e) {
throw new RuntimeException(e); // throw runtime exception
}
}
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,31 @@

public class TextCounterJavaFX extends Application {

public static void main(String[] args) {
launch(args);// launch the application by running the program
}
public static void main(String[] args) {
launch(args); // launch the application by running the program
}

public void start(Stage primaryStage) {
FlowPane root = new FlowPane();
Scene scene = new Scene(root, 480, 400);// set the application size to 480x400
primaryStage.setScene(scene);// set the scene
primaryStage.setTitle("Text Counter Application");// set title of stage
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.setOnAction((event) -> {// calculate text count 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
});
root.getChildren().addAll(textContent, updateText, textCountResult);// add all components to the stage
primaryStage.show();// show the stage
}
public void start(Stage primaryStage) {
FlowPane root = new FlowPane();
Scene scene = new Scene(root, 480, 400); // set the application size to 480x400
primaryStage.setScene(scene); // set the scene
primaryStage.setTitle("Text Counter Application"); // set title of stage
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.setOnAction(event -> { // calculate text count 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
});
root.getChildren().addAll(textContent, updateText, textCountResult); // add all components to the stage
primaryStage.show(); // show the stage
}
}
10 changes: 5 additions & 5 deletions java/javafx-textcounter/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module com.example.javafxtextcounter {
requires javafx.controls;
requires javafx.fxml;
requires javafx.controls;
requires javafx.fxml;

opens com.example.javafxtextcounter to javafx.fxml;

opens com.example.javafxtextcounter to javafx.fxml;
exports com.example.javafxtextcounter;
}
exports com.example.javafxtextcounter;
}
Loading

0 comments on commit 70fadbc

Please sign in to comment.