How to display an image chosen with filechooser Javafx FXML

1k views Asked by At

I have gotten the program to allow me to browse for a file to select using the file chooser. You will see the method for this in my code below. My problem is that I do not know what to do next. I want to assign the file chosen as an image file. I then want to display the image(only image files will be used in this program).

Since I am using scene builder, which contains the actual application(Paint) class as well as the FXML controller class, I believe I will have to add code to both classes. I have attached both for clarity sake.

public class FXMLDocumentController implements Initializable {


@FXML
private MenuItem open;
private MenuItem exit;
private AnchorPane pane;
private ImageView imgv;
private Stage primaryStage;


@FXML
private void handleOpenAction() {

    FileChooser fileChooser = new FileChooser();
    FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
    FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
    File file = fileChooser.showOpenDialog(primaryStage);


}
@FXML
private void handleExitAction(){
            System.exit(0);

}


@Override
public void initialize(URL url, ResourceBundle rb) {

}    



}


public class Paint extends Application {

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    Scene scene = new Scene(root, 400, 600);
    stage.setTitle("Paint");
    stage.setScene(scene);
    stage.show();


    }

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

I don't mind loading the image into the default program pane, however I must be able to "draw" on the image later on down the road.

  1. How do I get/set the image selected via filechooser?
    • Do I do this inside the same FXML method or make a separate method?
  2. How do I display the image?
    • Should I be creating the pane that the image will load into manually inside the FXML class or inside the Paint class?
  3. Is there a way to create a (choose, get, set) method that I could add to the Paint class that can be referenced from inside the FXML class?

  4. I am aware that I must assign the scene builder elements to the fxml handles/methods for them to function properly.

0

There are 0 answers