简体   繁体   中英

How to pause program having main() until a button from GUI is pressed?

I am new at Java Swing. I have two Java files. One having main() in it and the other is the GUI file.

Client

class Client
{
    GUI gui;
    public static void main(String args[])
    {
        //.......... do something.......
        gui = new GUI();
        // at thin point I want to have value of gui.s1 ....
        //but main() actually do not wait for the user input.
    }
}

GUI

class GUI extends JFrame implements ActionListener
{
    String s1="";

    GUI()
    {
        JTextField t1= new JTextField(20);
        JButton j1= new JButton("submit");
        j1.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {         
        s1=t1.getText();
    } 
}

Please guide me, and if it is not appropriate question then please suggest me the article that you think I should read to get the concept.

Right now I'm at phone so I can't help you with code I will try to let you understand the concept: An user input, a button click is something which vould happen after 5 seconds like could happen after 30 minutes. So yes, you could let sleep the main for sometimes and hope for an input, wait until .s1 get a value and etc.

But, I don't see it like the right thing to do here. The best thing which could be used is a callback which is called when the user click the button. It's done using interfaces.

Well, first you declare an interface maybe named OnRequestClick where you implement onRequestClick(String message); method.

Message will be the text of s1.

Now in the GUI class create a new field of type OnRequestClick named listener and take it in your constructor.

Now where you create the GUI object the compiler ask to you to provide a code for OnRequestClick so do it and it willbe the code which will be executed when the user press tbe button.

Well, righr now what I said is false: it doesn't get fired since we didn't have done any call to listener.onRequestClick ()

So in your actionPerformed add listener.onRequestClick (s1.getText ()); so in your main you will get the ebemt and the text.

Replace GUI with a JOptionPane.showInputDialog(..) and not only will the code be a lot shorter, but the problem will be solved. EG

import javax.swing.*;

class UserInput {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                String name = JOptionPane.showInputDialog(null, "Name?");
                if (name==null) {
                    System.out.println("Please input a name!");
                } else {
                    System.out.println("Name: " + name);
                }
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

You can use a Callback Mechanism .

I have already posted a sample code here JFrame in separate class, what about the ActionListener? . Please have a look.

interface Callback {
    void execute(String value);
}

abstract class GUI extends JFrame implements ActionListener, Callback{
     ...
     // do not provide the implementation of `execute` method here

     ...
     public void actionPerformed(ActionEvent e) {
        s1 = t1.getText();
        // now callback method is called
        execute(s1);
     }
}

Your main class will look like:

public static void main(String args[]) {
    gui = new GUI() {

        @Override
        public void execute(String value) {
            System.out.println("Entered value:" + value);
        }
    };
}

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