我创建了一个带有自定义表格渲染器和自定义单元格编辑器的 JTable,它在图像中给出了结果
我使用扩展 JPanel 的单独类创建了第一个表格单元格中显示的面板。并将表值添加为,
tbl.setCellEditor(new customCell());
tbl.getColumnModel().getColumn(0).setCellRenderer(new customCell());
DefaultTableModel dtm = (DefaultTableModel) tbl.getModel();
Vector v = new Vector();
v.add(new Panel());
v.add("Test");
dtm.addRow(v);
v.clear();
v.add(new Panel());
v.add("Test 2");
dtm.addRow(v);
这是我创建这个表的表自定义类,
class customCell extends DefaultTableModel implements TableCellRenderer, TableCellEditor {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Panel p = new Panel();
table.setRowHeight(row, p.getHeight());
return p;
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
return new Panel();
}
public Object getCellEditorValue() {
return "";
}
public boolean isCellEditable(EventObject anEvent) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
public boolean stopCellEditing() {
return true;
}
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.");
}
}
我的问题是认为面板显示如我所料,我无法在文本字段中键入内容或更改复选框或单击按钮。请告诉我如何解决这个问题。
最佳答案
我强烈建议重用默认表格渲染器和编辑器中提供的功能,因为您的代码有很多问题
- 请拆分您的编辑器、渲染器和表格模型。让他们都在同一个类(class)真是太奇怪了
- 对于您的渲染器,不要每次都创建一个
Component
的新实例。相反,重用相同的组件并仅在getTableCellRendererComponent
方法中修改该Component
- 同样适用于编辑器
Component
- 扩展默认编辑器,而不是使用
UnsupportedOperationException
或仅返回空的String
来实现方法
为了支持我的第四点,引用 Editors part in the JTable
tutorial :
What if you want to specify an editor other than a text field, check box, or combo box? As DefaultCellEditor does not support other types of components, you must do a little more work. You need to create a class that implements the TableCellEditor interface. The AbstractCellEditor class is a good superclass to use. It implements TableCellEditor's superinterface, CellEditor, saving you the trouble of implementing the event firing code necessary for cell editors.
Your cell editor class needs to define at least two methods — getCellEditorValue and getTableCellEditorComponent. The getCellEditorValue method, required by CellEditor, returns the cell's current value. The getTableCellEditorComponent method, required by TableCellEditor, should configure and return the component that you want to use as the editor.
正如那里清楚解释的那样,您必须实现事件触发代码:
saving you the trouble of implementing the event firing code necessary for cell editors.
你显然忽略了这一点。因此,我建议从 AbstractCellEditor
开始,而不是从头开始实现接口(interface)
关于java - 如何使用自定义 JTable 单元格编辑器和单元格渲染器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11858286/