简体   繁体   中英

why setImage() doesn't change my ImgView in JavaFx?

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;

public class ChooseScheme extends Application{

    GUIBoard board = new GUIBoard();
    Stage primaryStage;
    Integer totalScheme = 26;

//these are the imgs I want to change, in the fxml file they have their own path but with the method chooseRandomImgs(), 

    @FXML
    ImageView scheme1 = new ImageView();

    @FXML
    ImageView scheme2 = new ImageView();

    @FXML
    ImageView scheme3 = new ImageView();

    @FXML
    ImageView scheme4 = new ImageView();

    public void launchChooseScheme() throws Exception {

        start(new Stage());
    }

//standard method, it runs and open the scene

    @Override
    public void start(Stage primaryStage) throws Exception {

        this.primaryStage = primaryStage;
        Parent ChooseScheme;
        ChooseScheme = FXMLLoader.load(getClass().getClassLoader().getResource("GUIFiles/ChooseScheme.fxml"));

        Stage stage = new Stage();

        chooseRandomImgs();

        stage.setTitle("Choose Scheme");
        stage.setScene(new Scene(sgChooseScheme, 600, 400));
        stage.show();

    }

//this method should change my img

    private void chooseRandomImgs() {
        int countImages = totalScheme;
        int imageNumber = (int) (Math.random() * countImages);

        System.out.println(imageNumber);
        Image schemeImage = new Image("GUIfiles/imgs/schemecard/val5/"+ imageNumber + ".jpg");

        scheme1.setImage(schemeImage);
    }
}

The Img path is correct. When I run the class doesn't change the ImgView scheme1 ; instead it remains the Img I set on SceneBuilder, every time I run the code never shows the new Img. I don't understand why.

The instance of ChooseScheme is a different one than the one that is used with the fxml (assuming the fx:controller attribute is set to the appropriate value).

You always initialize the ImageView fields, but if the instance of ChooseScheme is not used with a fxml, the instances are never added to a scene. (In fact the instances created by the initializer are never added to a scene, but for the controller used with a fxml the initial values are replaced during load.)

It is never a good idea to initialize fields that are supposed to be injected from the fxml. This just fixes the NullPointerException that may be thrown.

Using the Application class as controller is not a good idea either.

Depending on where you want to determine the random images, you need to use one of the approaches presented here Passing Parameters JavaFX FXML .

Alternatively you could do this from the initialize method of the controller. This method is run during load after all objects have been injected.

Example

Controller class

package mypackage;

...

public ChooseSchemeController {

    private static final Random random = new Random();
    private static final int TOTAL_SCHEME = 26; // you don't want to use a wrapper type here

    @FXML
    private ImageView scheme1;

    @FXML
    private void initialize() {
        chooseRandomImage(scheme1);
    }

    private void chooseRandomImage(ImageView iv) {
        int imageNumber = random.nextInt(TOTAL_SCHEME);

        System.out.println(imageNumber);
        Image schemeImage = new Image("GUIfiles/imgs/schemecard/val5/"+ imageNumber + ".jpg");

        iv.setImage(schemeImage);
    }

}

GUIFiles/ChooseScheme.fxml

<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="mypackage.ChooseSchemeController">
    <children>
        <ImageView fx:id="scheme1"/>
    </children>
</VBox>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM