Java Editable JCombobox Keylistener event for Enter key
Asked Answered
Z

6

6

I have editable JCombobox and I added keylistener for combobox editor component. When user press 'Enter key' and if there is no text on the editable combobox I need to display message box using JOptinoPane. I have done necessary code in keyrelease event and it displays message as expected.

Problem is, when we get message box and if user press enter key on 'OK' button of JOptionPane, combobox editor keyevent fires again. Because of this, when user press Enter key on message box, JoptionPane displays continuously.

Any idea how to solve this?

Note that I can't use Action listener for this.

Zach answered 27/12, 2012 at 14:32 Comment(1)
Please edit your question to include an sscce that shows your current approach.Redroot
A
16

Please check if this code helps you!!!

JFrame frame = new JFrame("Welcome!!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComboBox cmb = new JComboBox();
cmb.setEditable(true);
cmb.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {

    @Override
    public void keyReleased(KeyEvent event) {
        if (event.getKeyChar() == KeyEvent.VK_ENTER) {
            if (((JTextComponent) ((JComboBox) ((Component) event
                    .getSource()).getParent()).getEditor()
                    .getEditorComponent()).getText().isEmpty())
                System.out.println("please dont make me blank");
        }
    }
});
frame.add(cmb);

frame.setLocationRelativeTo(null);
frame.setSize(300, 50);
frame.setVisible(true);

Most people find it difficult because of this casting.

Arsenal answered 16/6, 2013 at 8:1 Comment(2)
Would be easier to understand with a few variables in the middle. Especially with all the casting.Fore
writing ((JTextComponent) cmb.getEditor().getEditorComponent()).getText().isEmpty() in the event listener would have added some readability, anyway, good solution, helped me a lot!Organelle
A
4

We need to add a key listener on the component that the combo box is using to service the editing.

JTextComponent editor = (JTextComponent) urCombo.getEditor().getEditorComponent();
editor.addKeyListener(new KeyAdapter() {
   public void keyReleased(KeyEvent evt) {
      // your code
   }
});

Hope this code helps.

Altheaalthee answered 29/3, 2015 at 19:6 Comment(0)
C
1
Note that I can't use Action listener for this.

this doesn't make me any sence, then to use ItemListener

Any idea how to solve this?
  • never to use KeyListener for Swing JComponents, use (Note that I can't use Action listener for this.) KeyBindings instead,

  • notice ENTER key is implemented for JComboBox in API by default, have to override this action from ENTER key pressed

Conformance answered 27/12, 2012 at 15:30 Comment(0)
G
0

One option would be to replace the KeySelectionManager interface with your own. You want to replace the JComboBox.KeySelectionManager as it is responsible for taking the inputted char and returns the row number (as an int) which should be selected.

Gabie answered 14/2, 2013 at 1:17 Comment(0)
F
0

Please check the event ascii code by ev.getkeycode() and check if it is a number or character. If it is neither a number nor a character do nothing. If it is what you want then do the process.

Foeticide answered 18/9, 2016 at 6:47 Comment(0)
A
0

If you are using Netbeans then right click on your combobox and select customize code. add following lines of code

JTextComponent editor = (JTextComponent) Code.getEditor().getEditorComponent();
editor.addKeyListener(new KeyAdapter() {
   public void keyReleased(KeyEvent evt) {
        if(evt.getKeyCode()==10)
            //do your coding here.
   }
});
Avigation answered 19/12, 2019 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.