Skip to content

Instantly share code, notes, and snippets.

@gnusosa
Created April 27, 2018 04:25
Show Gist options
  • Save gnusosa/537a5cdb583cf39e3068f8401de9221b to your computer and use it in GitHub Desktop.
Save gnusosa/537a5cdb583cf39e3068f8401de9221b to your computer and use it in GitHub Desktop.
JavaFX Reader to Text Area
package org.gnusosa;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ReadText extends FileManager {
private Scanner scanner;
public Scanner getScanner() {
return scanner;
}
public ReadText(Scanner scanner) {
this.scanner = scanner;
}
public ReadText(String path) throws FileNotFoundException {
super(path);
this.scanner = new Scanner(this.getFileHandler());
}
@Override
public String getLine(int lineNumber) {
String[] stringArray = this.getArrayOfLines();
this.getScanner().reset();
return stringArray[lineNumber];
}
@Override
public String getAll() {
StringBuffer sbuffer = new StringBuffer();
while(this.getScanner().hasNext()) {
sbuffer.append(this.getScanner().nextLine() + "\n");
}
this.getScanner().reset();
return sbuffer.toString();
}
@Override
public String[] getArrayOfLines() {
ArrayList<String> stringArray = new ArrayList<String>();
while(this.getScanner().hasNextLine()) {
stringArray.add(this.getScanner().nextLine());
}
this.getScanner().reset();
String[] stockArr = new String[stringArray.size()];
return stringArray.toArray(stockArr);
}
}
package org.gnusosa;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import java.io.FileNotFoundException;
import java.util.HashSet;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(0, 0, 0, 0));
Scene scene = new Scene(grid, 800, 600);
Text scenetitle = new Text("Read Text File");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
grid.add(scenetitle, 0, 0, 2, 1);
TextArea userTextArea = new TextArea();
grid.add(userTextArea, 1, 1);
TextField userTextField = new TextField();
grid.add(userTextField, 1, 4);
Button btn = new Button("Read File to TextArea");
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
hbBtn.getChildren().add(btn);
grid.add(hbBtn, 0, 4);
final Text actionTarget = new Text();
grid.add(actionTarget, 1, 6);
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
String filePath = userTextField.getText();
try {
ReadText readText = new ReadText(filePath);
userTextArea.setWrapText(false);
userTextArea.setText(readText.getAll());
} catch (FileNotFoundException err) {
actionTarget.setFill(Color.FIREBRICK);
actionTarget.setText("Couldn't open the file. Error: " + err.toString());
}
}
});
primaryStage.setScene(scene);
primaryStage.setTitle("Read a text file");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package org.gnusosa;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ReadText extends FileManager {
private Scanner scanner;
public Scanner getScanner() {
return scanner;
}
public ReadText(Scanner scanner) {
this.scanner = scanner;
}
public ReadText(String path) throws FileNotFoundException {
super(path);
this.scanner = new Scanner(this.getFileHandler());
}
@Override
public String getLine(int lineNumber) {
String[] stringArray = this.getArrayOfLines();
this.getScanner().reset();
return stringArray[lineNumber];
}
@Override
public String getAll() {
StringBuffer sbuffer = new StringBuffer();
while(this.getScanner().hasNext()) {
sbuffer.append(this.getScanner().nextLine() + "\n");
}
this.getScanner().reset();
return sbuffer.toString();
}
@Override
public String[] getArrayOfLines() {
ArrayList<String> stringArray = new ArrayList<String>();
while(this.getScanner().hasNextLine()) {
stringArray.add(this.getScanner().nextLine());
}
this.getScanner().reset();
String[] stockArr = new String[stringArray.size()];
return stringArray.toArray(stockArr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment