1

ユーザーが。のブールセルをクリックしてテキストフィールドに入力できるようにするコードを少し作成しようとしていますJTable

画像

プログラムにテーブルのデータをテキストフィールドに入力させることはできますが、これを行う現在の方法には、奇妙な理由でテーブルがチェックボックスの値を変更できないようにするJOptionPaneが含まれます(つまり、チェックボックスは変更しません。 t黒からチェックマークに変更します)。これだけでなく、選択は更新されないため、選択によってtrueに切り替えられたとしても、最後の列の値はfalseのままになります。

JOptionPaneどういうわけか選択イベントをオーバーライドすることと関係があるのではないかと思いますが、そのJOptionPaneオブジェクトについては、その方法を説明するのに十分な知識がありません。私のコードは次のとおりです。

table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
ListSelectionModel selectionModel = table.getSelectionModel();
selectionModel.addListSelectionListener(new ListSelectionListener() {

    public void valueChanged(ListSelectionEvent e) {
        ListSelectionModel lsm = (ListSelectionModel) e.getSource();
        if (lsm.isSelectionEmpty()) {
            //no rows are selected do nothing
        } else {
            //First find the row clicked
            int selectedRow = lsm.getLeadSelectionIndex();
            /*
                * put a popup here to ask the user which peak to associate
                * the energy with.
                */
            System.out.println(selectedRow);
            //Get user to associate with a peak
            availablePeaks = getAvailablePeaks();
            String returnVal = (String) JOptionPane.showInputDialog(
                null,
                "Select the peak:",
                "Peak Matching",
                JOptionPane.QUESTION_MESSAGE,
                null,
                availablePeaks, null);
            System.out.println(returnVal);
            //Determine the selection
            int index = 0;
            for (int i = 0; i < availablePeaks.length; i++) {
                if (availablePeaks[i] == returnVal) {
                    index = i;
                } else {
                }
            }
            //Set the peak value in the peak specifier to the energy in the row
            double energy = (Double) table.getValueAt(selectedRow, 0);
            System.out.println(energy);
            frame.getPeakSetter().getPeakSpecifiers()[index].setEnergy(energy);
            frame.getPeakSetter().getPeakSpecifiers()[index].getTextField().setText("" + energy);
        }
    }
});

のがテーブルのチェックボックスの更新を停止するJOptionPane理由を誰かが知っていますか?ListSelectionListener

ありがとう!

4

1 に答える 1

2

true私はあなたのモデルがのために戻り、isCellEditable()それが列のためにgetColumnClass()戻ると仮定します。これにより、ここにリストされているデフォルトのrednerer/editorが有効になります。Boolean.classJCheckBox

行を選択するジェスチャがダイアログを表示しているように見えます。これがどのようにDefaultCellEditor結論を妨げるのかは明らかではありません。わたしにはできる。チェックしていないので、2つのインスタンスgetValueIsAdjusting()が表示されないことに驚いています。ListSelectionEvent

いずれにせよ、選択が変わるたびにダイアログを表示するのは面倒なようです。いくつかの選択肢があります。

  • を保持し、からListSelectionListener戻ることでセルを編集不可にし、ダイアログが正常に終了した場合にのみモデルにその値を設定します。falseisCellEditable()

  • ここに示されているエディターListSelectionListenerを優先してドロップします。JButton

  • 以下に概説するようListSelectionListenerに、カスタムを支持してドロップします。CellEditor

    table.setDefaultEditor(Boolean.class, new DefaultCellEditor(new JCheckBox()) {
    
        @Override
        public boolean stopCellEditing() {
            String value = JOptionPane.showInputDialog(...);
            ...
            return super.stopCellEditing();
        }
    });
    
于 2012-08-14T17:03:31.577 に答える