views:

4744

answers:

7

Hi,

I want to put individual JComboBoxes into each cells of a JTable. ie. The JComboBox content is not identical for each cell.

I basically would like to be able to just call the following code to add a row of JComboBox into the JTable. Anyone has any idea? Thanks

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

This is the resultant view if I do the above.

http://www.freeimagehosting.net/uploads/a6292e08ee.png

The closest example code I can find is as follows. But it is for where JComboBox content is identical for the individual column. Not the solution I need.

TableColumn col = table.getColumnModel().getColumn(vColIndex);
col.setCellEditor(new MyComboBoxEditor(values));

where

public class MyComboBoxEditor extends DefaultCellEditor {
    public MyComboBoxEditor(String[] items) {
        super(new JComboBox(items));
    }
}
A: 

This page might help you, although it seems you are restricted to having the same combobox in all the cells in a column.

Pourquoi Litytestdata
+1  A: 

You need to override:

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

...in TableCellEditor. The value passed in to this method is what you can put in your JComboBox. That means that the 'value' for that particular cell needs to be something that can be translated into a collection. It could potentially just be a List of objects or it could be a POJO with fields that could be made into a JComboBox.

So just edit MyComboBoxEditor to override that method and change your model to allow for an Object that actually represents several other objects.

willcodejavaforfood
+1  A: 

The easiest way is to implement your own 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);
    }

}

Load this into you JTable. If you haven't replaced the default cell renderer for Boolean's, all you cells will be rendered as check boxes thanks to you implementation of getColumnClass(). All user input to these check boxes is collected with our setValueAt().

Cogsy
A: 

You need to create a subclass of JTable to override the method TableCellEditor getCellEditor(int row, int column).

This enables you to set arbitrary cell editors for any row and column combination. The default way is to set the cell editor for an entire column.

(You can also set individual cell renderers by overriding getCellRenderer.)

A: 

Extend JTable with this code:

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

This will create a unique JComboBox cell editor for each combo box you get the a value for.

A: 
@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);
}

And then, override the toString method from JComboBox.

Rogério
A: 

There is a tutorial on a pattern of embedding components into JTables here:

http://ivolo.mit.edu/post/A-Simple-Pattern-for-Embedding-Components-into-a-Swing-JTable.aspx

Ilya