2013-03-30 53 views
4

我有一个JComboBox,JTable中的第3和第4列,但我不知道如何得到它的项目...问题是没有方法来获取项目,但剧组的JComboBox在JTable中

JComboBox combo=(JComboBox) jTable1.getColumnModel().getColumn(3).getCellEditor(); 

你能帮助我吗?

+0

为了更好地帮助越早,张贴[SSCCE](http://sscce.org/ ) –

回答

5

JComboBox包裹在CellEditor。使用DefaultCellEditor时,您必须检索包裹组件,例如:

DefaultCellEditor editor = (DefaultCellEditor)table.getColumnModel().getColumn(3).getCellEditor(); 
JComboBox combo = (JComboBox)editor.getComponent(); 
+0

它的作品,但它删除所有行的所有组合框的项目...我试过: DefaultCellEditor编辑器(DefaultCellEditor)jTable1.getCellEditor(0,3); JComboBox组合=(JComboBox)editor.getComponent(); combo.removeItemAt(combo.getSelectedIndex()); –

+0

不知道我明白:你是否试图删除给定行中组合框的项目? –

+0

是的,我想删除特定行中的组合框的特定项目(我通过双击鼠标来完成,因此我想删除选定的元素) –

0

尝试这样:

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