fets
fets

Reputation: 21

return a value from a JFrame to main()

I am writing a program which has a JFrame with a JMenu in it. Once the user clicks a menuItem, a JDialog is being called, to get a String from the user. I want to use that string in my main programm but i don't know how to return that value from the JFrame to the main programm (I managed to return the value from the JDialog to the JFrame). Any ideas?

My main::

public static void main(String[] args)
{
    myFirstFrame m = new myFirstFrame();

    m.setVisible(true);

    String localhost = m.getLh();

    System.out.println(localhost);
}

My JFrame::

public class myFirstFrame extends JFrame
{
    String lh;

    myDialog myD;

    public myFirstFrame(JFrame mf)
    {
        super();
        setTitle("Welcome");
        setSize(300, 300);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        JMenuItem playg = new JMenuItem("Play game");
        simetoxi.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                myD = new myDialog(myFirstFrame.this);
                myD.setVisible(true);

                String lh = myD.getText();
                System.out.println(lh + "ASasASas");
                dispose();
            }

        });

        JMenu game = new JMenu("GAME");
        game.add(playg);
        JMenuBar myBar = new JMenuBar();
        myBar.add(game);
        setJMenuBar(myBar);
    }

    public String getLh()
    {
        return lh;
    }
}

My JDialog::

public class myDialog extends JDialog
{
    JTextField t1;

    String sname;

    public myDialog(JFrame fr)
    {
        super(fr, true);
        setTitle("Connect");
        setSize(200, 200);
        setLayout(new FlowLayout());
        JLabel l1 = new JLabel("Give the server name");
        t1 = new JTextField(15);
        add(l1);
        add(t1);
        JButton okb = new JButton("submit");
        okb.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                sname = t1.getText();
                dispose();
            }
        });
        add(okb);
    }

    public String getText()
    {
        return sname;
    }
}

Upvotes: 0

Views: 4923

Answers (2)

laune
laune

Reputation: 31300

The main method will not wait for your JFrame to go through those steps, so calling a getter in your main program (even if you "correct"

 String lh = myD.getText();

to

 lh == myD.getText();

will not work. - Pass this information to a class/method that makes good use of it, perhaps processing it in a separate thread - depends of what you want to do with "localhost".

Upvotes: 0

Alex D
Alex D

Reputation: 30455

The problem is that when main creates the Frame, it doesn't stop and wait for the value to become available before executing the rest of the main function.

There are many ways you could solve this problem. For example, rather than putting System.out.println(localhost) in main, you could put it in a different method. Then, in the Frame, call that method when you get the value.

If you really want to put that call in main, you will have to find some way to make main block until the value is available. For example, you could create a BlockingQueue, and try to dequeue the value from within main. In the Frame event handler, push the needed value onto the queue.

Upvotes: 2

Related Questions