2011-07-25 71 views
1

我已經創建了一個表格,每行的單元格中顯示了一個組合框。我已經分別使用了以下兩個類作爲單元格編輯器和單元格渲染器。不知何故,當顯示錶格時,單元格中的每個組合框在點擊時都不會打開。任何人都可以給我一個提示嗎?在此先感謝JComboBox不在jTable中打開

public class CellEditor extends DefaultCellEditor{ 

private static final long serialVersionUID = 1L; 

public CellEditor(String[] items) { 
    super(new JComboBox(items)); 
} 
} 

public class ComboBoxRenderer extends JComboBox implements TableCellRenderer { 
/****/ 
private static final long serialVersionUID = 1L; 

public ComboBoxRenderer(String[] items) { 
    super(items); 
} 

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

    if (isSelected) { 
     this.setForeground(table.getSelectionForeground()); 
     super.setBackground(table.getSelectionBackground()); 
    } else { 
     this.setForeground(table.getForeground()); 
     this.setBackground(table.getBackground()); 
    }   
    this.setSelectedItem(value);// Select the current value  
    return this; 
} 
} 
+0

請發表[SSCCE](http://sscce.org/)! – Moonbeam

+2

爲什麼要擴展DefaultCellEditor?這是沒有必要的。 – camickr

回答

1

請讀JTable的教程,有Editors and RenderersUsing a Combo Box as an Editor,在這個論壇(INC自動完成的JComboBox JTable中)或herehere

一些例子,但基本上是你對問題,(檢查你是否設置)

public boolean isCellEditable(int row, int col) { 
    if (col == someInt) { 
     return true; 
    } else if (col == TableColumnsStartsWithZero) { 
     return true; 
    } else { 
     return false; 
    } 
}