Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import javax.swing.JComboBox;
- import javax.swing.JList;
- import javax.swing.JTextField;
- import javax.swing.plaf.basic.ComboPopup;
- /**
- * This is a quick fix to change the behavior of a JComboBox in a JTable.
- *
- * In a normal editable JComboBox the user is able to select entries with the arrow keys.
- * On each change the value of the JTextField (editorComponent) changes as well and you can
- * leave the ComboBox with TAB.
- *
- * In a JTable this described behavior changes. The value of the JTextField does not change
- * when a different value in the list gets selected. So the user is forced to press the
- * enter key in order to save the value.
- *
- * This ClientProperty triggers this change.
- * combobox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
- *
- * I can give no warranty that this will work in every case. But for me it simply did it.
- *
- * If you have any suggestions please write me a mail to wolfgang.jentner[at]sfg-singen.de
- *
- * @author Wolfgang Jentner
- *
- */
- public class ComboBoxTableFix {
- /**
- * This is a decorator method. Simply put your JComboBox in.
- *
- * @param combobox - An editable JComboBox.
- */
- public static void fix(JComboBox combobox) {
- if(!combobox.isEditable()) return;
- final JList c_list = ((ComboPopup) combobox.getUI().getAccessibleChild(combobox, 0)).getList();
- final JTextField c_field = (JTextField) combobox.getEditor().getEditorComponent();
- c_field.addKeyListener(new KeyListener() {
- @Override
- public void keyTyped(KeyEvent e) {}
- @Override
- public void keyReleased(KeyEvent e) {
- if(e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_UP) {
- Object val = c_list.getSelectedValue();
- if(val != null) {
- c_field.setText(String.valueOf(val));
- }
- c_field.selectAll();
- }
- }
- @Override
- public void keyPressed(KeyEvent e) {}
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement