Tableview ChangeListener冲突 [英] Tableview ChangeListener Conflict

查看:56
本文介绍了Tableview ChangeListener冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个相邻的TableView(table1和table2)

I have two TableView (table1 and table2) one next to the other

我需要做的是:当您在表1中选择一个项目时,在表2中选择了相应的项目到目前为止,一切都很容易,但是我需要在表2中重现相同的效果,此时导致 NPE 应用于表1的侦听器与表2的侦听器冲突.我试图在 focusedProperty()中创建一个事件,但是没有成功:(我制作了一个测试应用程序发布在这里,因为它不适合下面的所有代码下载链接 TableView-Teste .

What I need to do is: When you select an item in table1 the corresponding item is selected in table2 So far so good was easy, but I need to reproduce the same effect in table2, and it is when arises the NPE the listener applied in table1 conflict with the listener of table2. I tried to create an event in focusedProperty () but without success :( I made a test application to post here, as it would not fit all code follows download link TableView - Teste.

推荐答案

这似乎是一个错误,但是我没有时间进行适当的试验.一种解决方法似乎是更新 Platform.runLater()内其他"表中的选择.您需要注意不要通过检查选择是否确实不同来创建无限数量的这些调用:

This feels like it might be a bug, but I don't have time to experiment with it properly. A workaround seems to be to update the selection in the "other" table inside a Platform.runLater(). You need to be careful not to create an infinite number of these calls by checking the selection really is different:

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

    }
}

这篇关于Tableview ChangeListener冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆