Skip to content

Instantly share code, notes, and snippets.

@tarrsalah
Last active September 20, 2021 11:08
Show Gist options
  • Save tarrsalah/6340796 to your computer and use it in GitHub Desktop.
Save tarrsalah/6340796 to your computer and use it in GitHub Desktop.
Visualize the contents of a text file in JavaFX TextArea.
package org.tarrsalah.fileReader;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
* App.java (UTF-8)
*
* Aug 26, 2013
*
* @author tarrsalah.org
*/
public class App extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
stage.setScene(new Scene((Parent) FXMLLoader.load(getClass().getClassLoader().getResource("View.fxml"))));
stage.show();
}
}
package org.tarrsalah.fileReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* TextFileReaderImp.java (UTF-8)
*
* Aug 26, 2013
*
* @author tarrsalah.org
*/
public class TextFileReader {
@SuppressWarnings("NestedAssignment")
public List<String> read(File file) {
List<String> lines = new ArrayList<String>();
String line;
try {
BufferedReader br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null) {
lines.add(line);
}
br.close();
} catch (IOException ex) {
Logger.getLogger(TextFileReader.class.getName()).log(Level.SEVERE, null, ex);
}
return lines;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<BorderPane prefHeight="341.0" prefWidth="426.0" xmlns:fx="http://javafx.com/fxml" fx:controller="org.tarrsalah.fileReader.ViewController" >
<center>
<TextArea id="text" fx:id="linesTextArea" prefWidth="200.0" wrapText="true">
<BorderPane.margin>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</BorderPane.margin>
</TextArea>
</center>
<top>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0">
<children>
<Button fx:id="showButton" mnemonicParsing="false" text="show" onAction="#showFileLines" />
<TextField fx:id="urlTextField" prefWidth="-1.0" HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets left="20.0" />
</HBox.margin>
</TextField>
</children>
<padding>
<Insets left="20.0" right="20.0" />
</padding>
</HBox>
</top>
</BorderPane>
package org.tarrsalah.fileReader;
import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Logger;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
/**
* ViewController.java (UTF-8)
*
* Aug 26, 2013
*
* @author tarrsalah.org
*/
public class ViewController implements Initializable {
@FXML
private TextField urlTextField;
@FXML
private TextArea linesTextArea;
private static final Logger LOG = Logger.getLogger(ViewController.class.getName());
private Future<List<String>> future;
private ExecutorService executorService = Executors.newSingleThreadExecutor();
private TextFileReader reader = new TextFileReader();
public void initialize(URL url, ResourceBundle rb) {
}
@FXML
@SuppressWarnings("NestedAssignment")
public void showFileLines() throws InterruptedException, ExecutionException {
future = executorService.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return reader.read(new File(ViewController.this.urlTextField.getText()));
}
});
List<String> lines = future.get();
executorService.shutdownNow();
linesTextArea.clear();
for (String line : lines ) {
linesTextArea.appendText(line + "\n");
}
}
}
@jmhuddle4
Copy link

tarrsalah this is awesome and exactly what I want to do with my javafx application. I downloaded your code and opened it in my netbeans but I got a JavaFX Launcher error: Exception while running application. Do you happen to know what the issue may be? I'm running Java 8 what did you run? Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment