2009-01-19 62 views
14

我想將單個JComboBox放入JTable的每個單元格中。即。每個單元的JComboBox內容都不相同。將JComboBox放入JTable中

我基本上希望能夠調用下面的代碼將一行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)); 
    } 
} 
+0

非常簡單:表.getColumnModel()。getColumn(2).setCellEditor(new DefaultCellEditor(myComboBox));在那裏你明顯地加載你的值myComboBox。你不需要任何額外的課程! – Elmue 2016-12-24 04:15:09

回答

-8

最簡單的方法是實現自己的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。如果您還沒有替換Boolean的默認單元格渲染器,則由於您實現了getColumnClass(),所有單元格都將顯示爲複選框。所有用戶輸入到這些複選框都是用我們的setValueAt()收集的。

+20

Err,他問了一個JComboBox,而不是JCheckBox。這個答案如何被接受? – 2011-03-07 17:44:10

+0

@SarelBotha因爲OP最後一次出現在09年1月31日,正確的答案是從2009年6月3日。顯然沒有人認爲這是一個問題。參見[meta](http://meta.stackexchange.com/questions/161946/rethinking-sort-order-of-answers)。 – PiTheNumber 2013-02-04 07:06:14

0

This page可以幫助你,但看來你是僅限於具有一列所有單元相同的組合框。

2

你需要重寫:

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

...在TableCellEditor的。傳遞給此方法的值是您可以放入JComboBox的值。這意味着特定單元格的'價值'需要是可以轉化爲集合的東西。它可能只是一個對象列表,或者它可能是一個POJO,可以將它們製作成JComboBox。

因此,只需編輯MyComboBoxEditor來覆蓋該方法並更改模型以允許實際表示其他幾個對象的對象。

0

您需要創建JTable的子類來覆蓋方法TableCellEditor getCellEditor(int row,int column)。

這使您可以爲任何行和列組合設置任意單元格編輯器。默認的方法是爲整個列設置單元格編輯器。

(您也可以通過重寫getCellRenderer設置單獨的單元格渲染器。)

9

擴展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中單元格編輯器價值。

+3

+1 - 還應該在相應列的`TableColumnModel`上設置一個自定義的`TableCellRenderer`,以確保所選值被繪製而不是字符串`javax.swing.JCombobox [...]`而單元格未被編輯。這個`TableCellRenderer`應該實現`getTableCellRendererComponent(..)`,並且可以返回一個`JLabel`,其值爲'JComboBox.getSelectedItem()。toString()`(檢查空指針後)。 – 2011-06-04 09:24:19

1
@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重寫toString方法。

2

我相信這會解決您的問題。提及您需要在哪個列中設置組合框。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)); 
} 
2

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