4

i have seen some example of doing it but i still can't understand and not able to implement it.

What i want to do is on cell change (focus), the next selected cell will have all the text selected, ready for user to totally change it..

Any ideas on how to do it ?

//update// somehow i managed to come out with the following class but

implement this
tblLayers.setDefaultEditor(String.class, new Classes.CellEditor());

yields nothing, the "Not supported yet." is NOT thrown ..

how should I solve this problem ?

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 有一个渲染器和一个编辑器。

渲染通常只显示单元格中的文本。如果您希望在开始输入时替换文本,那么您需要做两件事:

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 回答