如何在cellchange上执行JTable选择所有文本

Mel*_*vin 4 java swing select jtable tablecelleditor

我已经看到了这样做的一些例子,但我仍然无法理解,也无法实现它.

我想做的是在细胞更换(焦点),下一个选定的单元格将选择所有文本,准备用户完全更改它..

关于如何做的任何想法?

//更新//不知怎的,我设法出来了下面的课但是

实现这个
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.");
}
}
Run Code Online (Sandbox Code Playgroud)

cam*_*ckr 11

我想做的是在细胞更换(焦点),下一个选定的单元格将选择所有文本,准备用户完全更改它..

解决方案取决于您的确切要求.JTable有一个渲染器和一个编辑器.

渲染通常只显示单元格中的文本.如果您希望在开始输入时替换文本,则需要执行以下两项操作:

a)更改渲染器以显示处于"已选择"状态的文本,以便用户知道键入字符将删除现有文本b)更改编辑器以在调用时选择所有文本

这种方法相对困难,因为您需要为表中的每种不同数据类型(即String,Integer)使用自定义渲染器.

或者另一种方法是在每个单元格获得焦点时自动将其置于编辑模式,因此您只需更改编辑器即可选择文本.

您可以这样做,这种方法很简单:

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();
        }
    }
};
Run Code Online (Sandbox Code Playgroud)


tra*_*god 5

关于editorComponent,我在哪里初始化这个变量?

变量editorComponent是一个字段DefaultCellEditor.

代替

class CellEditor extends JTextField implements TableCellEditor
Run Code Online (Sandbox Code Playgroud)

考虑

class CellEditor extends DefaultCellEditor
Run Code Online (Sandbox Code Playgroud)

然后你可以做这样的事情:

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

附录:正如@ 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;
    }
};
Run Code Online (Sandbox Code Playgroud)