holding key breaks java swing form

63 Views Asked by At

I am working on a basic Java Swing UI application, which seems to be working correctly, except for an issue I've noticed when using the form I created.

The problem is when I am typing in the text boxes, holding down a key seems to break the form--ie, after taking this action, the text boxes no longer seem to accept any input. I find it hard to believe this is a released issue with Java Swing components, but I also can't see how it would be related to my code. Has anyone seen a similar issue before? I am using OS x with Intellij IDEA if that is relevant.

My form is defined thusly:

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;


@SuppressWarnings("serial")
public class LoginDialogMcve extends JFrame {

  protected JTextField stringEntry, dateEntry;

  public LoginDialogMcve() {
    super("Create Textbox");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());

    this.stringEntry = new JTextField(5);
    add(this.stringEntry);

    this.dateEntry = new JTextField(5);
    add(this.dateEntry);

  }

  public static void main(String... args) {
    LoginDialogMcve me = new LoginDialogMcve();
    me.pack();
    me.setLocationByPlatform(true);
    me.setVisible(true);
  }
}

Edit: Thanks for the feedback, this is quite possibly not a Java problem. Could have something to do with OSx... Uploaded simpler example with mcve

1

There are 1 best solutions below

1
DontKnowMuchBut Getting Better On

My MCVE that works fine. Test it yourself to see.

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
// import net.miginfocom.swing.MigLayout;

@SuppressWarnings("serial")
public class LoginDialogMcve extends JFrame {

    protected JTextField stringEntry, dateEntry;
    protected JLabel stringEntryLabel, dateEntryLabel;
    protected JButton print;
    protected Action validateAction;

    public LoginDialogMcve() {
        super("Create Textbox");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        // setLayout(new MigLayout("ins 10, gap 5",
        // "[][grow]",
        // "[][][]"));

        this.stringEntryLabel = new JLabel("Name:");
        add(this.stringEntryLabel);

        this.stringEntry = new JTextField(5);
        add(this.stringEntry);

        this.dateEntryLabel = new JLabel("Date:");
        add(this.dateEntryLabel);

        this.dateEntry = new JTextField(5);
        add(this.dateEntry);

        this.validateAction = new MyAction();

        this.print = new JButton(this.validateAction);
        add(this.print);

        getRootPane().getActionMap().put("validate", this.validateAction);
    }

    public static void main(String... args) {
        LoginDialogMcve me = new LoginDialogMcve();
        me.pack();
        me.setLocationByPlatform(true);
        me.setVisible(true);
    }

    public class MyAction extends AbstractAction {

        public MyAction() {
            super("Validate");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // lots of irrelevant code
        }

    }

}