【问题标题】:JTable - Render checkbox in just one cellJTable - 仅在一个单元格中呈现复选框
【发布时间】:2013-12-17 09:42:56
【问题描述】:

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

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

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) 什么时候开始

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

我做错了什么?

【问题讨论】:

    标签: java swing jtable jcheckbox tablecelleditor


    【解决方案1】:

    您可以使用与渲染器相同的方式提供编辑器。扩展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);
    }          
    

    【讨论】:

    • 如果有getColumnClass真的不需要
    • @mKorbel OP 想要一种类似于属性表的行为,其中单个列可能具有不同的类型。例如第一行是字符串,第二行是布尔值。
    • aach 我看到 +1,感谢您的通知,我认为(非常肯定同步)需要覆盖 getColumnClass
    猜你喜欢
    • 1970-01-01
    • 2011-08-13
    • 2021-12-24
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多