Search code examples
javajavafxtableviewright-click

How to make disable right click on selected row in TableView in java fx?


I am creating TableView in JavaFX.By clicking the left mouse button, I choose a table row, the same occurs, when I click the right mouse button. I want to make right-click disable with no focus on the row.Also I tried:

table.setSelectionModel(null);

How can I do this?

class TableViewer

public class TableViewer extends Application {
private TableView table = new TableView();
final ObservableList<Person> data = FXCollections.observableArrayList(
        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]")
);

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

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setTitle("Table View Sample");
    stage.setWidth(450);
    stage.setHeight(500);

    final Label label = new Label("Address Book");
    label.setFont(new Font("Arial", 20));

    table.setEditable(true);

    TableColumn firstNameCol = new TableColumn("First Name");
    firstNameCol.setMinWidth(100);
    firstNameCol.setCellValueFactory(
            new PropertyValueFactory<Person, String>("firstName"));

    TableColumn lastNameCol = new TableColumn("Last Name");
    lastNameCol.setMinWidth(100);
    lastNameCol.setCellValueFactory(
            new PropertyValueFactory<Person, String>("lastName"));

    TableColumn emailCol = new TableColumn("Email");
    emailCol.setMinWidth(200);
    emailCol.setCellValueFactory(
            new PropertyValueFactory<Person, String>("email"));

    table.setItems(data);
    table.addEventFilter(MouseEvent.MOUSE_CLICKED, e -> {
        if (e.getButton() == MouseButton.SECONDARY) {
            table.getSelectionModel().clearSelection();
        }
    });
    table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);

    final VBox vbox = new VBox();
    vbox.setSpacing(5);
    vbox.setPadding(new Insets(10, 0, 0, 10));
    vbox.getChildren().addAll(label, table);

    ((Group) scene.getRoot()).getChildren().addAll(vbox);

    stage.setScene(scene);
    stage.show();
}
}

class Person

public class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;

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

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

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

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

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

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

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

}

Solution

  • You can do

    table.setRowFactory(tv -> {
        TableRow<Person> row = new TableRow<>();
        row.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
            if (e.getButton() == MouseButton.SECONDARY) {
                e.consume();
            }
        });
        return row ;
    });
    

    Note: you should properly type your table and columns, instead of using raw types. I.e. you need to replace

    private TableView table = new TableView();
    

    with

    private TableView<Person> table = new TableView<>();
    

    and

    TableColumn firstNameCol = new TableColumn("First Name");
    

    with

    TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
    

    etc..