user3743122
user3743122

Reputation: 1

Unresponsive JTextField

In my application I need to get a response between 1 and 9 from the user. I am using a JTextField to get the input. However when a button is pressed the user the JTextField becomes unresponsive. Here is a stripped down example of the problem:

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;

public class InputTest {
    private JTextField inputBox;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InputTest();
        }
    });
    } // end main()

    public InputTest() { // constructor
    JFrame f = new JFrame("Input Test");
    f.setBounds(100, 100, 450, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(null);

    JLabel label = new JLabel("Enter an integer:");
    label.setFont(new Font("Tahoma", Font.PLAIN, 14));
    label.setBounds(109, 120, 109, 30);
    f.add(label);

    inputBox = new JTextField();
    inputBox.setBounds(259, 120, 30, 30);
    f.add(inputBox);

    JButton button = new JButton("Button");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Button Pressed");
        }
    });
    button.setMnemonic('B');
    button.setBounds(166, 198, 78, 23);
    f.add(button);
    f.setVisible(true);

    inputBox.addKeyListener(new KeyAdapter() {
        public void keyTyped(KeyEvent e) {
            char c = e.getKeyChar();
            if (c < '!' || e.getModifiers() > 0)
                return;
            if (c < '1' || c > '9') {
                JOptionPane.showMessageDialog(null, c
                        + " is not an integer");
                // inputBox.setText("error");
                System.out.println("You typed a non-integer");
            } else
                System.out.println("You typed " + c);
            inputBox.setText(null);
        }
    });
} // end constructor
} // end class

Upvotes: 0

Views: 92

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285450

You should almost never use a KeyListener with a Swing JTextComponent such as a JTextField, for example what happens when the user tries to copy and paste in their input? Instead consider using either a JFormattedTextField, or a JTextField with a DocumentFilter, or my choice -- a JSpinner.


import java.awt.event.ActionEvent;
import javax.swing.*;

public class InputTest2 extends JPanel {
   private JSpinner spinner = new JSpinner(new SpinnerNumberModel(1, 1, 9, 1));
   private JButton getSelectionButton = new JButton(new GetSelectionAction("Get Selection"));

   public InputTest2() {
      add(spinner);
      add(getSelectionButton);
   }

   private static void createAndShowGui() {
      InputTest2 mainPanel = new InputTest2();

      JFrame frame = new JFrame("InputTest2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }

   private class GetSelectionAction extends AbstractAction {
      public GetSelectionAction(String name) {
         super(name);
      }

      @Override
      public void actionPerformed(ActionEvent evt) {
         int value = ((Integer)spinner.getValue()).intValue();
         System.out.println("You've selected " + value);
      }
   }

}

Upvotes: 2

Related Questions