JTextField vs JComboBox behaviour in JTable
Posted
by
Ash
on Stack Overflow
See other posts from Stack Overflow
or by Ash
Published on 2012-10-05T03:35:54Z
Indexed on
2012/10/05
3:37 UTC
Read the original article
Hit count: 304
Okay, this is a hard one to explain but I'll try my best.
I have a JTextField and a JComboBox in a JTable, whose getCellEditor method has been overriden as follows:
public TableCellEditor getCellEditor( int row, int column ) {
if ( column == 3 ) {
// m_table is the JTable
if ( m_table.getSelectedRowCount() == 1 ) {
JComboBox choices = new JComboBox();
choices.setEditable( true );
choices.addItem( new String( "item 1" ) );
return new DefaultCellEditor( choices );
}
return super.getCellEditor( row, column );
}
Here are the behavioral differences (NOTE that from this point on, when I say JTextField or JComboBox, I mean the CELL in the JTable containing either component):
When I click once on a JTextField, the cell is highlighted. Double clicking brings up the caret and I can input text. Whereas, with a JComboBox, single clicking brings up the caret to input text, as well as the combo drop down button.
When I tab or use the arrow keys to navigate to a JTextField and then start typing, the characters I type automatically get entered into the cell. Whereas, when I navigate to a JComboBox the same way and then start typing, nothing happens apart from the combo drop down button appearing. None of the characters I type get entered unless I hit F2 first.
So here's my question: What do I need to do have JComboBoxes behave exactly like JTextFields in the two instances described above?
Please do not ask why I'm doing what I'm doing or suggest alternatives (it's the way it is and I need to do it this way) and yes, I've read the API for all components in question....the problem is, it's a swing API.
Thanks in advance, Ash
© Stack Overflow or respective owner