0

I need to load and display some images at runtime in javafx, I managed to load one of them up, I create the ImageView passing that image as param but then the image doesn't show up. I discovered that the image is clickable (it recognizes on mouse events) but I can't display it

The controller

public class fx extends Application {
    @FXML
    // The reference of inputText will be injected by the FXML loader
    private TextField inputUsername;
    @FXML
    private TextField inputUrl;
    @FXML
    private ComboBox inputConnectionType;
    @FXML
    private ComboBox inputColour;
    @FXML
    private Button submit;
    @FXML
    private ImageView imageView;
    @FXML
    private AnchorPane anchor;


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

    @Override
    public void start(Stage stage) throws Exception {
        Image image = new Image("/AD_weapons_IT_0211.png");
        imageView = new ImageView(image);
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/form.fxml"));
        Scene scene = new Scene(root);
        stage.setWidth(415);
        stage.setHeight(200);
        stage.setScene(scene);
        stage.sizeToScene();
        stage.show();
    }

    public void submit()
    {
        System.out.println(inputUsername.getText()+"\n");
        System.out.println(inputColour.getValue().toString()+"\n");
        System.out.println(inputConnectionType.getValue().toString()+"\n");
        System.out.println(inputUrl.getText()+"\n");
    }

    public void press()
    {
        System.out.println("CLICKED");
    }
}

The fxml file

<?xml version="1.0" encoding="UTF-8"?>

<?language JavaScript?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.image.Image?>

<TitledPane animated="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" text="untitled" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.172-ea" fx:controller="provaProj.fx">
  <content>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
         <children>
            <ImageView fx:id = "imageView" fitHeight="150.0" fitWidth="200.0" layoutX="199.0" layoutY="112.0" onMousePressed="#press" pickOnBounds="true" preserveRatio="false" />
         </children></AnchorPane>
  </content>
</TitledPane>

1 Answers1

0

You should have 2 Classes and 1 fxml file for an FXML-Application:

Main Class:

public class TestApplication extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLTest.fxml"));
        stage.setTitle("Application");
        Scene scene = new Scene(root);
        stage.setWidth(415);
        stage.setHeight(200);
        stage.setScene(scene);
        stage.sizeToScene();
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

your fxml-File and a Controller-Class:

public class Controller {
    @FXML
    // The reference of inputText will be injected by the FXML loader
    private TextField inputUsername;
    @FXML
    private TextField inputUrl;
    @FXML
    private ComboBox inputConnectionType;
    @FXML
    private ComboBox inputColour;
    @FXML
    private Button submit;
    @FXML
    private ImageView imageView;
    @FXML
    private AnchorPane anchor;

    public void submit()
    {
        System.out.println(inputUsername.getText()+"\n");
        System.out.println(inputColour.getValue().toString()+"\n");
        System.out.println(inputConnectionType.getValue().toString()+"\n");
        System.out.println(inputUrl.getText()+"\n");
    }
@FXML
public void press()
{
    System.out.println("CLICKED");
    Image img = new Image("yourURL");
    imageView.setImage(img);
}

}

and you can then change your image in the Controller-Class..something like I made in the press method.

micpog90
  • 106
  • 8
  • Yeah, I tried the code and does the job. What if I want a method of the controller to receive an url as param and use that to set the ImageView, without pressing anything, as soon as I load the screen? I tried doing so but I get InvocationTargetException – Matteo Ferrini May 25 '19 at 15:38
  • 1
    @MatteoFerrini https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – fabian May 25 '19 at 15:45