How can I show an image using the ImageView component in javafx and fxml?

前端 未结 7 1931
感情败类
感情败类 2020-12-03 14:21

I suppose it\'s a very simple thing but I just can\'t get behind it. All I want is to show an image over an ImageView linked to fxml. Here is my code:

packag         


        
相关标签:
7条回答
  • 2020-12-03 15:02

    You don't need an initializer, unless you're dynamically loading a different image each time. I think doing as much as possible in fxml is more organized. Here is an fxml file that will do what you need.

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import java.lang.*?>
    <?import javafx.scene.image.*?>
    <?import javafx.scene.layout.*?>
    
    <AnchorPane
        xmlns:fx="http://javafx.co/fxml/1"
        xmlns="http://javafx.com/javafx/2.2"
        fx:controller="application.SampleController"
        prefHeight="316.0"
        prefWidth="321.0"
        >
        <children>
            <ImageView
                    fx:id="imageView"
                    fitHeight="150.0"
                    fitWidth="200.0"
                    layoutX="61.0"
                    layoutY="83.0"
                    pickOnBounds="true"
                    preserveRatio="true"
                >
                <image>
                    <Image
                        url="src/Box13.jpg"
                        backgroundLoading="true"
                        />
                </image>
            </ImageView>
        </children>
    </AnchorPane>
    

    Specifying the backgroundLoading property in the Image tag is optional, it defaults to false. It's best to set backgroundLoading true when it takes a moment or longer to load the image, that way a placeholder will be used until the image loads, and the program wont freeze while loading.

    0 讨论(0)
  • 2020-12-03 15:04

    Please find below example to load image using JavaFX.

        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 LoadImage extends Application {
    
        public static void main(String[] args) {
        Application.launch(args);
        }
        @Override
        public void start(Stage primaryStage) {
        primaryStage.setTitle("Load Image");
    
        StackPane sp = new StackPane();
        Image img = new Image("javafx.jpg");
        ImageView imgView = new ImageView(img);
        sp.getChildren().add(imgView);
    
        //Adding HBox to the scene
        Scene scene = new Scene(sp);
        primaryStage.setScene(scene);
        primaryStage.show();
        }
    
      }
    

    Create one source folder with name Image in your project and add your image to that folder otherwise you can directly load image from external URL like following.

    Image img = new Image("http://mikecann.co.uk/wp-content/uploads/2009/12/javafx_logo_color_1.jpg");

    0 讨论(0)
  • 2020-12-03 15:08

    If you want to use FXML, you should separate the controller (like you were doing with the SampleController). Then your fx:controller in your FXML should point to that.

    Probably you are missing the initialize method in your controller, which is part of the Initializable interface. This method is called after the FXML is loaded, so I recommend you to set your image there.

    Your SampleController class must be something like this:

    public class SampleController implements Initializable {
    
        @FXML
        private ImageView imageView;
    
        @Override
        public void initialize(URL location, ResourceBundle resources) {
            File file = new File("src/Box13.jpg");
            Image image = new Image(file.toURI().toString());
            imageView.setImage(image);
        }
    }
    

    I tested here and it's working.

    0 讨论(0)
  • 2020-12-03 15:14

    src/sample/images/shopp.png

    **
        Parent root =new StackPane();
        ImageView ımageView=new ImageView(new Image(getClass().getResourceAsStream("images/shopp.png")));
        ((StackPane) root).getChildren().add(ımageView);
    
    **
    
    0 讨论(0)
  • 2020-12-03 15:18

    It's recommended to put the image to the resources, than you can use it like this:

    imageView = new ImageView("/gui.img/img.jpg");
    
    0 讨论(0)
  • 2020-12-03 15:24
    @FXML
    ImageView image;
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
      image.setImage(new Image ("/about.jpg"));
    }
    
    0 讨论(0)
提交回复
热议问题