Tableview Changelistener - NPE

Asked

Viewed 110 times

2

I have two Tableview (tabela1 and tabela2) side by side.

What I need to do is: when selecting an item in tabela1 the corresponding item in the tabela2.

So far so good, it was easy, but I need to reproduce the same effect on tabela2, and that’s when the NPE, the Listener applied in the tabela1 conflicts with the tabela2.

I tried to create an event on focusedProperty(), but without success.

I made a test application to be able to post here, but since it would not fit all code, follows link to download: Tableview - Test.

  • try to create a MCVE that reproduces the problem and post it here, as it will be easier to help you. Only with your Istener will be difficult.

  • Oops, I added two video links of my working application, take a look

  • I guess you still don’t understand what the MCVE. You need to reduce your problem to something minimal and compilable, it’s a great debug exercise and helps us answer your question. Posting videos won’t help much, post a complete code that reproduce the error you are facing.

  • So thanks a lot friend for your effort in trying to help me, but the codes are those already cited as My Listener, but as soon as I get home I will explain step by step the code thank you very much

  • again the code you posted there are far from being compilable and verifiable just posted listeners and want to check something that happens in the Graphical Interface.

  • Good evening, I rephrased the question, I made a test application, but as it would not fit all code I left the link to download

Show 1 more comment

1 answer

1


Solution provided by 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", "[email protected]"),
            new Person("Isabella", "Johnson", "[email protected]"),
            new Person("Ethan", "Williams", "[email protected]"),
            new Person("Emma", "Jones", "[email protected]"),
            new Person("Michael", "Brown", "[email protected]")        
    );

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

}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.