Hi there! Me again with more questions!
I'm trying to figure out the finer parts of JTable navigation and editing controls. It's getting a bit confusing. The main problem I'm trying to solve is how to make a JTable using a combo box editor stop editing by hitting the 'enter' key in the same fashion as a JTextField editor. This is no regular DefaultCellEditor though -- it's one that uses the SwingX AutoCompleteDecorator. I have an SSCCE that demonstrates the issue:
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.AbstractCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableModel;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
public class AutoCompleteCellEditorTest extends JFrame {
public AutoCompleteCellEditorTest() {
JTable table = new JTable();
Object[] items = {"A", "B", "C", "D"};
TableModel tableModel = new DefaultTableModel(2, 2);
table.setModel(tableModel);
table.getColumnModel().getColumn(0).setCellEditor(new ComboCellEditor(items));
getContentPane().add(table);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pack();
}
private class ComboCellEditor extends AbstractCellEditor implements TableCellEditor {
private JComboBox comboBox;
public ComboCellEditor(Object[] items) {
this.comboBox = new JComboBox(items);
AutoCompleteDecorator.decorate(this.comboBox);
}
public Object getCellEditorValue() {
return this.comboBox.getSelectedItem();
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
comboBox.setSelectedItem(value);
return comboBox;
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new AutoCompleteCellEditorTest().setVisible(true);
}
});
}
}
Problem 1: Starting to 'type' into the AutoCompleteDecorate combo box doesn't cause it to start editing. You have to hit F2, it would appear. I've also noticed this behaviour with other JComboBox editors. Ideally that would be fixed too. Not sure how to do this one.
Problem 2: After editing has started (say, with the F2 key), you may start typing. If you type one of A, B, C, or D, the item appears. That's all good. Then you try to 'complete' the edit by hitting the 'enter' key... and nothing happens. The 'tab' key works, but it puts you to the next cell. I would like to make the 'enter' key stop editing, and stay in the current cell.
I found some stuff online suggesting you take the input map of the table and set the Enter key so that it does the same thing as tab. Even though that's not exactly what I desired (I wanted the same cell to be active), it didn't work anyway.
I also tried setting a property on the JComboBox that says that it's a table cell editor combo box (just like the DefaultCellEditor), but that didn't work either. I think the reason that fails is because the AutoCompleteDecorator sets isEditable to true, and that seems to stop the enter key from doing anything.
After tracing endless paths through processKeyBindings calls, I'm not sure I'm any closer to a solution. I feel like this should be a fairly straightforward thing but I'm having a fair amount of difficulty with it.
Thanks for any direction you can provide!