Tableview删除changeListener

时间:2014-07-29 14:06:03

标签: java css javafx

早上好 我的桌子有问题。 我有两个tableview(tbl_notas和tbl_disciplinas),我在tbl_notas上创建了一个事件,这样当用户选择一个表项时,会自动选择tbl_disciplinas中的相应项。我只需要在tbl_disciplinas中添加相同的事件,但是当我这样做时,它会产生错误。

My tables

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException

我尝试在表格的focus属性中创建一个事件,但没有成功

我的ChangeListener

   tbl_notasListener = (ChangeListener) (ObservableValue observable, Object oldValue, Object newValue) -> {
        tbl_disciplinas.getSelectionModel().select(tbl_notas.getSelectionModel().getSelectedIndex());
    };

    tbl_disciplinasListener = (ChangeListener) (ObservableValue observable, Object oldValue, Object newValue) -> {
        tbl_notas.getSelectionModel().select(tbl_disciplinas.getSelectionModel().getSelectedIndex());
    };

   //add changeListener
            tbl_disciplinas.getSelectionModel().selectedItemProperty().addListener(tbl_disciplinasListener);                                                  
                tbl_notas.getSelectionModel().selectedItemProperty().addListener(tbl_notasListener);  

完整错误

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at javafx.scene.control.TableView$TableViewArrayListSelectionModel.handleSelectedCellsListChangeEvent(TableView.java:2657)
at javafx.scene.control.TableView$TableViewArrayListSelectionModel.handleSelectedCellsListChangeEvent(TableView.java:2657)
at javafx.scene.control.TableView$TableViewArrayListSelectionModel.clearAndSelect(TableView.java:2180)
at javafx.scene.control.TableView$TableViewSelectionModel.clearAndSelect(TableView.java:1733)
at com.sun.javafx.scene.control.behavior.TableCellBehaviorBase.simpleSelect(TableCellBehaviorBase.java:318)
at com.sun.javafx.scene.control.behavior.TableCellBehaviorBase.doSelect(TableCellBehaviorBase.java:231)
at com.sun.javafx.scene.control.behavior.TableCellBehaviorBase.mousePressed(TableCellBehaviorBase.java:144)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:95)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:204)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3746)
at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:314)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:243)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:345)
at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
at com.sun.glass.ui.View.notifyMouse(View.java:898)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:744)

1 个答案:

答案 0 :(得分:0)

回答here James_D

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

}
}