Different JComboBoxes in JTable
Hi,
I want to add several JComboBoxes in a JTable but each one of them with different items.
I have already a JTable with a JComboBox in it .. but all comboBoxes have the same values and I can't figure out how to load a different array in a combobox.
Does anyone knows ?
This is what I do right now:
// Create the JTable and its model
JTable table =new JTable();
DefaultTableModel model = (DefaultTableModel)table.getModel();
String[] comboValues =null;
List reportOptions = getReportOptions();
comboValues =new String[reportOptions.size()];
for (int j=0;j<reportOptions.size();j++){
ReportOption option = (ReportOption) reportOptions.get(j);
comboValues[j] = option.getWhereDescription();
}
model.addColumn("Current Selection", comboValues);
// Create rederer and editor
TableColumn col = table.getColumnModel().getColumn(0);
col.setCellRenderer(new ReportOptionComboBoxRenderer(comboValues));
col.setCellEditor(new ReportOptionComboBoxEditor(comboValues));
The renderer and editor:
publicclass ReportOptionComboBoxRendererextends JComboBoximplements TableCellRenderer{
public ReportOptionComboBoxRenderer(Object[] items){
super(items);
}
publicvoid setItems(Object[] items){
}
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected,
boolean hasFocus,int row,int column){
if (isSelected){
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
}else{
setForeground(table.getForeground());
setBackground(table.getBackground());
}
// Select the current value
setSelectedItem(value);
returnthis;
}
}
publicclass ReportOptionComboBoxEditorextends DefaultCellEditor{
public ReportOptionComboBoxEditor(Object[] items){
super(new JComboBox(items));
}
}
>

