java - 如何使用自定义 JTable 单元格编辑器和单元格渲染器

标签 java swing jtable tablecellrenderer tablecelleditor

我创建了一个带有自定义表格渲染器和自定义单元格编辑器的 JTable,它在图像中给出了结果

enter image description here

我使用扩展 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.");
        }
    }

我的问题是认为面板显示如我所料,我无法在文本字段中键入内容或更改复选框或单击按钮。请告诉我如何解决这个问题。

最佳答案

我强烈建议重用默认表格渲染器和编辑器中提供的功能,因为您的代码有很多问题

  1. 请拆分您的编辑器、渲染器和表格模型。让他们都在同一个类(class)真是太奇怪了
  2. 对于您的渲染器,不要每次都创建一个Component 的新实例。相反,重用相同的组件并仅在 getTableCellRendererComponent 方法中修改该 Component
  3. 同样适用于编辑器 Component
  4. 扩展默认编辑器,而不是使用 UnsupportedOperationException 或仅返回空的 String
  5. 来实现方法

为了支持我的第四点,引用 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/

相关文章:

java - 是否可以使用 EasyMock 创建一个实现多个接口(interface)的模拟对象?

Java jList1.getSelectedValue() 返回一个对象。我需要所选列表项中的字符串。

java - 无法在 JScrollPane 内构建或显示带有复选框的 Swing JTable

java - 过滤器后的渲染器,行不再突出显示

java - Tomcat - 单击 Web 服务获取 "The requested resource is not available"

java - 仅覆盖 Java 对象的 equals() 方法

java - 如何将 rxjava2 Zip 函数(从 Single/Observable)的数量概括为 n 个可选参数而不丢失其类型?

java - 这段代码有什么问题? (JScrollPane 和 JFrame)

java - JLabel 删除文本和边框之间的空格

java - JTable 列中可单击按钮的问题