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.