len*_*len 0 binding javafx tableview invocationtargetexception unsupportedoperation
UnsupportedOperationExceptionBindings.bindContent()将对象绑定到时抛出TableView。为什么?如何解决这个问题?
我正在使用Java 8 update181。
Exception in Application start method java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
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$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:161)
at java.util.AbstractList$Itr.remove(AbstractList.java:374)
at java.util.AbstractList.removeRange(AbstractList.java:571)
at java.util.AbstractList.clear(AbstractList.java:234)
at com.sun.javafx.binding.ContentBinding.bind(ContentBinding.java:55)
at javafx.beans.binding.Bindings.bindContent(Bindings.java:1020)
at problem_bind.Main.start(Main.java:42)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
... 1 more Exception running application problem_bind.Main
Run Code Online (Sandbox Code Playgroud)
我的代码:
package problem_bind;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
House house = new House();
TableView<Person> table = new TableView<>();
table.setEditable(true);
TableColumn<Person, String> firstNameColumn = createColumn("First Name", "firstName");
TableColumn<Person, String> lastNameColumn = createColumn("Last Name", "lastName");
table.getColumns().add(firstNameColumn);
table.getColumns().add(lastNameColumn);
ObservableList<Person> data = FXCollections.observableArrayList();
data.addAll(house.getPersons());
table.setItems(data);
Bindings.bindContent(house.getPersons(), table.getItems());
BorderPane root = new BorderPane(table, null, null, null, null);
root.setPadding(new Insets(10));
primaryStage.setScene(new Scene(root, 600, 600));
primaryStage.show();
}
private TableColumn<Person, String> createColumn(String title, String property) {
TableColumn<Person, String> col = new TableColumn<>(title);
col.setSortable(false);
col.setCellValueFactory(
new PropertyValueFactory<Person, String>(property));
col.setCellFactory(TextFieldTableCell.forTableColumn());
return col ;
}
public static class House {
private List<Person> persons = new ArrayList<Person>();
public House() {
this.persons = Arrays.asList(
new Person("Jacob", "Smith", this),
new Person("Isabella", "Johnson", this),
new Person("Ethan", "Williams", this),
new Person("Emma", "Jones", this),
new Person("Michael", "Brown", this));
}
public List<Person> getPersons() {
return persons;
}
}
public static class Person {
private String firstName ;
private String lastName ;
private House house;
public Person(String firstName, String lastName, House house) {
this.firstName = firstName ;
this.lastName = lastName ;
this.house = house;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是您对的使用Arrays.asList(Object...)。该方法:
返回由指定数组支持的固定大小的列表。
因此,返回的List不支持诸如add或的操作remove。相反,您可以通过set方法修改此类列表。前两种方法固有地需要一个可变大小的列表。Bindings.bindContent显然,该实现会尝试在同步列表时使用那些不受支持的方法。
更改此:
this.persons = Arrays.asList(
new Person("Jacob", "Smith", this),
new Person("Isabella", "Johnson", this),
new Person("Ethan", "Williams", this),
new Person("Emma", "Jones", this),
new Person("Michael", "Brown", this));
Run Code Online (Sandbox Code Playgroud)
对此:
persons = new ArrayList<>();
persons.add(new Person("Jacob", "Smith", this));
persons.add(new Person("Isabella", "Johnson", this));
persons.add(new Person("Ethan", "Williams", this));
persons.add(new Person("Emma", "Jones", this));
persons.add(new Person("Michael", "Brown", this));
Run Code Online (Sandbox Code Playgroud)
注意:在您当前的代码中,this不需要带前缀。但是,如果愿意,您仍然可以使用它。
注意#2:请参阅@Pagbo的注释。
基本上,请确保使用List支持add和remove操作的。
您可能想知道为什么通话Bindings.bindContent(List,ObservableList)首先导致remove通话。这是因为该方法返回:
内容绑定[确保]
List包含与相同的元素ObservableList。如果ObservableList更改的内容,List将自动更新。
换句话说,List会更新以匹配ObservableList。这涉及到,当首先创建绑定时,先清除,List然后再添加的所有元素ObservableList。
| 归档时间: |
|
| 查看次数: |
68 次 |
| 最近记录: |