0

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2742399
  • 61
  • 2
  • 6
  • What do you want to happen after the button is clicked? – Code-Apprentice Apr 13 '14 at 21:59
  • Also, please reformat your code with consistent indentation. It will help others be able to read it. – Code-Apprentice Apr 13 '14 at 21:59
  • @Code-Guru : after button is clicked , it should store the string in s1(of GUI) , and then I want to take the s1 value by obj.s1(from Client) . But the problem is the value of obj.s1 is comming as ""(as I initialized in GUI class). – user2742399 Apr 13 '14 at 22:02
  • You either need a blocking function ... or a callback as described in the answer below. Tip - use a callcack, aka event handlers. There is plenty of reading material around the net. There are even design patterns for this kind off stuff .... Gang of four. Anywho - lookup callbacks and events in Java and that'll get you where you need to go. – Travis Sharp Apr 13 '14 at 22:16

3 Answers3

2

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.

Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
1

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

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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

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);
        }
    };
}
Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76