The picture above shows a UI design that I wish to implement in my project.
As you can see, there are 2 similar rectangles with varying details in them. These details are from a custom object Bell
in my project. There is a List
of Bell
s that needs to have their details displayed like shown above.
My plan was to create a class called BellBox
that will represent the rectangles containing the details of a single Bell
. This class will have a method called build()
that will load the FXML file named "BellBoxFMXL.fxml". The FXML file contains an AnchorPane
as the root and has the necessary containers to represent all the information of a Bell
. The AnchorPane
will then be returned.
The BellBox
class:
public class BellBox {
protected final Bell bell ;
protected final int index ;
public BellBox(Bell bell, int index) {
this.bell = bell;
this.index = index;
}
public Pane build() throws IOException {
Pane pane = FXMLLoader.load(Objects.requireNonNull(this.getClass().getResource("/model/BellBoxFXML.fxml"))) ;
return pane ;
}
}
Now, for the controller class. The controller class of the FXML file is called BellBoxController
. Remember I said that the AnchorPane
will have containers to show the details of a Bell
object? Yeah, so I decided have the controller class to extend the BellBox
class. The reason was so that when the initialize()
method is called, the labels can be updated using the bell
attribute/property of the BellBox
class.
The BellBoxController
class:
public class BellBoxController extends BellBox implements Initializable {
public Label indexLbl ;
public BellBoxController(Bell bell, int index) {
super(bell, index) ;
System.out.println("called with " + index) ;
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
System.out.println("initializing labels and stuff...");
this.indexLbl.setText("#" + (this.index + 1));
}
}
However, when I compiled the program and tried to run it, it crashed with a LoadException
error.
javafx.fxml.LoadException:
/D:/.../model/BellBoxFXML.fxml:9
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:105)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:941)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:227)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:752)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at model.BellBox.build(BellBox.java:20)
at mainapp.Main.openHomeView(Main.java:126)
at mainapp.Main.start(Main.java:109)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.InstantiationException: model.BellBoxController
at java.base/java.lang.Class.newInstance(Class.java:571)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:936)
... 24 more
Caused by: java.lang.NoSuchMethodException: model.BellBoxController.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3350)
at java.base/java.lang.Class.newInstance(Class.java:556)
... 25 more
Process finished with exit code 0
I also noticed that both output statements in the constructor and initialize()
method were not displayed in the console. Could someone explain to me why this is and how I could go about to achieve what I hope to do? And point out what I'm doing wrong (if that's the case).