简体   繁体   中英

Java Swing form and cannot type text in newly added jtextfield

I was asked to change a previously made java swing form to add a JTextField which would respond to keystrokes. I added the text field to the form, tried to force focus on it with no success and even if I click on it, it does not get the focus nor lets me input any text on it. I tryed to add a key listener to the JPanel (which is the base of the form) so that when I type text, it would keep it in a string variable and dump it to the method as soon as I pressed return.

I need help to sort out what's happening. Did not post any code, because the form is copyrighted to its fullest. Really need some ideas on how to get around this problem.

Here's the part of the code where I try to add the listener.

public void actionPerformed(ActionEvent e) {
    MyButton jb = (MyButton) e.getSource();
    String buttonGetName = jb.getName();

    /* MÉTODO PARA CAPTURA DE ENTRADAS POR TECLADO / LEITOR DE COD BARRAS */
    back.addKeyListener(new KeyAdapter() {
        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                barcode.setText(sBarcode);
                System.out.println("sBarcode");
                sBarcode = null;
            } else {
                System.out.println(e.getKeyChar());
                sBarcode = sBarcode + e.getKeyChar();
            }
        }
    }); 

... some more code here that reads which button is pressed in an array of buttons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM