gpt4 book ai didi

JavaFX : ComboBoxTableCell - how to select a value in one click?

转载 作者:行者123 更新时间:2023-12-04 17:47:05 25 4
gpt4 key购买 nike

我有一个带有 ComboBoxTableCell 的 TableView,当使用默认实现时,用户必须单击三次 才能从 ComboBox 的列表中选择一个值。我希望当用户单击单元格时显示组合框列表。我的解决方案基于这个: JavaFX editable ComboBox in a table view

单元格确实进入了编辑模式(调用了 startEdit()),但需要再次单击才能显示值列表,我错过了什么?

table.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> 
{
if (table.getEditingCell() == null)
{
TablePosition focusedCellPos = table.getFocusModel().getFocusedCell();
table.edit(focusedCellPos.getRow(), focusedCellPos.getTableColumn());
}
});

谢谢。

最佳答案

有趣的问题 - 一段时间后再次冒泡 :)

看起来 OP 的方法确实有效(从 fx11 开始,围绕其编辑的一些错误似乎已修复)- 在组合单元格的帮助下:

  • 在 tableView 上的单击处理程序中开始编辑(来自 OP)
  • 扩展 ComboBoxTableCell 并覆盖其 startEdit 以打开下拉列表

代码片段:

// set editable to see the combo
table.setEditable(true);
// keep approach by OP
table.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
TablePosition<Person, ?> focusedCellPos = table.getFocusModel()
.getFocusedCell();
if (table.getEditingCell() == null) {
table.edit(focusedCellPos.getRow(),
focusedCellPos.getTableColumn());
}
});
// use modified standard combo cell shows its popup on startEdit
firstName.setCellFactory(cb -> new ComboBoxTableCell<>(firstNames) {

@Override
public void startEdit() {
super.startEdit();
if (isEditing() && getGraphic() instanceof ComboBox) {
// needs focus for proper working of esc/enter
getGraphic().requestFocus();
((ComboBox<?>) getGraphic()).show();
}
}

});

关于JavaFX : ComboBoxTableCell - how to select a value in one click?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47994158/

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