从JavaFX中的ListView中选择一个对象

时间:2016-10-27 14:20:23

标签: java javafx fxml

我有一个包含"任务"对象的ListView。我希望能够从ListView中选择一个任务并获取该特定对象的值。我试图通过调用一个方法(startTask())来获取ListView(一个任务对象)中的选定项,并通过使用任务对象中的getter方法获取其值。目前我只是将对象打印到控制台。

当我运行应用程序时,这是我得到的错误。

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

这是包含ListView和Button的fxml文件,当单击按钮时调用startTask()方法

<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>

这是startTask()方法

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



}

这是我在Controller.java中初始化ListView

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

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

将控制器方法指定为事件处理程序要求您使用#,后跟方法的名称,而不是其他(另请参阅Introduction to FXML - Controller Method Event Handlers)。从()属性中删除onAction

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

此外,如果从fxml注入,则不需要在代码中使用构造函数调用初始化ListView;此外,使用构造函数调用创建的ListView不会将自身插入到场景中。您可能只是创建了一个从未显示的第二个ListView。相反,控制器类中应该有一个名为ListView的{​​{1}}字段;不应该从java代码初始化该字段:

taskList

答案 1 :(得分:0)

你可以试试这样的......

ObservableList<Node> list=taskList.getChildren();
for(int i=0;i<list.size();i++) {   
    Task task=list.get(i);
}