Shaakir
Shaakir

Reputation: 484

java showMessageDialog enter text input focus

I'm writing a small app that uses barcode scanner as input. After some testing i settled on the evt.getKeyCode() == KeyEvent.VK_ENTER to fire off checks to the DB. So the user can either enter barcode manually in the textbox, and press enter to fire off DB checks, or use the scanner, which also automatically sends enter character once all scanned characters has been sent.

if there is a problem with the scanned barcode, I show a error message to the user using JOptionPane.showMessageDialog. When the user presses enter button to dismiss the dialog, then it fires off the dialog again, as if the enter button has been pressed again on the text field.. it goes into this loop until the user clicks the OK button with the mouse.

How do i avoid this...

is there a better way to check if scanner completely send all it's characters and start DB checks than using the enter button

OR

is there a way for the text box to loose focus for a brief moment when the dialog showing and return focus back to it when dialog closes (user presses enter / clicks on OK / clicks on little 'x').

I have managed to use the hasFocus() to return focus to the text box, but since i only have a text box and Jtable on the frame i'm a bit limited. I tried to setfocus to the Jtable before showing the dialog as a workaround, then to refocus to textbox , but the problem still persists. Even tried to set focus to the frame, still no luck.

any ideas please?

Upvotes: 0

Views: 786

Answers (3)

ControlAltDel
ControlAltDel

Reputation: 35011

Your focus idea won't work. But since you haven't published any code, all I can offer is this kludge.

public static class MyKeyListener extends KeyAdapter {
  private changed = false;
  public void keyPressed (KeyEvent ke) {
    if (ke.getKeyCode() == VK.ENTER) {
      if (changed) runIt(); // submit to db
    } else {
      changed = true;
    }
  }
}

Upvotes: 0

Shaakir
Shaakir

Reputation: 484

after some more testing i found an alternative.

i did a simple app with just a text field.

previously i had :

private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {
    if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
        JOptionPane.showMessageDialog(null, "Enter button pressed");
    }
}

This would lead to a loop of dialog showing and closing if the enter button was used to dismiss the dialog.

I changed this to :

private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
    if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
        JOptionPane.showMessageDialog(null, "Enter button pressed");
    }
}

THis solved the problem. No more loop.

I assume that the OK button on the dialog is pressed on Keydown event, which closes the dialog, hands focus back to the text field, then by the few milliseconds i held the enter key , the text box now has focus and thus fires the KeyReleased event as i lift my finger.

Works for the scanner as well.

thanks for the all the suggestion.

Upvotes: 0

user1803551
user1803551

Reputation: 13407

Here is an example using key bindings:

public class EnterKey extends JFrame {

    EnterKey() {

        JPanel base = new JPanel();
        base.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "Scan!");
        base.getActionMap().put("Scan!", new ScanAction());
        base.getInputMap().put(KeyStroke.getKeyStroke("A"), "Simulate error");
        base.getActionMap().put("Simulate error", new ErrorAction());

        add(base);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {

        new EnterKey();
    }

    private class ScanAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent e) {

            //perform scan
            System.out.println("Scan performed");
        }
    }

    private class ErrorAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent e) {

            //open error dialog
            JOptionPane.showMessageDialog(EnterKey.this, "Error", "Bad scan", JOptionPane.ERROR_MESSAGE);
        }
    }
}

The frame just holds an empty panel to receive key events. Press Enter to initiate a scan or a to pop an error dialog. If you dismiss the dialog with Enter it will not initialize a scan unless you keep the key pressed (duration depends on the OS).

Upvotes: 1

Related Questions