package javafxreadtextfile; import java.io.File; import java.io.IOException; import java.nio.file.Files; 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.Scene; import javafx.scene.control.Button; import javafx.scene.layout.VBox; import javafx.stage.FileChooser; import javafx.stage.Stage; /** * * @web http://java-buddy.blogspot.com/ */ public class JavaFXReadTextFile extends Application { @Override public void start(Stage primaryStage) { Button btnLoad = new Button("Load"); btnLoad.setOnAction(btnLoadEventListener); VBox rootBox = new VBox(); rootBox.getChildren().add(btnLoad); Scene scene = new Scene(rootBox, 300, 300); primaryStage.setTitle("java-buddy.blogspot.com"); primaryStage.setScene(scene); primaryStage.show(); } EventHandler<ActionEvent> btnLoadEventListener = (ActionEvent event) -> { FileChooser fileChooser = new FileChooser(); fileChooser.getExtensionFilters() .addAll( new FileChooser.ExtensionFilter("TXT files (*.TXT)", "*.TXT"), new FileChooser.ExtensionFilter("txt files (*.txt)", "*.txt")); File file = fileChooser.showOpenDialog(null); if (file != null) { try { Files.lines(file.toPath()).forEach(System.out::println); } catch (IOException ex) { Logger.getLogger(JavaFXReadTextFile.class.getName()) .log(Level.SEVERE, null, ex); } } }; public static void main(String[] args) { launch(args); } }
Monday, December 28, 2015
Files.lines() read text file returned by JavaFX FileChooser
Example show how to read all lines from a file returned by JavaFX FileChooser, using Files.lines().
Sunday, December 6, 2015
JavaFX FileChooser to open image file, and display on ImageView, run on Ubuntu GNOME 15.10/NetBeans 8.1
Re-test "Use JavaFX FileChooser to open image file, and display on ImageView" on Ubuntu GNOME 15.10/NetBeans 8.1.
package javafxpixel; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.VBox; import javafx.stage.FileChooser; import javafx.stage.Stage; import javax.imageio.ImageIO; /** * @web http://java-buddy.blogspot.com/ */ public class JavaFxPixel extends Application { ImageView myImageView; @Override public void start(Stage primaryStage) { Button btnLoad = new Button("Load"); btnLoad.setOnAction(btnLoadEventListener); myImageView = new ImageView(); VBox rootBox = new VBox(); rootBox.getChildren().addAll(btnLoad, myImageView); Scene scene = new Scene(rootBox, 300, 300); primaryStage.setTitle("java-buddy.blogspot.com"); primaryStage.setScene(scene); primaryStage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } EventHandler<ActionEvent> btnLoadEventListener = new EventHandler<ActionEvent>(){ @Override public void handle(ActionEvent t) { FileChooser fileChooser = new FileChooser(); //Set extension filter FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.JPG)", "*.JPG"); FileChooser.ExtensionFilter extFilterjpg = new FileChooser.ExtensionFilter("jpg files (*.jpg)", "*.jpg"); FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.PNG)", "*.PNG"); FileChooser.ExtensionFilter extFilterpng = new FileChooser.ExtensionFilter("png files (*.png)", "*.png"); fileChooser.getExtensionFilters() .addAll(extFilterJPG, extFilterjpg, extFilterPNG, extFilterpng); //Show open file dialog File file = fileChooser.showOpenDialog(null); try { BufferedImage bufferedImage = ImageIO.read(file); Image image = SwingFXUtils.toFXImage(bufferedImage, null); myImageView.setImage(image); } catch (IOException ex) { Logger.getLogger(JavaFxPixel.class.getName()).log(Level.SEVERE, null, ex); } } }; }
Subscribe to:
Posts (Atom)