這裏的James_D的解決方案的一個例子。我剛剛創建了它,因爲我很想知道如何通過在上下文菜單保持打開狀態時選擇上下文菜單中的列來更改表列的可見性,以便您可以選擇多個列。
public class TableWithContextMenu extends Application {
private TableView<Person> table = new TableView<Person>();
private 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]"));
final HBox hb = new HBox();
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(550);
TableColumn<Person, String> firstNameCol = new TableColumn<Person, String>("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
TableColumn<Person, String> lastNameCol = new TableColumn<Person, String>("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
TableColumn<Person, String> emailCol = new TableColumn<Person, String>("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
// build context menu
// --------------------------------
ContextMenu contextMenu = new ContextMenu();
MenuItem item1 = new MenuItem("Closing Item");
item1.setOnAction(e -> System.out.println("Clicked: " + e));
CustomMenuItem item2 = new CustomMenuItem(new Label("Stay Open Item"));
item2.setHideOnClick(false);
item2.setOnAction(e -> System.out.println("Clicked: " + e));
CustomMenuItem item3 = new CustomMenuItem(new Label("Manually Closing Item"));
item3.setHideOnClick(false);
item3.setOnAction(e -> {
System.out.println("Clicked: " + e);
contextMenu.hide();
});
CheckBox firstNameCheckBox = new CheckBox("First Name");
firstNameCheckBox.selectedProperty().bindBidirectional(firstNameCol.visibleProperty());
CustomMenuItem item4 = new CustomMenuItem(firstNameCheckBox);
item4.setHideOnClick(false);
CheckBox lastNameCheckBox = new CheckBox("Last Name");
lastNameCheckBox.selectedProperty().bindBidirectional(lastNameCol.visibleProperty());
CustomMenuItem item5 = new CustomMenuItem(lastNameCheckBox);
item5.setHideOnClick(false);
CheckBox emailCheckBox = new CheckBox("email");
emailCheckBox.selectedProperty().bindBidirectional(emailCol.visibleProperty());
CustomMenuItem item6 = new CustomMenuItem(emailCheckBox);
item6.setHideOnClick(false);
CustomMenuItem item7 = new CustomMenuItem(new CheckBox("Custom Action"));
item7.setHideOnClick(false);
item7.setOnAction(e -> {System.out.println(e);});
contextMenu.getItems().addAll(item1, item2, item3, new SeparatorMenuItem(), item4, item5, item6, new SeparatorMenuItem(), item7);
// set context menu
table.setContextMenu(contextMenu);
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(table);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;
private 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);
}
}
}
按你的建議只是嘗試。它仍然沒有工作。明天我會安排一個SSCCE。 – tmn
James_D的解決方案確實有效。那麼究竟哪些不適合你?請提供完整的示例代碼。 – Roland