Brian Smith Brian Smith - 1 month ago 7
Java Question

Selecting an object from a ListView in JavaFX

I have a ListView that contains "Task "objects. I want to be able to select a task from the ListView and get the values of that specific object. I am trying to do this by calling a method (startTask()) that gets the selected item in the ListView (a task object) and gets its values by using the getter methods in the task object. For the moment i am just printing the object to the console.

When I run the application this is the error that I get.

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
/C:/Users/Brian.Nora-PC/workspace/TaskApp/bin/application/taskapp.fxml:63

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:20)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Caused by: java.lang.IllegalArgumentException: Unable to coerce #startTask() to interface javafx.event.EventHandler.
at com.sun.javafx.fxml.BeanAdapter.coerce(BeanAdapter.java:496)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:258)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:54)
at javafx.fxml.FXMLLoader$Element.applyProperty(FXMLLoader.java:512)
at javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:363)
at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:325)
at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:235)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:767)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
... 17 more
Exception running application application.Main


Here is the fxml file that contains the ListView and Button with startTask() method call when the button is clicked

<Tab text="Tasks">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Label fx:id="noOfTasks" layoutX="403.0" layoutY="294.0" prefHeight="17.0" prefWidth="190.0" text="You have 0 tasks to be completed" />
<ListView fx:id="taskList" layoutX="8.0" layoutY="14.0" prefHeight="274.0" prefWidth="585.0" />
<Button layoutX="8.0" layoutY="299.0" mnemonicParsing="false" text="Start Task" OnAction="#startTask()"/>
</children>

</AnchorPane>
</content>
</Tab>


Here is the startTask() method

public void startTask(){
Task taskToStart = taskList.getSelectionModel().getSelectedItem();
System.out.println(taskToStart);



}


Here is my initialisation of ListView in Controller.java

public ListView<Task> taskList = new ListView<Task>();


Any help would be much appreciated thanks.

Answer

Specifying controller methods as event handlers requires you to use a # followed by the method's name and nothing else (see also Introduction to FXML - Controller Method Event Handlers ). Remove the () from the onAction attribute:

<Button layoutX="8.0" layoutY="299.0" mnemonicParsing="false" text="Start Task" onAction="#startTask"/>

Furthermore there should be no need to initialize the ListView using a constructor call in the code, if you inject it from the fxml; Furthermore a ListView created using a constructor call won't insert itself to the scene. It's likely that you just create a second ListView that is never shown. Instead there should be a ListView field with the name taskList in the controller class; this field should not be initialized from java code:

@FXML
private ListView<Task> taskList;
Comments