Thursday, April 4, 2013

Display multi images in JavaFX TitledPanes

Last post demonstrate simple example of "JavaFX TitledPane". This post show how to load multi images in TitledPane within Accordion.

Display multi images in JavaFX 2 TitledPanes
Display multi images in JavaFX 2 TitledPanes


package javafx_titledpane;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
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.Group;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Button;
import javafx.scene.control.TitledPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_TitledPane extends Application {
    
    File filesJpg[];
    Image images[];
    ImageView imageViews[];
    BufferedImage bufferedImage[];
    TitledPane titledPanes[];
    
    @Override
    public void start(final Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Open a New Window");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                openDirectoryChooser(primaryStage);
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
    private void openDirectoryChooser(Stage parent) {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        File selectedDirectory =
                directoryChooser.showDialog(parent);

        if (selectedDirectory != null) {
            FilenameFilter filterJpg = new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return name.toLowerCase().endsWith(".jpg");
                }
            };

            filesJpg = selectedDirectory.listFiles(filterJpg);
            openTitledPane();
        }
    }
    
    private void openTitledPane(){
        int numOfJpg = filesJpg.length;
        images = new Image[numOfJpg];
        bufferedImage = new BufferedImage[numOfJpg];
        imageViews = new ImageView[numOfJpg];
        titledPanes = new TitledPane[numOfJpg];

        for (int i = 0; i < numOfJpg; i++) { 
            try {
                File file = filesJpg[i];
                
                bufferedImage[i] = ImageIO.read(file);
                images[i] = SwingFXUtils.toFXImage(bufferedImage[i], null);
                imageViews[i] = new ImageView();
                imageViews[i].setImage(images[i]);
                imageViews[i].setFitWidth(400);
                imageViews[i].setPreserveRatio(true);
                imageViews[i].setSmooth(true);
                imageViews[i].setCache(true); 
                titledPanes[i] = new TitledPane(String.valueOf(i), imageViews[i]);
            } catch (IOException ex) {
                Logger.getLogger(JavaFX_TitledPane.class.getName()).log(Level.SEVERE, null, ex);
            }
        } 
        
        Accordion accordion = new Accordion();
        accordion.getPanes().addAll(titledPanes);
        
        Stage titledPaneStage = new Stage();
        titledPaneStage.setTitle("TitledPane");
        Scene scene = new Scene(new Group(), 400, 400);
        Group root = (Group)scene.getRoot();
        root.getChildren().add(accordion);
        titledPaneStage.setScene(scene);
        titledPaneStage.show();
    }
}




No comments:

Post a Comment

Post a Comment