首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tableview ChangeListener冲突

Tableview ChangeListener冲突
EN

Stack Overflow用户
提问于 2014-08-12 14:10:39
回答 1查看 167关注 0票数 0

我有两个TableView (table1和table2),一个在另一个旁边

我需要做的是:当您在table1中选择一个项目时,在table2中选择了相应的项,到目前为止,这是很容易的,但是我需要在table2中再现同样的效果,当出现NPE时,应用在table1中的侦听器与table2的侦听器发生冲突。我试图在focusedProperty ()中创建一个事件,但没有成功:(我在这里创建了一个测试应用程序,因为它不适合下载链接TableView -泰斯特中的所有代码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-12 14:59:28

这感觉可能是个bug,但我没有时间对它进行适当的实验。解决方法似乎是更新Platform.runLater()中“其他”表中的选择。您需要小心,不要通过检查所选内容的不同来创建无限数量的这些调用:

代码语言:javascript
复制
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class TwoTableTest extends Application {

    private ChangeListener<Number> table1SelectionListener ;
    private ChangeListener<Number> table2SelectionListener ;

    @Override
    public void start(Stage primaryStage) {
        TableView<Person> table1 = createTableView() ;
        TableView<Person> table2 = createTableView() ;

        table1.getSelectionModel().select(0);
        table2.getSelectionModel().select(0);

        table1SelectionListener = (obs, oldIndex, newIndex) -> {
            int table1SelectedIndex = table1.getSelectionModel().getSelectedIndex() ;
            int table2SelectedIndex = table2.getSelectionModel().getSelectedIndex() ;
            if (table1SelectedIndex != table2SelectedIndex) {
                Platform.runLater(() -> table2.getSelectionModel().select(table1SelectedIndex));
            }
        };

        table2SelectionListener = (obs, oldIndex, newIndex) -> {
            int table1SelectedIndex = table1.getSelectionModel().getSelectedIndex() ;
            int table2SelectedIndex = table2.getSelectionModel().getSelectedIndex() ;
            if (table1SelectedIndex != table2SelectedIndex) {
                Platform.runLater(() -> table1.getSelectionModel().select(table2SelectedIndex));
            }
        };
        table1.getSelectionModel().selectedIndexProperty().addListener(table1SelectionListener);
        table2.getSelectionModel().selectedIndexProperty().addListener(table2SelectionListener);

        HBox root = new HBox(5, table1, table2);
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private TableView<Person> createTableView() {
        TableView<Person> table = new TableView<>();
        TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
        firstNameCol.setCellValueFactory(data -> data.getValue().firstNameProperty());

        TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
        lastNameCol.setCellValueFactory(data -> data.getValue().lastNameProperty());

        TableColumn<Person, String> emailCol = new TableColumn<>("Email");
        emailCol.setCellValueFactory(data -> data.getValue().emailProperty());

        table.getColumns().addAll(firstNameCol, lastNameCol);

        table.getItems().addAll(
                new Person("Jacob", "Smith", "jacob.smith@example.com"),
                new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                new Person("Ethan", "Williams", "ethan.williams@example.com"),
                new Person("Emma", "Jones", "emma.jones@example.com"),
                new Person("Michael", "Brown", "michael.brown@example.com")        
        );

        return table ;
    }

    public static void main(String[] args) {
        launch(args);
    }

    public static class Person {
        private final StringProperty firstName;
        private final StringProperty lastName;
        private final StringProperty email ;


        Person(String firstName, String lastName, String email) {
            this.firstName = new SimpleStringProperty(this, "firstName",
                    firstName);
            this.lastName = new SimpleStringProperty(this, "lastName", lastName);
            this.email = new SimpleStringProperty(this, "email", email);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String firstName) {
            this.firstName.set(firstName);
        }

        public StringProperty firstNameProperty() {
            return firstName;
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String lastName) {
            this.lastName.set(lastName);
        }

        public StringProperty lastNameProperty() {
            return lastName;
        }

        public String getEmail() {
            return email.get();
        }

        public void setEmail(String email) {
            this.email.set(email);
        }

        public StringProperty emailProperty() {
            return email ;
        }

        @Override
        public String toString() {
            return firstName.get() + " " + lastName.get();
        }

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25266520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档