Write a program in JavaFx that displays a text file in a text area, as shown in
ID: 654394 • Letter: W
Question
Write a program in JavaFx that displays a text file in a text area, as shown in Figure 17.20a. The user enters a file name in a text field and clicks the View button; the file is then displayed in a text area of a scrollPane.
Hint:
This code block will get the file name from a text area, read the file line by line will writing to the scollPane/text area.
try{
String S = FileLocation.getText().trim;
Scanner SelectedFile = new Scanner(new File(S) );
while(SelectedFile.hasNext()) {
String 1 = SelectedFile.nextLine();
Files.append(1 + " ";
}
}
Explanation / Answer
package javafxfilechooser;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextAreaBuilder;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
/**
*
*/
public class JavaFX_OpenFile extends Application {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(final Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");
Group root = new Group();
final TextArea textArea = TextAreaBuilder.create()
.prefWidth(400)
.wrapText(true)
.build();
ScrollPane scrollPane = new ScrollPane();
scrollPane.getStyleClass().add("noborder-scroll-pane");
scrollPane.setContent(textArea);
scrollPane.setFitToWidth(true);
scrollPane.setPrefWidth(400);
scrollPane.setPrefHeight(180);
Button buttonLoad = new Button("Load");
buttonLoad.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent arg0) {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
fileChooser.getExtensionFilters().add(extFilter);
//Show save file dialog
File file = fileChooser.showOpenDialog(primaryStage);
if(file != null){
textArea.setText(readFile(file));
}
}
});
VBox vBox = VBoxBuilder.create()
.children(buttonLoad, scrollPane)
.build();
root.getChildren().add(vBox);
primaryStage.setScene(new Scene(root, 500, 400));
primaryStage.show();
}
private String readFile(File file){
StringBuilder stringBuffer = new StringBuilder();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(file));
String text;
while ((text = bufferedReader.readLine()) != null) {
stringBuffer.append(text);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(JavaFX_OpenFile.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(JavaFX_OpenFile.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
bufferedReader.close();
} catch (IOException ex) {
Logger.getLogger(JavaFX_OpenFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
return stringBuffer.toString();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.