JComboBox CellEditor Does Not Highlight On KeyEvent Selection
Posted by admin on
October 6, 2011
Even I was confused why this problem occurred to me. I mean, I have another JComboBox cell editor used in another JTable and there was no problem with the selection highlight when I pressed either the up or down arrow key to navigate through the items.
i double checked line by line and compared my two classes to ensure that there is nothing different with the two of them. And still, the selection highlight problem persisted.
Luckily, there is a property that you can set to a JComboBox cell editor to highlight the selected item when you use the up and down arrow keys.
combobox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
If you encounter this problem, avoid debugging your code and use this property right away. It took me some time to find out the solution but the fix is pretty quick.
Donations appreciated. Every little $ helps. Or click Google +1.tags: celleditor, highlight, jcombobox, keyevent, select, selection
No Comments
JComboBox CellEditor And “component must be showing on the screen to determine its location” Error
Posted by admin on
January 28, 2010
If you use a JComboBox as a cell editor in a JTable and you get this error message “component must be showing on the screen to determine its location”, chances are your cell editor is inherited from DefaultCellEditor. Thomas Bang saved us all the trouble in trying to figure out how to make a workaround for this problem by creating a ComboBoxCellEditor class inheriting from AbstractCellEditor.
Pretty cool. This class actually did the trick.
import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.Serializable; import javax.swing.AbstractCellEditor; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.table.TableCellEditor; public class ComboBoxPaneledCellEditor extends AbstractCellEditor implements ActionListener, TableCellEditor, Serializable { private JComboBox comboBox; public ComboBoxPaneledCellEditor(JComboBox comboBox) { this.comboBox = comboBox; this.comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE); // hitting enter in the combo box should stop cellediting (see below) this.comboBox.addActionListener(this); } private void setValue(Object value) { comboBox.setSelectedItem(value); } @Override public void actionPerformed(java.awt.event.ActionEvent e) { // Selecting an item results in an actioncommand "comboBoxChanged". // We should ignore these ones. // Hitting enter results in an actioncommand "comboBoxEdited" if(e.getActionCommand().equals("comboBoxEdited")) { stopCellEditing(); } } // Implementing CellEditor @Override public Object getCellEditorValue() { return comboBox.getSelectedItem(); } @Override public boolean stopCellEditing() { if (comboBox.isEditable()) { // Notify the combo box that editing has stopped (e.g. User pressed F2) comboBox.actionPerformed(new ActionEvent(this, 0, "")); } fireEditingStopped(); return true; } @Override public Component getTableCellEditorComponent(javax.swing.JTable table, Object value, boolean isSelected, int row, int column) { setValue(value); return comboBox; } }









