Putting JComboBox into JTable

前端 未结 9 1294
离开以前
离开以前 2020-11-30 10:22

I want to put individual JComboBoxes into each cells of a JTable. ie. The JComboBox content is not identical for each cell.

I basically would like to be able to jus

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 10:48

    The easiest way is to implement your own TableModel

    public class MyModel extends AbstractTableModel {
        List rows;
    
        public int getRowCount() {
            return rows.size();
        }
    
        public int getColumnCount() {
             return 4;
        }
    
        public Object getValueAt(int row, int column) {
            return rows.get(row).getCol(col);  //assuming your row "Object" has a getCol()
        }
    
        public Class getColumnClass(int col) {
            return Boolean.class;
        }
    
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            rows.get(rowIndex).getCol(columnIndex).setValue(aValue);
        }
    
    }
    

    Load this into you JTable. If you haven't replaced the default cell renderer for Boolean's, all you cells will be rendered as check boxes thanks to you implementation of getColumnClass(). All user input to these check boxes is collected with our setValueAt().

提交回复
热议问题