JTable 表格中添加 JComboBox 控件

2080阅读 0评论2020-09-01 dehuai
分类:Java

在jtable中添加JComboBox控件
新建JComboBox对象,从jtable中得到列对象TableColumn,给此列设置JComboBox

javax.swing.JComboBox cbox1 = new JComboBox();
    cbox1.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            itemChanged(e); // comboBox某项被选中
        }
    });
    TableColumn tc0 = this.jTable1.getColumnModel().getColumn(0);
    tc0.setCellEditor(new DefaultCellEditor(cbox1));
    DefaultComboBoxModel cbm =(DefaultComboBoxModel) cbox1.getModel();
    
    for (Map row:sheetPrice){
        Integer id = 1;
        String type = "type";
        cbm.addElement(new u_sheet(id,type));
        
    }

u_sheet 是一个子定义的class
class u_sheet{
    private Integer id;
    private String type;
    public u_sheet(Integer id, String type) {
            this.id = id;
            this.type = type;
        }

        @Override
        public String toString() {  // 这句重要
            return this.type;
        }
        
    }
上一篇:字符串格式验证
下一篇:ubuntu/linux 启动次序