I'm new to JavaFX, so please forgive the noobishness.
So I'm making a Picture Viewer. This is my first time designing a GUI with the MVC paradigm, so I'm not very sure what I'm doing.
It's just a normal picture viewer. Like your standard Windows program that opens when you want to look at a picture.
So I currently have 2 views:
the Root.fxml view, which is a BorderPane with just a MenuBar on top with a MenuItem "Open" that triggers a FileChooser and sets the MainApp.java currentImage variable to whatever the user chose.
Nested inside of (root.setCenter) it I have ImageViewer.fxml view, which is an AnchorPane that only contains an ImageView.
I'm 100% sure there is nothing wrong with my FXML and that all controllers and variables are bound correctly. This must be an issue in the MainApp or the Controllers.
I have no idea how to pass the Image from the RootViewController to the ImageViewerController. Whenver I try to initialize a listener for mainApp.currentImage in the ImageViewerController initializer I get a NullPointerException. If I put it in the setMainApp method it does nothing. Here's my code:
MainApp.java
import sample.model.MyImage;
import samlple.view.ImageViewerController;
import sample.view.RootController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.File;
import java.io.IOException;
public class MainApp extends Application {
final static File img = new File("C:\\test\\1.png"); // test initial image loading
private Stage primaryStage;
private BorderPane rootLayout;
private MyImage currentImage = new MyImage(img);
public MainApp() {
}
public MyImage getCurrentImage(){
return this.currentImage;
}
public void setCurrentImage(MyImage myImage){
currentImage = myImage;
}
public void start(Stage primaryStage) throws Exception{
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Hi");
initRootLayout();
showImageViewer();
}
/**
* Initializes the root layout.
*/
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/Root.fxml"));
rootLayout = loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
// Giving the controller access to the main app.
RootController controller = loader.getController();
controller.setMainApp(this);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
System.err.println("Root layout loading error.");
}
}
/**
* Shows the ImageViewer inside the root layout.
*/
public void showImageViewer() {
try {
// Load ImageViewer.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/ImageViewer.fxml"));
AnchorPane imageViewer = loader.load();
// Set the ImageViewer into the center of root layout.
rootLayout.setCenter(imageViewer);
// Giving the controller access to the main app.
ImageViewerController controller = loader.getController();
controller.setMainApp(this);
} catch (IOException e) {
e.printStackTrace();
System.err.println("ImageViewer layout loading error.");
}
}
/**
* Called when user clicks the "Open" menu item
*/
public void menuOpenImageFile() {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Image types (*.jpg; *.gif; *.bmp; *.png)", "*.jpg", "*.png", "*.bmp", "*.gif");
fileChooser.getExtensionFilters().add(extFilter);
//Show save file dialog
File imageFile = fileChooser.showOpenDialog(primaryStage);
MyImage myImageToSetAsCurrent = new MyImage(imageFile);
setCurrentImage(myImageToSetAsCurrent);
}
/**
* Returns the main stage.
* @return
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) { launch(args); }
}
MyImage.java (a model image class to hold properties):
package sample.model;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.image.Image;
import java.io.File;
public class MyImage {
private final ObjectProperty<File> imageFile;
private final ObjectProperty<Image> image;
public MyImage(){
this(null);
}
public MyImage(File imageFile) {
this.imageFile = new SimpleObjectProperty<>(imageFile);
this.image = new SimpleObjectProperty<>(new Image("file:" + imageFile.toString()));
}
public File getImageFile() {
return imageFile.get();
}
public ObjectProperty<File> imageFileProperty() {
return imageFile;
}
public void setImageFile(File myImageFile) {
this.imageFile.set(myImageFile);
}
public Image getImage() {
return image.get();
}
public ObjectProperty<Image> imageProperty() {
return image;
}
public void setImage(Image myImage) {
this.image.set(myImage);
}
}
RootController.java
import samlple.MainApp;
import javafx.fxml.FXML;
import javafx.scene.control.MenuItem;
public class RootController {
@FXML
private MenuItem open;
// Reference to the main application.
private MainApp mainApp;
/**
* The constructor.
* The constructor is called before the initialize() method.
*/
public RootController() {
}
/**
* Initializes the controller class. This method is automatically called
* after the fxml file has been loaded.
*/
@FXML
private void initialize() {
}
@FXML
private void openImageFile () {
mainApp.menuOpenImageFile();
}
/**
* Is called by the main application to give a reference back to itself.
*
*/
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
}
}
ImageViewerController.java
import sample.MainApp;
import javafx.fxml.FXML;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class ImageViewerController {
@FXML
private ImageView imageView;
// Reference to the main application.
private MainApp mainApp;
/**
* The constructor.
* The constructor is called before the initialize() method.
*/
public ImageViewerController() {
}
/**
* Initializes the controller class. This method is automatically called
* after the fxml file has been loaded.
*/
@FXML
private void initialize() {
mainApp.getCurrentImage().imageProperty().addListener(((observable, oldValue, newValue) -> imageView.setImage(newValue))); //This keeps throwing NullPointerException!
}
/**
* Is called by the main application to give a reference back to itself.
*
*/
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
imageView.setImage(mainApp.getCurrentImage().imageProperty().get());
}
}
When you initialize everything, the ImageViewController
is observing the imageProperty
of the main app's current MyImage
object. When you choose a new image from your menu, you set a new MyImage
object in the main app: however the ImageViewController
is still observing the imageProperty
in the original MyImage
object. That imageProperty
never changes.
You probably want to make currentImage
final, and just change the property:
public class MainApp extends Application {
final static File img = new File("C:\\test\\1.png"); // test initial image loading
private Stage primaryStage;
private BorderPane rootLayout;
private final MyImage currentImage = new MyImage(img);
public MainApp() {
}
public MyImage getCurrentImage(){
return this.currentImage;
}
// public void setCurrentImage(MyImage myImage){
// currentImage = myImage;
// }
// ...
public void menuOpenImageFile() {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Image types (*.jpg; *.gif; *.bmp; *.png)", "*.jpg", "*.png", "*.bmp", "*.gif");
fileChooser.getExtensionFilters().add(extFilter);
//Show save file dialog
File imageFile = fileChooser.showOpenDialog(primaryStage);
currentImage.setImageFile(imageFile);
currentImage.setImage(new Image(imageFile.toURI().toString()));
}
}
Note you have another bug: you are calling mainApp.getCurrentImage()
in the initialize()
method of your controller, which is necessarily called before you get a chance to set the main app. You should register the listener in the setMainApp(...)
method:
import sample.MainApp;
import javafx.fxml.FXML;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class ImageViewerController {
@FXML
private ImageView imageView;
// Reference to the main application.
private MainApp mainApp;
/**
* Is called by the main application to give a reference back to itself.
*
*/
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
imageView.setImage(mainApp.getCurrentImage().imageProperty().get());
mainApp.getCurrentImage().imageProperty().addListener(((observable, oldValue, newValue) -> imageView.setImage(newValue)));
}
}