简体   繁体   中英

Return a String from a JButton press Action Listener?

I've been trying for a while to get a JButton I have to return a string and I have it working to an extent however it only works when i'm using the System.out.println() method.

My Relevant Code:

private String answer;

public void inputDataButton(JButton inButton)
{
inButton.addActionListener(new Action1());
}

public String returnAnswer()
{
return answer;
}

private void fileAnswer(String inString)
{
answer = new String(inString);
}

public class Action1 implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        String sub = JOptionPane.showInputDialog("Input The Date");
        fileAnswer(sub);
    }
}

With my Main controlling it:

public class test
{
    protected static JTextField text;

    public static void main(String[] args)
    {
    javaUI base = new javaUI("Date Killer", 800, 800);

    JLabel label = new JLabel("Place Holder");
    base.addLabel(label, 1, 0, false);

    JButton button = new JButton("Press Here");
    base.addButton(button, 0, 0, false);

    String str = "EMPTY";

    base.inputDataButton(button);

    while (str == "EMPTY")
        {
        str = base.returnAnswer();
        }

    System.out.println(str + " TEST");
    label.setText(str + "SETTED");
    }
}

JavaUI is simply another class with simplifies the entire JFrame and Jpanel setup for Labels, Buttons etc.

Anyways, heres my problem. In the main class, in the while statement, str is successfully set to the string if I have a System.out.println() statement directly after. This obviously makes quite a mess of the terminal as it repeates "EMPTY" 100+ times until the button is pressed.

However if I remove that statement, nothing is obviously printed out, but str is never set to the str either.

I've been messing around with this for quite a while (New to all java UI stuff, mainly just worked in the calculations part of things) and I've yet to find a working solution that doesn't make a mess of my terminal. Thanks for all the Help!!!

Cail

1) It's a little strange to use a loop to "wait" for the action to happen. You can watch your cpu usage and it could be dead if it loops forever.

2) Also, the method will return no matter whether your button is clicked -- Before the action happens, the method returns a null string according to your code. And in your main method, str == "EMPTY" soon becomes false after the first round. Then you'll see "null TEST" on your screen.


Try to put the code after while loop into actionPerformed method or fileAnswer method and delete the loop:

public void actionPerformed(ActionEvent e)
{
    String sub = JOptionPane.showInputDialog("Input The Date");
    System.out.println(sub + " TEST");
    label.setText(sub + "SETTED");
}

Also, I am not sure why you call answer = new String(inString) . If you want to assign it to another variable, you only need to call answer = inString since String is unmodifiable. And in this case, you don't to do this.

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