Tuesday, January 8, 2013

Use JavaFX FileChooser to open image file, and display on ImageView

The example demonstrate how to implement JavaFX FileChooser to open image file (jpg or png), and display it in a ImageView.

Use JavaFX FileChooser to open image file, and display on ImageView
Use JavaFX FileChooser to open image file, and display on ImageView


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();
    }

    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 extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
            fileChooser.getExtensionFilters().addAll(extFilterJPG, 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);
            }

        }
    };
}


Related:
- Make ImageView scrollable, in ScrollPane.
- Auto fit JavaFX 2 ImageView
- Load image file as InputStream


Convert java.awt.image.BufferedImage to javafx.scene.image.Image

To convert image from java.awt.image.BufferedImage to javafx.scene.image.Image, SwingFXUtils.toFXImage() can be used.

Example:
            BufferedImage bufferedImage = ImageIO.read(file);
            Image image = SwingFXUtils.toFXImage(bufferedImage, null);
            myImageView.setImage(image);


Related:
- Get width and height of javafx.scene.image.Image


Saturday, January 5, 2013

Manually create GUI application of Hello World on NetBeans 7.2.1

Application wizard of Java Desktop Application missed in my current NetBeans 7.2.1 (refer to my old post "HelloNetBeans: Create Java Desktop Application using NetBeans IDE"). Is it anything wrong in my setting? Or because JSR-296 (Swing Application Framework) is no longer developed...? Anyway, this post describe how to create GUI application manually.

To start-up Swing (Not JavaFX) GUI Desktop Application in NetBeans, we have to create a Java Application with GUI container. In this example, we will create a dummy GUI Application with JFrame.

New a project, without main class:

- Select Category of Java, and Project Java Application.

- Change the Project Name to HelloGUI, make sure the box Create Main Class UN-checked.

- A start-up project will be generated.

Next, Add GUI container of JFrame:

- Right click on your project, and select New JFrame.

- Enter your Class Name of MyFrame and Package my.hellogui.

- A JFrame will be added.

Assign main class:

- Try to run your project, Run -> Run Project (F6).

- You will be complained your project does not have a main class set. Now you can select your main class (my.hellogui.MyJFrame) from the selection box.