是否有可能选择&在鼠标单击或按下某个TableCell
的{{1}}之后,在TableView
内修改MenuItem
?默认ContextMenu
实施仅支持在TextFieldTableCell
有焦点时按Enter键或直接点击相应的单元格以选择&编辑其内容。
答案 0 :(得分:1)
您可以通过扩展TextFieldTableCell
来完成此操作。
示例强>
public class ContextTextFieldTableCell<S, T> extends TextFieldTableCell<S, T> {
private void init() {
MenuItem editItem = new MenuItem("edit");
ContextMenu contextMenu = new ContextMenu(editItem);
setContextMenu(contextMenu);
setEditable(false);
setOnContextMenuRequested(evt -> {
editItem.setDisable(!(getTableColumn().isEditable() && getTableView().isEditable()));
});
editItem.setOnAction(evt -> {
setEditable(true);
getTableView().edit(getIndex(), getTableColumn());
});
}
public ContextTextFieldTableCell() {
}
public ContextTextFieldTableCell(StringConverter<T> converter) {
super(converter);
init();
}
@Override
public void cancelEdit() {
super.cancelEdit();
setEditable(false);
}
@Override
public void commitEdit(T newValue) {
super.commitEdit(newValue);
setEditable(false);
}
public static <T> Callback<TableColumn<T, String>, TableCell<T, String>> cellFactory() {
return cellFactory(new DefaultStringConverter());
}
public static <S, T> Callback<TableColumn<S, T>, TableCell<S, T>> cellFactory(final StringConverter<T> converter) {
if (converter == null) {
throw new IllegalArgumentException();
}
return column -> new ContextTextFieldTableCell<>(converter);
}
}
@Override
public void start(Stage primaryStage) {
TableView<Item<String>> tableView = createTable();
tableView.setEditable(true);
TableColumn<Item<String>, String> editColumn = new TableColumn("value(editable)");
editColumn.setCellFactory(ContextTextFieldTableCell.cellFactory());
editColumn.setCellValueFactory(cd -> cd.getValue().valueProperty());
tableView.getColumns().add(editColumn);
Scene scene = new Scene(tableView);
primaryStage.setScene(scene);
primaryStage.show();
}
请注意,这会删除&#34;标准&#34;进入编辑模式的方式。如果您不需要此功能,只需使用cellFactory
自定义TextFieldTableCell
:
public static <S, T> Callback<TableColumn<S, T>, TableCell<S, T>> cellFactory(final StringConverter<T> converter) {
if (converter == null) {
throw new IllegalArgumentException();
}
return column -> {
final TextFieldTableCell<S, T> cell = new TextFieldTableCell<>(converter);
MenuItem editItem = new MenuItem("edit");
ContextMenu contextMenu = new ContextMenu(editItem);
cell.setContextMenu(contextMenu);
cell.setOnContextMenuRequested(evt -> {
editItem.setDisable(!(cell.isEditable() && cell.getTableColumn().isEditable() && cell.getTableView().isEditable()));
});
editItem.setOnAction(evt -> {
cell.getTableView().edit(cell.getIndex(), cell.getTableColumn());
});
return cell;
};
}
答案 1 :(得分:1)
如果正确理解你的问题,是的,你可以这样做。这是一个允许您使用上下文菜单编辑第二列中的单元格的示例:
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.stage.Stage;
public class EditableTableMCVE extends Application {
@Override
public void start(Stage stage) {
int numOfCols = 10;
ObservableList<ObservableList<String>> tableData = FXCollections.observableArrayList();
// Generate dummy data.
for (int i = 0; i < 100; i++) {
ObservableList<String> row = FXCollections.observableArrayList();
for (int j = 0; j < numOfCols; j++)
row.add("Row" + i + "Col" + j);
tableData.add(row);
}
TableView<ObservableList<String>> table = new TableView<ObservableList<String>>();
table.setEditable(true);
// Add columns to the table.
for (int i = 0; i < numOfCols; i++) {
// We make all cells in column on index 1 editable.
if (i == 1) {
table.getColumns().add(addEditableColumn(i, "Column " + i));
} else {
table.getColumns().add(addColumn(i, "Column " + i));
}
}
table.getItems().addAll(tableData);
MenuItem editItem = new MenuItem("Edit");
editItem.setOnAction(e -> {
// We need to get the index of the selected row and the TableColumn
// on the column index we want to edit.
int selectedRowIndex = table.getSelectionModel().getSelectedIndex();
table.edit(selectedRowIndex, table.getColumns().get(1));
});
ContextMenu menu = new ContextMenu(editItem);
table.setContextMenu(menu);
Scene scene = new Scene(table);
stage.setScene(scene);
stage.show();
}
/**
* Returns a simple column.
*/
private TableColumn<ObservableList<String>, String> addColumn(int index, String name) {
TableColumn<ObservableList<String>, String> col = new TableColumn<ObservableList<String>, String>(name);
col.setCellValueFactory(e -> new SimpleStringProperty(e.getValue().get(index)));
return col;
}
/**
* Returns an editable column.
*/
private TableColumn<ObservableList<String>, String> addEditableColumn(int index, String name) {
TableColumn<ObservableList<String>, String> col = new TableColumn<ObservableList<String>, String>(name);
col.setCellValueFactory(e -> new SimpleStringProperty(e.getValue().get(index)));
col.setCellFactory(TextFieldTableCell.forTableColumn());
col.setOnEditCommit(new EventHandler<CellEditEvent<ObservableList<String>, String>>() {
@Override
public void handle(CellEditEvent<ObservableList<String>, String> t) {
((ObservableList<String>) t.getTableView().getItems().get(t.getTablePosition().getRow())).set(index,
t.getNewValue());
}
});
return col;
}
public static void main(String[] args) {
launch();
}
}