我看过一些这样做的例子,但我仍然无法理解并且无法实现它。
我想做的是单元格更改(焦点),下一个选定的单元格将选择所有文本,准备好让用户完全更改它。
关于如何做有什么想法吗?
//更新//
不知怎的,我设法完成了以下课程,但是
实施这个
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.");
}
}
我想做的是单元格更改(焦点),下一个选定的单元格将选择所有文本,准备好让用户完全更改它。
解决方案取决于您的具体要求。 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();
}
}
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)