JTable - 只在一个单元格中渲染复选框

时间:2013-12-01 18:11:58

标签: java swing jtable jcheckbox tablecelleditor

我正在尝试使用JTable作为属性编辑器。我想在一个列中使用不同类型的JComponents。

到目前为止,只要属性具有布尔值,我就可以显示复选框。但是,我无法点击该复选框并相应地设置值。它只是在单元格中显示,但当我点击它时,它的值变为字符串。

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

public class Main extends JFrame {

    public static void main(String[] args) {
        Main main = new Main();
    }

    public Main() {
        this.setBounds(55, 5, 400, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        init();

        this.setVisible(true);
        this.validate();

    }

    private void init() {
        JTable table = new JTable() {
            @Override
            public TableCellRenderer getCellRenderer(int row, int column) {
                if (column == 1) {
                    if (row == 1) {
                        Class cellClass = getValueAt(row, column).getClass();
                        return getDefaultRenderer(Boolean.class);
                    }
                }
                return super.getCellRenderer(row, column);
            }

        };
        table.setModel(new PropertyModel(new Property(true, 1234)));

        getContentPane().add(table);
    }

    public class Property {
        private Integer height;
        private boolean visible;

        public Property(boolean visible, Integer height) {
            super();
            this.visible = visible;
            this.height = height;
        }

        public Integer getHeight() {
            return height;
        }

        public boolean isVisible() {
            return visible;
        }

        public void setHeight(Integer height) {
            this.height = height;
        }

        public void setVisible(boolean visible) {
            this.visible = visible;
        }

        @Override
        public String toString() {
            return "Property [visible=" + visible + ", height=" + height + "]";
        }
    }

    public class PropertyModel extends AbstractTableModel {
        private String HEIGHT = "Height";
        private String VISIBLE = "Visible";

        private Property property;

        private String[] columnNames = { "Name", "Value" };
        private Object[][] data = { { HEIGHT, "", },
                { VISIBLE, new Boolean(false) } };

        public PropertyModel(Property property) {
            super();
            this.property = property;

            initializeData();

            this.addTableModelListener(new CustomPropertyTable());
        }

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        public String getColumnName(int c) {
            return columnNames[c];
        }

        @Override
        public int getRowCount() {
            return data.length;
        }

        @Override
        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        /*
         * Initializes the data as per the world object values.
         */
        private void initializeData() {
            data[0][1] = property.getHeight();
            data[1][2] = property.isVisible();
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            if (col == 0) {
                return false;
            } else {
                return true;
            }
        }

        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }

        public class CustomPropertyTable implements TableModelListener {

            @Override
            public void tableChanged(TableModelEvent e) {
                int row = e.getFirstRow();
                int column = e.getColumn();
                TableModel model = (TableModel) e.getSource();
                String columnName = model.getColumnName(column);
                String propertyName = (String) model
                        .getValueAt(row, column - 1);

                if (propertyName.equals(HEIGHT)) {
                    String propertyValue = (String) model.getValueAt(row,
                            column);
                    property.setHeight(Integer.valueOf(propertyValue));

                } else if (propertyName.equals(VISIBLE)) {
                    String propertyValue = (String) model.getValueAt(row,
                            column);
                    Boolean visible = Boolean.valueOf(propertyValue);
                    property.setVisible(visible);
                }

                System.out.println(property);
            }
        }
    }

}

截图: 1)什么时候开始

When it starts, it looks something like this

2)当我点击或尝试取消选中时

When I click or try to uncheck it, it becomes string

我可能做错了什么?

1 个答案:

答案 0 :(得分:4)

您可以使用与渲染器相同的方式提供编辑器。延长getCellEditor。例如,基于发布的代码:

@Override
public TableCellEditor getCellEditor(int row, int column) {
    if (column == 1) {
        Object value = getValueAt(row, column);
        if (value != null)
            return getDefaultEditor(value.getClass());
    }
    return super.getCellEditor(row, column);
}