代码之家  ›  专栏  ›  技术社区  ›  Dan

将jComboBox放入jTable

  •  14
  • Dan  · 技术社区  · 16 年前

    我想把单独的JComboboBoxes放到jtable的每个单元中。也就是说,每个单元格的jComboBox内容不相同。

    我基本上希望能够调用以下代码,将一行JComboBox添加到jtable中。有人知道吗?谢谢

    JComboBox cb1 = new JComboBox(...);
    JComboBox cb2 = new JComboBox(...);
    model.addRow(new Object[] {"Row name", cb1, cb2} );
    
    JComboBox cb3 = new JComboBox(...);
    JComboBox cb4 = new JComboBox(...);
    model.addRow(new Object[] {"Row name 2", cb3, cb4} );
    

    我能找到的最接近的示例代码如下。但这是因为jcombobox内容对于单个列是相同的。不是我需要的解决方案。

    TableColumn col = table.getColumnModel().getColumn(vColIndex);
    col.setCellEditor(new MyComboBoxEditor(values));
    

    哪里

    public class MyComboBoxEditor extends DefaultCellEditor {
        public MyComboBoxEditor(String[] items) {
            super(new JComboBox(items));
        }
    }
    
    9 回复  |  直到 9 年前
        1
  •  -8
  •   Cogsy    16 年前

    最简单的方法是实现自己的 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);
        }
    
    }
    

    把这个载入你的jtable。如果尚未替换布尔值的默认单元格呈现器,则由于getColumnClass()的实现,所有单元格都将呈现为复选框。这些复选框的所有用户输入都是用setValueat()收集的。

        2
  •  9
  •   user74523    16 年前

    用此代码扩展jtable:

    @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);
    }
    

    这将为您获得A值的每个组合框创建一个唯一的JComboBox单元编辑器。

        3
  •  2
  •   willcodejavaforfood    16 年前

    您需要覆盖:

    Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    

    …在TableCellEditor中。传递给此方法的值是可以放入JComboBox中的值。这意味着该特定单元格的“值”必须是可以转换为集合的值。它可能只是一个对象列表,也可能是一个包含字段的POJO,这些字段可以构成JComboBox。

    所以,只需编辑mycomboxeditor来重写该方法,并更改您的模型,以允许实际表示多个其他对象的对象。

        4
  •  2
  •   Mujahid    13 年前

    我相信这能解决你的问题。说明在.getcolumn(int列)中需要设置组合框的列

    private void addComboToTable(JComboBox combo) {
        TableColumn gradeColumn = YourTable.getColumnModel().getColumn(0);
        JComboBox comboBox = combo;
        comboBox.removeAllItems();
        try {
            comboBox.addItem("Item 1");
            comboBox.addItem("Item 2");
            comboBox.addItem("Item 3");
        } catch (NullPointerException e) {
        } catch (Exception e) {
            e.printStackTrace();
        }
        gradeColumn.setCellEditor(new DefaultCellEditor(comboBox));
    }
    
        5
  •  2
  •   Ivar    13 年前

    对于每个行选择,jcomboBox内容呈现相同,因为 jtable不提供每列有多个编辑器的功能。 您必须扩展jtable类以支持行的附加选择。

    这篇文章很好地解释了这一点: http://www.javaworld.com/javaworld/javatips/jw-javatip102.html

        6
  •  2
  •   Adrian    12 年前

    除了CellEditor之外,还需要执行CellRenderer来绘制单元中的组合框,请看:

     public void example(){  
    
          TableColumn tmpColum =table.getColumnModel().getColumn(1);
          String[] DATA = { "Data 1", "Data 2", "Data 3", "Data 4" };
          JComboBox comboBox = new JComboBox(DATA);
    
          DefaultCellEditor defaultCellEditor=new DefaultCellEditor(comboBox);
          tmpColum.setCellEditor(defaultCellEditor);
          tmpColum.setCellRenderer(new CheckBoxCellRenderer(comboBox));
          table.repaint();
       }
    
    
    /**
       Custom class for adding elements in the JComboBox.
    */
    class CheckBoxCellRenderer implements TableCellRenderer {
            JComboBox combo;
            public CheckBoxCellRenderer(JComboBox comboBox) {
                this.combo = new JComboBox();
                for (int i=0; i<comboBox.getItemCount(); i++){
                    combo.addItem(comboBox.getItemAt(i));
                }
            }
            public Component getTableCellRendererComponent(JTable jtable, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                combo.setSelectedItem(value);
                return combo;
            }
        }
    
        7
  •  1
  •   sth    15 年前
    @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);
    }
    

    然后,重写 toString 方法从 JComboBox .

        8
  •  0
  •   Luke Woodward    16 年前

    This page 可能对您有所帮助,尽管您似乎只限于在列中的所有单元格中使用相同的组合框。

        9
  •  0
  •   Aivar    16 年前

    需要创建jtable的子类来重写方法tablecellEditor getcelleditor(int row,int column)。

    这使您能够为任何行和列组合设置任意单元格编辑器。默认方法是为整个列设置单元编辑器。

    (也可以通过覆盖getcellrenderer来设置单个单元渲染器。)

    推荐文章