如何根据javafx中的Action为同一个tableview提供一个Contextmenu?
Posted
技术标签:
【中文标题】如何根据javafx中的Action为同一个tableview提供一个Contextmenu?【英文标题】:How to give a Contextmenu to same tableview according to Action in javafx? 【发布时间】:2016-08-19 05:08:48 【问题描述】:我有一个组合框和一个表格。组合框有两个项目,它们是 type1 和 type2。当我从组合中选择 type1 时,表上下文菜单显示“type1 菜单”,当我从组合中选择 type2 时,表上下文菜单显示“type2 菜单”。如何解决这个问题? 这是我的实验代码......但它不能正常工作......!
import java.util.Arrays;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class TableViewSample extends Application
private final TableView<Person> table = new TableView<>();
private final ComboBox<String> combo = new ComboBox<>();
private 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("Right Click a table Row");
label.setFont(new Font("Arial", 20));
combo.getItems().setAll("Type1","Type2");
combo.getSelectionModel().select(0);
table.setEditable(true);
//----------------------- Add table column ------------------------------//
TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
new PropertyValueFactory<>("firstName"));
TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
new PropertyValueFactory<>("lastName"));
TableColumn<Person, String> emailCol = new TableColumn<>("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(
new PropertyValueFactory<>("email"));
table.setItems(data);
table.getColumns().addAll(Arrays.asList(firstNameCol, lastNameCol, emailCol));
setTableMenu(0); // You Can Comment This //
combo.setOnAction(event ->
setTableMenu(combo.getSelectionModel().getSelectedIndex());
);
//-------------------------- Final Works ------------------------------//
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, combo, table);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
//---------- Set What ContextMenu according to the combobox -----------//
private void setTableMenu(int selectedIndex)
table.setRowFactory((TableView<Person> tableView) ->
final TableRow<Person> row = new TableRow<>();
switch(selectedIndex)
case 0:
final ContextMenu contextMenu1 = new ContextMenu();
final MenuItem item1 = new MenuItem("Type1 menu");
item1.setOnAction((ActionEvent event) ->
System.out.println("Type 1 menu selected");
);
contextMenu1.getItems().add(item1);
// Set context menu on row, but use a binding to make it only show for non-empty rows:
row.contextMenuProperty().bind(
Bindings.when(row.emptyProperty())
.then((ContextMenu) null)
.otherwise(contextMenu1)
);
break;
case 1:
final ContextMenu contextMenu2 = new ContextMenu();
final MenuItem item2 = new MenuItem("Type2 menu");
item2.setOnAction((ActionEvent event) ->
System.out.println("Type 2 menu selected");
);
contextMenu2.getItems().add(item2);
// Set context menu on row, but use a binding to make it only show for non-empty rows:
row.contextMenuProperty().bind(
Bindings.when(row.emptyProperty())
.then((ContextMenu) null)
.otherwise(contextMenu2)
);
return row ;
);
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);
【问题讨论】:
好吧,我认为这是一个错误 - tableView 应该在设置新工厂时重新创建所有行/单元格 【参考方案1】:更改rowFactory
不会更新TableRow
s。这些行仍然是由初始 rowFactory
创建的行,上下文菜单的类型在创建行时决定并且永远不会更改。
但是,您可以在 ContextMenu
显示之前准备它并根据 ComboBox
的状态对其进行修改:
// in start method
table.setRowFactory((TableView<Person> tableView) ->
final TableRow<Person> row = new TableRow<>();
final ContextMenu contextMenu = new ContextMenu();
final MenuItem item1 = new MenuItem("Type1 menu");
item1.setOnAction((ActionEvent event) ->
System.out.println("Type 1 menu selected");
);
contextMenu.getItems().add(item1);
final MenuItem item2 = new MenuItem("Type2 menu");
item2.setOnAction((ActionEvent event) ->
System.out.println("Type 2 menu selected");
);
row.contextMenuProperty().bind(
Bindings.when(row.emptyProperty())
.then((ContextMenu) null)
.otherwise(contextMenu)
);
row.setOnContextMenuRequested(evt ->
// update menu when requested
switch (combo.getSelectionModel().getSelectedIndex())
case 0:
contextMenu.getItems().setAll(item1);
break;
case 1:
contextMenu.getItems().setAll(item2);
break;
);
return row;
);
/*setTableMenu(0);
combo.setOnAction(event ->
setTableMenu(combo.getSelectionModel().getSelectedIndex());
);*/
或者,您也可以对所有行使用相同的ContextMenu
,并在ComboBox
的更改中对其进行修改:
// in start method
final MenuItem item1 = new MenuItem("Type1 menu");
item1.setOnAction((ActionEvent event) ->
System.out.println("Type 1 menu selected");
);
final ContextMenu contextMenu = new ContextMenu(item1);
final MenuItem item2 = new MenuItem("Type2 menu");
item2.setOnAction((ActionEvent event) ->
System.out.println("Type 2 menu selected");
);
combo.setOnAction(evt ->
switch (combo.getSelectionModel().getSelectedIndex())
case 0:
contextMenu.getItems().setAll(item1);
break;
case 1:
contextMenu.getItems().setAll(item2);
break;
);
table.setRowFactory((TableView<Person> tableView) ->
final TableRow<Person> row = new TableRow<>();
row.contextMenuProperty().bind(
Bindings.when(row.emptyProperty())
.then((ContextMenu) null)
.otherwise(contextMenu)
);
return row;
);
/*setTableMenu(0);
combo.setOnAction(event ->
setTableMenu(combo.getSelectionModel().getSelectedIndex());
);*/
【讨论】:
hmmm .. 在设置行/cellFactory 时不重新创建行/单元格对我来说似乎是一个错误 只需调用refresh
即可重新创建行,但我决定不这样做,因为仅仅为了修改上下文菜单而无需重新创建行...
是的,永远不要使用刷新......这是邪恶的 api,专门用于极其罕见的情况,没有其他帮助。这绝对是一个错误:设置 cellFactory(与 rowFactory 相比)确实会按预期更新单元格。
hmm ... 似乎绑定阻碍了:普通设置 contextMenu 对于 rowFactory 看起来不错 ... 不知道为什么,虽然
不,这不是绑定——在我的测试中,我在设置 rowFactory 的同时设置了一个新的 cellFactory,然后使用新的 ContextMenu 创建了新的 rowFactory,用于绑定和手动设置。现在回到实际工作;-)以上是关于如何根据javafx中的Action为同一个tableview提供一个Contextmenu?的主要内容,如果未能解决你的问题,请参考以下文章
如何以编程方式用户根据 JavaFX 中的字符串值定义列表视图的颜色