0

I need help to program a windows based application with JavaFX. I simply want to show or hide some images with if/else condition. what I exactly want is something like this: if x=1 then show image1 if x=2 them show image2

I tried setvisible() and setDisable() methodes to do this, but it doesn't work.

public class BoardSelection implements Initializable {
    @FXML
    public ImageView image1= new ImageView();
    @FXML
    public ImageView image2 = new ImageView();

    public int batteryVariation(int x){

        if(x=1){
            image1.setVisible(true);
            image2.setVisible(false);
        }
        if(x=2){
            image2.setVisible(false);
            image2.setVisible(true);
        }
        return x;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

and my .fxml file codes are:

<ImageView fx:id="image1" fitHeight="60.0" fitWidth="30.0" layoutX="118.0" layoutY="227.0">
      <image>
        <Image url="@../img/image1.png" />
      </image>
    </ImageView>
    <ImageView fx:id="image2" fitHeight="60.0" fitWidth="30.0" layoutX="191.0" layoutY="227.0" pickOnBounds="true" preserveRatio="true">
      <image>
        <Image url="@../img/image2.png" />
      </image>
    </ImageView>
  • 6
    1. Please post [mre]. 2. Before trying to hide the image you need to get it displayed. I doubt if it does. `image1` is constructed by the FXML but then overwritten with `image1= new ImageView();`. The same applies to `image2` – c0der Sep 09 '21 at 10:51
  • 2
    Post a [mre], using the actual code you ran. This doesn’t even compile. (`if (x=1)`, for example). – James_D Sep 09 '21 at 10:57
  • 3
    ", but it doesn't work." What doesn't work about it? Do the images not show? Does the program not compile? How are you calling `batteryVariation`? – matt Sep 09 '21 at 11:11
  • 4
    **Never** set an `@FXML` variable to a new value, those values are injected by the loader and should never be changed or re-initialized by your application code. – jewelsea Sep 09 '21 at 11:48
  • 1
    Once you get it working, you will probably run into: [setVisible hides the element but doesn't rearrange adjacent nodes](https://stackoverflow.com/questions/28558165/javafx-setvisible-hides-the-element-but-doesnt-rearrange-adjacent-nodes) – jewelsea Sep 09 '21 at 11:50

1 Answers1

2

In the if statement you used the assignment operator (=) instead of comparison operator == .

public int batteryVariation(int x) {
    if (x == 1) {
        image1.setVisible(true);
        image2.setVisible(false);
    }
    if (x == 2) {
        image1.setVisible(false);
        image2.setVisible(true);
    }
    return x;
}

Example of working code:

public class PicturesTestController implements Initializable {

    @FXML
    private TextField tf_testInput;

    @FXML
    private ImageView image1;

    @FXML
    private ImageView image2;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        image2.setVisible(false);
        tf_testInput.setOnAction(actionEvent -> batteryVariation(Integer.parseInt(tf_testInput.getText())));
    }

    public int batteryVariation(int x) {
        if (x == 1) {
            image1.setVisible(true);
            image2.setVisible(false);
        }
        if (x == 2) {
            image1.setVisible(false);
            image2.setVisible(true);
        }
        return x;
    }
}

Anyway, is better use the setImage() method to set/change pictures in a ImageView.

https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/ImageView.html

Marco
  • 36
  • 4