0

UnsupportedOperationException将对象绑定Bindings.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

我的代码:

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

     }
}
4

1 回答 1

5

问题是您使用Arrays.asList(Object...). 那个方法:

返回由指定数组支持的固定大小的列表。

因此,返回的List不支持 or 等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));

对此:

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

注意:在您当前的代码中,前缀 withthis是不必要的。但是,如果您愿意,您仍然可以使用它。

注意#2:见@Pagbo 的评论

基本上,请确保您使用List支持addandremove操作的 a。


您可能想知道为什么呼叫Bindings.bindContent(List,ObservableList)首先会导致remove呼叫。这是因为该方法返回:

内容绑定 [确保]List包含与 . 相同的元素ObservableList。如果内容发生ObservableList变化,List将自动更新。

换句话说,List被更新以匹配ObservableList。这涉及到,当第一次创建绑定时,清除List然后将所有元素添加ObservableList到它。

于 2019-03-13T03:36:27.857 回答