【问题标题】:JavaFX editable ComboBox in a table view表格视图中的 JavaFX 可编辑组合框
【发布时间】:2014-06-02 05:47:21
【问题描述】:

我在表格视图中使用可编辑的组合框单元格。这是我的 ComboBox 单元格类

public class ComboBoxCell extends TableCell<ClassesProperty, String> {

    private ComboBox<String> comboBox;

    public ComboBoxCell() {
    }

    @Override
    public void startEdit() {
        super.startEdit();

        if (comboBox == null) {
            createComboBox();
        }

        setGraphic(comboBox);
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);


        Platform.runLater(new Runnable() {

            @Override
            public void run() {
            comboBox.requestFocus();
            comboBox.getEditor().requestFocus();
            comboBox.getEditor().selectAll();


            }
        });


    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();

        setText(String.valueOf(getItem()));
        setContentDisplay(ContentDisplay.TEXT_ONLY);
    }

    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);

        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            if (isEditing()) {
                if (comboBox != null) {
                    comboBox.setValue(getString());
                }
                setGraphic(comboBox);
                setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
            } else {
                setText(getString());
                setContentDisplay(ContentDisplay.TEXT_ONLY);
            }
        }
    }

    private void createComboBox() {
        // ClassesController.getLevelChoice() is the observable list of String
        comboBox = new ComboBox<>(ClassesController.getLevelChoice());
        comboBox.setEditable(true);
        comboBox.setMinWidth(this.getWidth() - this.getGraphicTextGap()*2);
        comboBox.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent t) {
                if (t.getCode() == KeyCode.ENTER) {
                    commitEdit(comboBox.getSelectionModel().getSelectedItem());
                } else if (t.getCode() == KeyCode.ESCAPE) {
                    cancelEdit();
                }
            }
        });
    }

    private String getString() {
        return getItem() == null ? "" : getItem().toString();
    }
}

问题我需要在表格单元格上按三下才能得到组合框的文本字段来编辑内容。有没有办法一键两下?我什至使用过 Platforn runlater,但当我第一次尝试编辑一个单元格时,它需要点击三下鼠标,但第二次只需点击两下。

【问题讨论】:

    标签: javafx javafx-8


    【解决方案1】:

    在您重写的 cell.startEdit() 方法中,添加一个带有文本字段的 ListView,然后将此 listview 添加到 setGraphic。这将在选择行并单击单元格后直接显示列表视图,列表视图将在表格单元内,我还没有找到一种方法将其作为弹出窗口

    【讨论】:

      猜你喜欢
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 2016-05-09
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      相关资源
      最近更新 更多