user3275944
user3275944

Reputation: 57

how to add icon in JFXPanel

Hello i am a bit new on JavaFx , i get a source code on oracle website and i want to add an icon in JFXPanel.How to insert an icon in the JFXPanel.The icon should be position. Here is the code:

public class Test {

    private static void initAndShowGUI() {
        // This method is invoked on the EDT thread
        JFrame frame = new JFrame("Swing and JavaFX");

        final JFXPanel fxPanel = new JFXPanel();
        JScrollPane pane = new JScrollPane(fxPanel,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);            

        frame.add(pane);
        frame.setSize(300, 200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                initFX(fxPanel);
            }
       });
    }

    private static void initFX(JFXPanel fxPanel) {
        // This method is invoked on the JavaFX thread
        Scene scene = createScene();
        fxPanel.setScene(scene);
    }

    private static Scene createScene() {
        ImageIcon icon=new ImageIcon ( "icon/filter.jpg" );
        // add icon here
        return (scene);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                initAndShowGUI();
            }
        });
    }
}

Upvotes: 1

Views: 567

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208944

Instead of ImageIcon use javafx.scene.image.Image and add the Image to an ImageView, which is a Node you can add to a layout.

Image icon = new Image("stackoverflow5.png", 300, 300, false, false);
StackPane pane = new StackPane();
pane.getChildren().add(new ImageView(icon));
Scene scene = new Scene(pane, 300, 300);

See Image and ImageView


Notes from the Image class about file location

// The image is located in default package of the classpath
Image image1 = new Image("/flower.png", true);

// The image is located in my.res package of the classpath
Image image2 = new Image("my/res/flower.png", 100, 150, false, false);

// The image is downloaded from the supplied URL through http protocol
Image image3 = new Image("http://sample.com/res/flower.png", 100, 0, false, false);

// The image is located in the current working directory
Image image4 = new Image("file:flower.png", 0, 100, false, false);

Here's a simple example

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class StackJavaFx extends Application {

    @Override
    public void start(Stage primaryStage) {

        Image icon = new Image("stackoverflow5.png", 300, 300, false, false);
        StackPane pane = new StackPane();
        pane.getChildren().add(new ImageView(icon));
        Scene scene = new Scene(pane, 300, 300);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

enter image description here

Upvotes: 2

Related Questions