gpt4 book ai didi

java - 如何在javafx中的TableView中禁用右键单击所选行?

转载 作者:行者123 更新时间:2023-11-30 02:50:13 25 4
gpt4 key购买 nike

我正在JavaFX中创建TableView。通过单击鼠标左键,我选择一个表行,当我单击鼠标右键时,也会发生同样的情况。我想禁用右键单击而不将焦点放在该行上。我也尝试过:

table.setSelectionModel(null);

我该怎么做?

类TableViewer

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

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

人员类别

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

}

最佳答案

你可以做到

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

注意:您应该正确键入表格和列,而不是使用原始类型。 IE。你需要更换

private TableView table = new TableView();

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

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

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

等等..

关于java - 如何在javafx中的TableView中禁用右键单击所选行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38933797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com