0

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.

user2556079
  • 624
  • 3
  • 9
  • 23
  • 1
    Start with [Convert java.awt.image.BufferedImage to javafx.scene.image.Image](http://java-buddy.blogspot.com.au/2013/01/convert-javaawtimagebufferedimage-to.html), [JavaFX Live View](http://blog.ngopal.com.np/2012/09/10/javafx-liveview/) and [SwingFXUtils](https://docs.oracle.com/javafx/2/api/javafx/embed/swing/SwingFXUtils.html) to convert a `BufferedImaege` to a `javafx.scene.image.Image` – MadProgrammer Oct 13 '15 at 22:38
  • Then have a look at [How can I show an image using the ImageView component in javafx and fxml?](http://stackoverflow.com/questions/22710053/how-can-i-show-an-image-using-the-imageview-component-in-javafx-and-fxml), [How to Load Image](http://www.javacodegeeks.com/2013/10/javafx-2-how-to-load-image.html) and [`javafx.scene.image.ImageView`](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/ImageView.html) – MadProgrammer Oct 13 '15 at 22:39
  • If you need more info on JavaFX (8), take a look at [the tutorials](http://docs.oracle.com/javase/8/javase-clienttechnologies.htm) – MadProgrammer Oct 13 '15 at 22:40

1 Answers1

2

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);
}
Loonger
  • 110
  • 1
  • 10
  • Depending on where your initial image is coming from, of course, you might be able to construct a `javafx.scene.image.Image` directly, instead of first creating a `BufferedImage` and then converting it. – James_D Oct 14 '15 at 00:36