📄 e953. creating a custom table cell editor in a jtable component.txt
字号:
A table cell editor needs to implement the TableCellEditor interface. This interface supports listeners. The listener code is conveniently provided by the class AbstractCellEditor so that most table cell editors extend from this class.
Like a renderer (see e928 Creating a Custom Cell Renderer in a JTable Component), an editor returns a component used to edit the value in the cell. For performance reasons, the editor should not create a new component each time getTableCellEditorComponent() is called. Rather, it should return the same component (or one from a set) every time.
The job of getTableCellEditorComponent() is to configure the component based on the coordinates and value in the cell. The table then uses the configured component and paints it on the screen. After painting it, the table no longer needs the component.
JTable table = new JTable();
// Add some data...
// Install the custom editor on the first column
int vColIndex = 0;
TableColumn col = table.getColumnModel().getColumn(vColIndex);
col.setCellEditor(new MyTableCellEditor());
public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {
// This is the component that will handle the editing of the cell value
JComponent component = new JTextField();
// This method is called when a cell value is edited by the user.
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int rowIndex, int vColIndex) {
// 'value' is value contained in the cell located at (rowIndex, vColIndex)
if (isSelected) {
// cell (and perhaps other cells) are selected
}
// Configure the component with the specified value
((JTextField)component).setText((String)value);
// Return the configured component
return component;
}
// This method is called when editing is completed.
// It must return the new value to be stored in the cell.
public Object getCellEditorValue() {
return ((JTextField)component).getText();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -