我想在表中改变某一列为JCheckBox或JComboBox,而且想再单独控制每一个JCheckBox或JComboBox,各位大大帮帮忙!

解决方案 »

  1.   

    在JTable中修改估计不行吧,JTable中只能添加 文本 内容。要实现你想要的,只能用网格布局,这样每个格子中就可以添加组建了,文本内容只要在格子中添加JTextField组建即可,其他的任何组建都可在你想的地方添加。
      

  2.   

    你说的单独控制JCheckBox或JComboBox是什么意思?让某一列是JComboBox的编辑器实现起来不难,不清楚你想要的效果是怎么样的
      

  3.   

    创建你的renderer和Editor,这部分直接复制到你代码里就行了
        public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {        public MyComboBoxRenderer(String[] items) {
                super(items);
            }
            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) {
                if (isSelected) {
                    setForeground(table.getSelectionForeground());
                    super.setBackground(table.getSelectionBackground());
                } else {
                    setForeground(table.getForeground());
                    setBackground(table.getBackground());
                }
                setSelectedItem(value);
                return this;
            }
        }    public class MyComboBoxEditor extends DefaultCellEditor {
            public MyComboBoxEditor(String[] items) {
                super(new JComboBox(items));
            }
        }一个两列数据的例子,这个根据你的需要改    DefaultTableModel model = new DefaultTableModel();
        model.addColumn("A", new Object[]{"item1"});
        model.addColumn("B", new Object[]{"item6"});   
        jTable1.setModel(model);
        
        String[][] values = {{"item1", "item2", "item3", "item4"}, {"item6", "item7", "item8", "item9"}};
        for (int vColIndex = 0; vColIndex < jTable1.getColumnCount(); vColIndex++) {
            TableColumn col = jTable1.getColumnModel().getColumn(vColIndex);
            col.setCellEditor(new MyComboBoxEditor(values[vColIndex]));        col.setCellRenderer(new MyComboBoxRenderer(values[vColIndex]));    }
      

  4.   

    换成JCheckBox一个道理,DefaultCellEditor 也支持JCheckBox
    要稍微改一下MyComboBoxRenderer