第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何向 ComboBoxTableCell 添加鍵盤編輯支持

如何向 ComboBoxTableCell 添加鍵盤編輯支持

鴻蒙傳說 2023-06-14 14:21:45
ComboBoxTableCellComboBox允許在編輯模式下添加一個TableCell。如果comboBox.show()被調(diào)用(例如顯示彈出窗口),comboBox則按預(yù)期按下箭頭鍵和向上箭頭鍵時會做出反應(yīng),并在按下回車鍵后立即結(jié)束編輯模式。我只想使用鍵盤控制編輯。我找不到使用鍵盤調(diào)用“comboBox.show()”的方法。到現(xiàn)在為止,我嘗試使用setOnKeyPressed向ComboBoxTableCell(在通過工廠方法創(chuàng)建期間)或ComboBox(通過使用ComboBoxTableCell.getGraphic())添加回調(diào)。調(diào)用回調(diào)ComboBox.show()以打開彈出窗口,但未調(diào)用它們(通過System.out回調(diào)中的打印驗證)。actColumn.setCellFactory(        new Callback<TableColumn<S,Object>, TableCell<S,Object>>() {    private ObservableList<Object> list=optionList;    @SuppressWarnings("unchecked")    @Override    public TableCell<S, Object> call(TableColumn<S, Object> param) {        final ComboBoxTableCell<S,Object> cell=                new ComboBoxTableCell<S,Object>(list);        cell.setConverter((StringConverter<Object>) converter);        cell.setOnKeyPressed(event -> {           cell.startEdit();            Node node=cell.getGraphic();            System.out.println(node);            if(node instanceof ComboBox) {                System.out.println("Hit Key.");                final ComboBox<?> box=(ComboBox<?>) node;                box.show();            }        });除了這段代碼很奇怪之外,當(dāng)在單元格的編輯模式下按下一個鍵時,處理程序不會被調(diào)用(或者至少我沒有輸出)。我希望能夠選擇一個ComboBoxTableCell,按回車鍵(可能還有一個額外的鍵),然后內(nèi)部的彈出窗口ComboBox應(yīng)該顯示出來,而無需鼠標(biāo)進(jìn)行任何交互。
查看完整描述

1 回答

?
MYYA

TA貢獻(xiàn)1868條經(jīng)驗 獲得超4個贊

您可以子類化ComboBoxTableCell以添加您想要的行為。這是一個概念驗證:


import javafx.beans.InvalidationListener;

import javafx.beans.Observable;

import javafx.collections.ObservableList;

import javafx.event.EventHandler;

import javafx.scene.control.ComboBox;

import javafx.scene.control.TableView;

import javafx.scene.control.cell.ComboBoxTableCell;

import javafx.scene.input.KeyCode;

import javafx.scene.input.KeyEvent;

import javafx.util.StringConverter;


public class AutoShowComboBoxTableCell<S, T> extends ComboBoxTableCell<S, T> {


    /* 

     * May want to provide alternate constructors and static methods similar to

     * the ComboBoxTableCell class (i.e. the superclass).

     */


    private boolean enterPressed;


    public AutoShowComboBoxTableCell(StringConverter<T> converter, ObservableList<T> values) {

        super(converter, values);

        getStyleClass().add("auto-show-combo-box-table-cell");


        // Assumes TableView property is set only once (valid assumption?)

        tableViewProperty().addListener(new InvalidationListener() {

            @Override

            public void invalidated(Observable observable) {

                // Need to know if editing was started by the user pressing

                // the ENTER key (see #startEdit())

                EventHandler<KeyEvent> filter = event -> {

                    if (event.getCode() == KeyCode.ENTER) {

                        enterPressed = event.getEventType() == KeyEvent.KEY_PRESSED;

                    }

                };

                // Possible memory leak? Consider using WeakEventHandler (read docs)

                getTableView().addEventFilter(KeyEvent.KEY_PRESSED, filter);

                getTableView().addEventFilter(KeyEvent.KEY_RELEASED, filter);

                observable.removeListener(this);

            }

        });

    }


    @Override

    public void startEdit() {

        if (isEditing()) return;


        super.startEdit();

        if (isEditing()) {

            if (enterPressed) {

                // Cell was put into edit mode by the user pressing ENTER. This causes

                // problems since *releasing* ENTER while the ComboBox has the focus

                // results in the value being committed; this leads to the current value

                // being committed *immediately* after entering edit mode—not what we want. 

                // To fix that we consume the first ENTER-released event and then let all

                // subsequent events through (by removing the event filter).

                addEventFilter(KeyEvent.KEY_RELEASED, new EventHandler<>() {

                    @Override public void handle(KeyEvent event) {

                        if (event.getCode() == KeyCode.ENTER) {

                            event.consume();

                            removeEventFilter(KeyEvent.KEY_RELEASED, this);

                        }

                    }

                });

            }

            ComboBox<?> comboBox = (ComboBox<?>) getGraphic();

            comboBox.requestFocus(); // Needed to allow releasing ENTER to commit the value

            comboBox.show();

        }

    }


    @Override

    public void cancelEdit() {

        if (isEditing()) {

            super.cancelEdit();

            requestTableViewFocus();

        }

    }


    @Override

    public void commitEdit(T newValue) {

        if (isEditing()) {

            super.commitEdit(newValue);

            requestTableViewFocus();

        }

    }


    // Allows user to keep navigating the table via the keyboard

    private void requestTableViewFocus() {

        TableView<S> tableView = getTableView();

        if (tableView != null) {

            tableView.requestFocus();

        }

    }


}

注意:以上使用了實現(xiàn)細(xì)節(jié)的知識,圖形設(shè)置為編輯開始時的事實ComboBox以及導(dǎo)致提交編輯的原因。實施細(xì)節(jié)可能會更改,恕不另行通知。


查看完整回答
反對 回復(fù) 2023-06-14
  • 1 回答
  • 0 關(guān)注
  • 164 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號