user2556079
user2556079

Reputation: 644

BufferedImage on StackPane in JavaFX

I have a BufferedImage and I want to show in a stackpane, because I'm working in JavaFX application.I was in the same situation few day ago, but I was working in Java, in that case I did like this:

public static  void VisImmagineDaPc(JFrame frame, BufferedImage image) throws Throwable
             {
                    if (label==null){
                    label = new JLabel(new ImageIcon(image));


                    frame.getContentPane().add(label, BorderLayout.AFTER_LAST_LINE);
                    // frame.setSize(10, 10);
                    int larghezza = frame.getWidth();
                    int altezza = frame.getHeight();

//                  frame.setSize(larghezza, altezza); 
                    frame.setSize(larghezza, altezza );
                    frame.setResizable(false);  
                    frame.setVisible(true);
        }

...

The Method continue with other code, but it's not important at the moment. So, in Java I create a Jlabel with the image and then I add to the Jframe. What do I have to do in JavaFX in order to show the picture in a stackpane? I tried content.getChildren().addAll(image); where content is a stackpane, but it doesn't work.

Thanks in advance and I apologize for the simplicity of the question.

Upvotes: 0

Views: 2220

Answers (1)

Loonger
Loonger

Reputation: 110

After converting the BufferedImage to a Javafx Image you have to add a Nodeto the StackPane and the Image isn´t a Node so you can construct an ImageView with the Image.

@FXML StackPane s;

@FXML void initialize(){

    BufferedImage b = ImageIO.read(file);
    Image i = SwingFXUtils.toFXImage(b, null);

    ImageView v = new ImageView(i);
    s.getChildren().add(v);
}

Upvotes: 2

Related Questions