Skip to main content

Type Ahead feature in Combo boxes implemented JTable

No replies
bharat20
Offline
Joined: 2008-01-17

I have implemented some comboboxes in a jtable.

I have used the following code for implementing the comboboxes. I have used the cellrenderer and celleditor for implementing them.

TableColumn col = jTable1.getColumnModel().getColumn(1);
col.setCellEditor(new MyComboBoxEditor(values));
col.setCellRenderer(new MyComboBoxRenderer(values));
col = jTable1.getColumnModel().getColumn(2);
col.setCellEditor(new MyComboBoxEditor(values));
col.setCellRenderer(new MyComboBoxRenderer(values));

public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
public MyComboBoxRenderer(Vector items) {
super(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());
}

setSelectedItem(value);
return this;
}
}

public class MyComboBoxEditor extends DefaultCellEditor {
public MyComboBoxEditor(Vector items) {
super(new JComboBox(items));
}
}

Here "values" is the vector values which have to be the objects of the comboboxe. Anyway I want to implement the type ahead feature in these comboboxes. This is the feature in default jcomboboxes where if we type any alphabet the element in the jcombobox that start with that particular alphabet gets selected. this is there by default in jcombobox fields. But this is not implemented in the jtable.

I have tried to use the table.setSurrendersFocusOnKeystroke(true) method for the table. But this is transfering focus to the combobox for alternate keys.

Can anybody who faced a similar issue help me.

thanks in advance.