Alph Main
Alph Main

Reputation: 21

Running a GUI client multiple times without having to close it

I'm in the process of making a GUI where I input a string into a text box and, once I click a Jbutton, a second text box will produce the string I inputted into the first or produce a random string from a method I created (public void associate()). When I run the GUI and click the button to produce the text in the second text box, everything works fine. However, when I click the button a second time so the GUI performs the same action, nothing happens. Is there anything I can do so that I don't have to close the GUI every time I wish to run it multiple times?

public class GUIWindow extends JFrame {
private Joketeller robot= new Joketeller();
private JLabel speakerlabel = new JLabel("Joke");
private JLabel MarcoLabel= new JLabel ("Marco");
private JTextField speakerfield= new JTextField ("Enter Joke Here");
private JTextField Marcofield= new JTextField ("",20);
private JButton Jokebutton=new JButton("Recite Joke >>>");

public GUIWindow()  {
    JPanel dataPanel= new JPanel(new GridLayout(2,2,12,16));
    dataPanel.add(speakerlabel);
    dataPanel.add(MarcoLabel);
    dataPanel.add(speakerfield);
    dataPanel.add(Marcofield);

    JPanel buttonPanel= new JPanel();
    buttonPanel.add(Jokebutton);
    Container container = getContentPane();
    container.add(dataPanel,BorderLayout.CENTER);
    container.add(buttonPanel,BorderLayout.SOUTH);
    Jokebutton.addActionListener(new JokeListener());
}

    private class JokeListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        String input=speakerfield.getText();
        if (Jokebutton.isEnabled()) {
        robot.setJoke(input);
        String Response= robot.getResponse();
        Marcofield.setText(Response);}

Joketeller class:

public class Joketeller {


    private static String Marco;
    private static String Response;
    static int i= (int)(Math.random()*((5-1)+1)+1);
    static String r;

    public void setMarco(String Joke ) {
        Marco=Joke;
    }

    public void setJoke(String Joke) {
        Marco=Joke;
        associate();

    }


    public String getJoke() {
        return Marco;
    }

    public static String getMarco() {
        return Marco;
    }

        public static void associate(){
        if(i==1) 
            r= "Connect Angie";
        else if(i==2)
            r= "*Cloud Laugh*";
        else if(i==3)
            r= "Community";
        else if(i==4)
            r=getMarco();
        else if(i==5)
            r= "Indeed!";
        Response=r;

        }

    public String getResponse() {
        return Response;
    }

    }

Any help is appreciated. Thank you.

Upvotes: 1

Views: 83

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

The first thing that jumps out at me, is the overuse of static...

public class Joketeller {


    private static String Marco;
    private static String Response;
    static int i= (int)(Math.random()*((5-1)+1)+1);
    static String r;

This is not helping your here, and if done right, shouldn't be needed.

The next issue is with i...

    static int i = (int) (Math.random() * ((5 - 1) + 1) + 1);

    public static void associate() {
        if (i == 1) {
            r = "Connect Angie";
        } else if (i == 2) {
            r = "*Cloud Laugh*";
        } else if (i == 3) {
            r = "Community";
        } else if (i == 4) {
            r = getMarco();
        } else if (i == 5) {
            r = "Indeed!";
        }
        Response = r;

    }

i is never changed. Because it's static, you can create as many instance of Joketeller as you like and it will need change, so the response will always be the same.

While there are a number of possible ways to fix it, the simplest would be to remove all the static and make i a local variable within associate, as it's really not used anywhere else..

public void associate() {
    int rnd = (int) (Math.random() * ((5 - 1) + 1) + 1);
    if (rnd == 1) {
        r = "Connect Angie";
    } else if (rnd == 2) {
        r = "*Cloud Laugh*";
    } else if (rnd == 3) {
        r = "Community";
    } else if (rnd == 4) {
        r = getMarco();
    } else if (rnd == 5) {
        r = "Indeed!";
    }
    response = r;

}

This means you don't need to create a new instance of Joketeller in order to get a different response.

For example....

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GUIWindow extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                GUIWindow wnd = new GUIWindow();
                wnd.pack();
                wnd.setLocationRelativeTo(null);
                wnd.setVisible(true);
            }
        });
    }

    private Joketeller robot = new Joketeller();
    private JLabel speakerlabel = new JLabel("Joke");
    private JLabel marcoLabel = new JLabel("Marco");
    private JTextField speakerfield = new JTextField("Enter Joke Here");
    private JTextField marcofield = new JTextField("", 20);
    private JButton jokebutton = new JButton("Recite Joke >>>");

    public GUIWindow() {
        JPanel dataPanel = new JPanel(new GridLayout(2, 2, 12, 16));
        dataPanel.add(speakerlabel);
        dataPanel.add(marcoLabel);
        dataPanel.add(speakerfield);
        dataPanel.add(marcofield);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(jokebutton);
        Container container = getContentPane();
        container.add(dataPanel, BorderLayout.CENTER);
        container.add(buttonPanel, BorderLayout.SOUTH);
        jokebutton.addActionListener(new JokeListener());
    }

    private class JokeListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String input = speakerfield.getText();
            if (jokebutton.isEnabled()) {
                robot.setJoke(input);
                String Response = robot.getResponse();
                marcofield.setText(Response);
            }
        }
    }

    public class Joketeller {

        private String marco;
        private String response;
        private String r;

        public void setMarco(String Joke) {
            marco = Joke;
        }

        public void setJoke(String Joke) {
            marco = Joke;
            associate();

        }

        public String getJoke() {
            return marco;
        }

        public String getMarco() {
            return marco;
        }

        public void associate() {
            int rnd = (int) (Math.random() * ((5 - 1) + 1) + 1);
            if (rnd == 1) {
                r = "Connect Angie";
            } else if (rnd == 2) {
                r = "*Cloud Laugh*";
            } else if (rnd == 3) {
                r = "Community";
            } else if (rnd == 4) {
                r = getMarco();
            } else if (rnd == 5) {
                r = "Indeed!";
            }
            response = r;

        }

        public String getResponse() {
            return response;
        }

    }

}

Upvotes: 2

Related Questions