简体   繁体   中英

JTextField Won't accept Input

So I made a test window with a JTextField in it. And I cannot tell whats going wrong. The main code is down below. The problem is that no matter what I do, I cannot edit the text field nor the second one I made. I have a sample program with a text field that works as well, yet it doesn't work at all.

I'm not sure if I need to post it, but I can get a sample jar of the complete program up here. I only posted the area that handles the text Fields

EDIT: The full source is available here: GITHUB

I removed something and it worked, I give up...\\

EDIT2: It turns out it was the fact I was calling a class that extended JPanel, simply calling a new JPanel instead of extending it worked

EDIT3: Ok, the problem was the key event dispatcher, the post I marked as the answer explains it in-depth

 public class Main {
    private static JPanel mainPanel = new JPanel();
    private static JFrame frame;
    public static JTextField textField1 = new JTextField();
    public static JTextField textField2 = new JTextField();
    private static GroupLayout layout = new GroupLayout(mainPanel);

    public static void main(String[] Args) throws InterruptedException{
        frame = new JFrame("Test Window");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainPanel.setLayout(layout);
        layout.setAutoCreateContainerGaps(false);
        layout.setAutoCreateGaps(false);
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        vGroup.addGap(200).addGroup(layout.createParallelGroup().addComponent(textField1, 25, 25, 25).addComponent(textField2, 25, 25, 25).addGap(350));
        hGroup.addGap(300)
        .addGroup(layout.createParallelGroup().addComponent(textField1, 200, 200, 200).
                addComponent(textField2, 200, 200, 200)).addGap(300);
        layout.setVerticalGroup(vGroup);
        layout.setHorizontalGroup(hGroup);
        frame.add(mainPanel);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
        textField1.setText("I am a simple uneditable testbox");
    }
}

Your problem is as I suspected the KeyEventDispatcher. When you add it back, and have it return true the JTextField does not work. Per the KeyEventDispatcher API :

If an implementation of this method returns false, then the KeyEvent is passed to the next KeyEventDispatcher in the chain, ending with the current KeyboardFocusManager. If an implementation returns true, the KeyEvent is assumed to have been dispatched (although this need not be the case), and the current KeyboardFocusManager will take no further action with regard to the KeyEvent.

import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class Main {
   private static JPanel mainPanel = new JPanel();
   private static JFrame frame;
   public static JTextField textField1 = new JTextField(20);
   public static JTextField textField2 = new JTextField(20);

   public static void main(String[] Args) throws InterruptedException {
      frame = new JFrame("Test Window");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

      final JCheckBox dispatchKeyEventReturnCheckBox = 
            new JCheckBox("Dispatch Key Event Return Value", true);

      mainPanel.add(textField1);
      mainPanel.add(textField2);
      mainPanel.add(dispatchKeyEventReturnCheckBox);
      frame.add(mainPanel);

      KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .addKeyEventDispatcher(new KeyEventDispatcher() {

               @Override
               public boolean dispatchKeyEvent(KeyEvent evt) {
                  // TODO Fix this!!!
                  // !! return false;
                  return dispatchKeyEventReturnCheckBox.isSelected();
               }
            });

      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      textField1.setText("I am a simple uneditable testbox");
   }
}

Solution: 1) don't have your dispatchKeyEvent(KeyEvent e) return true, unless you do not want the GUI to handle the key stroke. Or 2) even better , don't use this class. Instead tell us why you feel you need it, and let's help you find a better way.

1+ to your question for trying to create and post an MCVE.

Don't use Thread.sleep() that sometime hangs the whole swing application instead try with Swing Timer that is most suitable for swing application.

Read more How to Use Swing Timers

Sample code:

private Timer timer;
...

// wait for 10 milli-seconds
timer = new javax.swing.Timer(10, new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // next call
    }
});
timer.setRepeats(true); // you can turn off reputation
timer.start();

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