我希望把个人JComboBoxes成一个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));
    }
}
有帮助吗?

解决方案

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

其他提示

与此代码扩展的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);
}

这将创建的每个组合框,你得到了一个价值的独特的JComboBox格编辑器。

我相信这将解决你的问题。提到在哪一列需要设置组合框在.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));
}

您需要重写:

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

...在TableCellEditor的。传递给此方法的价值是你可以把什么在你的JComboBox。这意味着,“价值”为特定的细胞必须的东西,可以转化为一个集合。它可能只是对象的列表,或者它可以是包含可作成的JComboBox字段POJO。

所以只需编辑MyComboBoxEditor覆盖该方法,改变你的模型,以便对实际上代表了其他几个对象的对象。

在JComboBox的内容是呈现为每一行选择,因为相同 JTable中不提供每列多个编辑器的能力。 必须扩展JTable类以支持额外的选择的行。

本文解释非常好: http://www.javaworld.com/javaworld/javatips/jw-javatip102.html

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

然后,覆盖从toStringJComboBox方法。

此页面可以帮助你,虽然看上去你被限制于具有在列中的所有小区是相同的组合框。

您需要创建的JTable的子类要覆盖的方法TableCellEditor的getCellEditor(INT行,INT列)。

此,可以设置任意的单元格编辑器为任何行和列的组合。缺省方式是设置为整个列中的单元的编辑器。

(也可以通过重写getCellRenderer设置单个细胞渲染器。)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top