4

私はそれを行ういくつかの例を見ましたが、それでも理解できず、実装することができません。

私がやりたいのはセルの変更(フォーカス)です。次に選択されたセルではすべてのテキストが選択され、ユーザーが完全に変更できるようになります。

それを行う方法についてのアイデアはありますか?

//更新//どういうわけか私は次のクラスを出すことができましたが

この
tblLayers.setDefaultEditor(String.class、new Classes.CellEditor());を実装します。

「まだサポートされていません」という結果は得られません。スローされません..

この問題をどのように解決すればよいですか?

import java.awt.Component;
import java.util.EventObject;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;


public class CellEditor extends JTextField implements TableCellEditor {


public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
    //        final JTextField ec = (JTextField) editorComponent;
    //
    //        ec.setText((String) value);
    //
    //        // selectAll, so that whatever the user types replaces what we just put there
    //        ec.selectAll();
    //
    //        SwingUtilities.invokeLater(new Runnable() {
    //
    //            public void run() {
    //                // make the component take the keyboard focus, so the backspace key works
    //                ec.requestFocus();
    //
    //                SwingUtilities.invokeLater(new Runnable() {
    //
    //                    public void run() {
    //                        // at this point the user has typed something into the cell and we
    //                        // want the caret to be AFTER that character, so that the next one
    //                        // comes in on the RHS
    //                        ec.setCaretPosition(ec.getText().length());
    //                    }
    //                });
    //            }
    //        });
    //        return editorComponent;


    throw new UnsupportedOperationException("Not supported yet.");
}

public Object getCellEditorValue() {
    throw new UnsupportedOperationException("Not supported yet.");
}

public boolean isCellEditable(EventObject anEvent) {
    throw new UnsupportedOperationException("Not supported yet.");
}

public boolean shouldSelectCell(EventObject anEvent) {
    throw new UnsupportedOperationException("Not supported yet.");
}

public boolean stopCellEditing() {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void cancelCellEditing() {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void addCellEditorListener(CellEditorListener l) {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void removeCellEditorListener(CellEditorListener l) {
    throw new UnsupportedOperationException("Not supported yet.");
}
}
4

3 に答える 3

12

私がやりたいのはセルの変更(フォーカス)です。次に選択されたセルではすべてのテキストが選択され、ユーザーが完全に変更できるようになります。

解決策は、正確な要件によって異なります。JTableには、レンダラーとエディターがあります。

レンダリングは通常、セル内のテキストのみを表示します。入力を開始するときにテキストを置き換えたい場合は、次の2つのことを行う必要があります。

a)レンダラーを変更して、テキストを「選択済み」状態で表示し、ユーザーが文字を入力すると既存のテキストが削除されることを認識できるようにします。b)エディターを変更して、呼び出されたときにすべてのテキストを選択します。

テーブル内のさまざまなデータ型(文字列、整数など)ごとにカスタムレンダラーが必要になるため、このアプローチは比較的困難です。

または、別のアプローチは、フォーカスが取得されたときに各セルを自動的に編集モードにすることです。したがって、テキストを選択するためにエディターを変更するだけで済みます。

このアプローチは、次のように簡単に実行できます。

JTable table = new JTable(data, columnNames)
{
    //  Place cell in edit mode when it 'gains focus'

    public void changeSelection(
        int row, int column, boolean toggle, boolean extend)
    {
        super.changeSelection(row, column, toggle, extend);

        if (editCellAt(row, column))
        {
            Component editor = getEditorComponent();
            editor.requestFocusInWindow();
            ((JTextComponent)editor).selectAll();
        }
    }
};
于 2011-12-13T16:56:11.260 に答える
5

に関してeditorComponent、この変数はどこで初期化できますか?

変数editorComponentはのフィールドですDefaultCellEditor

それ以外の

class CellEditor extends JTextField implements TableCellEditor

検討

class CellEditor extends DefaultCellEditor

次に、次のようなことを行うことができます。

@Override
public Component getTableCellEditorComponent(JTable table,
        Object value, boolean isSelected, int row, int column) {
    JTextField ec = (JTextField) editorComponent;
    if (isSelected) {
        ec.selectAll();
    }
    return editorComponent;
}

補遺:@ Edozによって提案され、この完全なselectAll()に示されているように、マウスクリックで編集が開始されたときに、を選択的に再キューイングできます。

JTable table = new JTable(model) {

    @Override // Always selectAll()
    public boolean editCellAt(int row, int column, EventObject e) {
        boolean result = super.editCellAt(row, column, e);
        final Component editor = getEditorComponent();
        if (editor == null || !(editor instanceof JTextComponent)) {
            return result;
        }
        if (e instanceof MouseEvent) {
            EventQueue.invokeLater(() -> {
                ((JTextComponent) editor).selectAll();
            });
        } else {
            ((JTextComponent) editor).selectAll();
        }
        return result;
    }
};
于 2011-12-13T11:50:30.840 に答える
2

これは https://forums.oracle.com/forums/thread.jspa?threadID=2317349に役立つはずです

于 2011-12-13T10:09:00.293 に答える