简体   繁体   English

JTable单元格编辑器更改类型

[英]JTable cell editor changing types

I ahve a JTable that is supposed to be 2 columns (String, JComboBox). 我有一个JTable,应该是2列(String,JComboBox)。 When i initialize the table everything looks good. 当我初始化表时,一切看起来都很好。 As soon as a I select a value in the table the JComboBox cell aquires the data type of the selected item. 一旦我在表中选择一个值,JComboBox单元就会获取所选项目的数据类型。

I want to keep the JCOmboBox there and have it fire the events of data change and the Table ignore data changes in that column and keep the ComboBox populated. 我想将JCOmboBox保留在那里,并使其触发数据更改的事件,并且Table忽略该列中的数据更改,并保持ComboBox的填充。

My table has this as an override 我的表将此作为替代

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

Implementation 实作

    JComboBox uploadBox = new JComboBox();
    uploadBox.addItem(MyPanel.UPLOAD_OPTIONS.PROMPT);
    uploadBox.addItem(MyPanel.UPLOAD_OPTIONS.UPLOAD);
    uploadBox.addItem(MyPanel.UPLOAD_OPTIONS.DONT_UPLOAD);

    Object[][] tableData = new Object[][]{
        {"Upload data on save", uploadBox}
    };



    table.setModel(
            new DefaultTableModel(tableData, new String[]{"Description", "Options"}) {
        Class[] types = new Class[]{String.class, JComboBox.class};
        boolean[] canEdit = new boolean[]{false, true};

        @Override
        public Class getColumnClass(int columnIndex) {
            return types[columnIndex];
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit[columnIndex];
        }


    });

    table.getColumnModel().getColumn(1).setCellRenderer(new TableCellRenderer() {

        @Override
        public Component getTableCellRendererComponent(JTable jtable, Object o, boolean bln, boolean bln1, int i, int i1) {
            return (Component)o;
        }
    });
  • answer is quite simple don't put JComboBox to the XxxTableModel or to set getColumClass for JComboBox.class , this is wrong (sure is possible but with bunch of side effects), XxxTableModel (is designated for) can hold directly only standard Java data types ( String , Date , Icon / ImageIcon , Integer , Double etc... ) 答案很简单,不要将JComboBox放到XxxTableModel或为JComboBox.class设置getColumClass ,这是错误的(确定是可能的,但有很多副作用), XxxTableModel (指定用于)只能直接保存标准Java数据类型( StringDateIcon / ImageIconIntegerDouble ImageIcon等)

  • XxxTableModel should be store (if you don't want to parsing between Java data types) the same data type like as is stored in DefaultComboBoxModel (noting clear what constans are MyPanel.XXX ), eg in XxxTableModel is stored String value when DefaultComboBoxModel has the same data types, similair logics for Date , Icon / ImageIcon , Integer or Double XxxTableModel应该是存储(如果你不想Java数据类型之间的解析)等为存储在相同的数据类型DefaultComboBoxModel (注意清楚的constans是MyPanel.XXX ),例如,在XxxTableModel存储String值时DefaultComboBoxModel有相同的数据类型, DateIcon / ImageIconIntegerDouble ImageIcon型的相似逻辑

  • for more info to read Oracle tutorial How to use Tables - Using a Combo Box as an Editor 有关更多信息,请阅读Oracle教程如何使用表-使用组合框作为编辑器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM