How to wait for input in a text field?
Asked Answered
K

2

6

I'm converting a console application to one that uses Swing. At the moment I want my program to do a similar thing to this .nextInt(); how can I achieve this by using .getText(); or something similar?

In short;

How can I hold the execution of the program till the user has entered something in the text field and pressed enter.

Kentledge answered 29/8, 2011 at 12:3 Comment(1)
still don't understand the question - what do you mean by "hold execution"? Apart from that: read a basic Swing tutorial and textfield's api doc (hint: setAction might be worth a look :-)Lever
L
5

Update: So you want to wait for the user to to input something from the GUI. This is possible but needs to be synchronized since the GUI runs in another thread.

So the steps are:

  1. Create an "holder" object that deligates the result from GUI to "logic" thread
  2. The "logic" thread waits for the input (using holder.wait())
  3. When the user have entered text it synchronizes the "holder" object and gives the result + notifies the "logic" thread (with holder.notify())
  4. The "logic" thread is released from its lock and continues.

Full example:

public static void main(String... args) throws Exception {
    final List<Integer> holder = new LinkedList<Integer>();

    final JFrame frame = new JFrame("Test");

    final JTextField field = new JTextField("Enter some int + press enter");
    frame.add(field);
    field.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            synchronized (holder) {
                holder.add(Integer.parseInt(field.getText()));
                holder.notify();
            }
            frame.dispose();
        }
    });

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

    // "logic" thread 
    synchronized (holder) {

        // wait for input from field
        while (holder.isEmpty())
            holder.wait();

        int nextInt = holder.remove(0);
        System.out.println(nextInt);
        //....
    }
}
Lanford answered 29/8, 2011 at 12:7 Comment(2)
@DCON I have no idea why I was doing this all those years ago, but I'm glad my (hopefully former) incompetence helped someone.Kentledge
If it helps, I don't even know why I was doing this just 6 months ago!Springhalt
K
0

Console application and GUI application are quite different in the behaviour. Console application takes input from command line arguments or wait for user entered input from keyboard while GUI application is driven by event mechanism in order to perform a task.

For example, you add a TextField object to your Frame and add a keyListener to your text field object. The listener is invoked when the key event has ben notified. There are lots of example out there, e.g. official java example http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html

Koon answered 29/8, 2011 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.