DRH1469
DRH1469

Reputation: 91

Making a program wait until a button is clicked

I have a problem that probably has a simple fix but I can't seem to get it to work.

I need my program to pause or wait until the user selects either the skip button, or the positive/negative feedback button, before moving on. I assume this is going to require basic threading, but I'm not sure how to implement it. Any help will be appreacited.

The gui is displayed in a separate class(GUI) and the rest is another class. The code is sort of messy as it was coded for a Hackfest in 12 hours.

EDIT: Solved it on my own by removing button listeneers and making the variables static.

public void onStatus(Status status) {
//this is a listener from the Twitter4J class. Every time new Tweet comes in it updates.
            for (int i = 0; i <= posWords.length; i++) {

                if (status.getText().toLowerCase().contains(gui.sCrit.getText())
                        && (status.getText().toLowerCase().contains(posWords[i].toLowerCase()))) {
//If the tweet matches this criteria do the following:
                    String tweet;
                    Status tempStoreP;
                    System.out.println("Flagged positive because of " +posWords[i].toLowerCase()+" " + i);
                    tempStoreP = status;

                    tweet = tempStoreP.getUser().getName() + ":" + tempStoreP.getText() + " | Flagged as positive \n\r";
                    gui.cTweet.setText(tweet);
                    //Write to log
                    try {


                        wPLog.append("~" + tweet);


                    } catch (IOException e) {
                    }
                    //Add action listeneer to gTweet.
                //here is my problem. I want it to wait until a user has clicked one of these buttons before moving on and getting the next Tweet. It has to pause the thread until a button is clicked then it can only move on to getting another Tweet.
//the problem is that the button listener uses local variables to work. 
                        gui.gTweet.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                if (gui.pTweet.getText().equalsIgnoreCase("")) {
                                    gui.pTweet.setText("Please type a response");
                                } else if (gui.pTweet.getText().equalsIgnoreCase("Please type a response")) {
                                    gui.pTweet.setText("Please type a response");

                                } else {
                                    try {
                                        Status sendPTweet = twitter
                                                .updateStatus("@" + tempStoreP.getUser().getScreenName() + " "
                                                        + gui.pTweet.getText());

                                    } catch (TwitterException e1) {
                                    }   
                                }

                                gui.gTweet.removeActionListener(this);
                            }
                        });

                    //add Aaction listert to sTweet

                        gui.sTweet.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                try {

                                    wPLog.append("Skipped \n\r");

                                } catch (IOException e1) {
                                }
                                gui.sTweet.removeActionListener(this);
                            }
                        });


                }

Thank you for any help. On a side note, if anyone can tell me why when the button is clicked, it loops and spams people with the same message, it will be helpful. Thank you.

Upvotes: 0

Views: 1629

Answers (3)

Solomon Slow
Solomon Slow

Reputation: 27115

I assume this is going to require basic threading,

Not true at all.

You say you want your program to "wait." That word doesn't actually mean much to a Swing application. A Swing application is event driven. That is to say, your program mostly consists of event handlers that are called by Swing's top-level loop (a.k.a., the Event Dispatch Thread or EDT) in response to various things happening such as mouse clicks, keyboard events, timers, ...

In an event driven program, "Wait until X" mostly means to disable something, and then re-enable it in the X handler.

Of course, Swing programs sometimes create other threads to handle input from sources that the GUI framework does not know about and, to perform "background" calculations. In that case, "Wait until X" might mean to signal some thread to pause, and then signal it to continue its work in the X handler.

Upvotes: 0

Marcel
Marcel

Reputation: 1594

I thought about the multithreading thing and i think it isn't easy enough. If i were you i would disable all controls except the button that is to click.

Example:

JButton button = new JButton( "Test );
button.setEnabled( false );

The user won't be able to click the button until you use button.setEnabled( true );, if you just disable all controls but the button that should be fine.

Upvotes: 1

Ngima Sherpa
Ngima Sherpa

Reputation: 1555

In my understand of your problem you can use Multithreading like this:

try{
    while(!isSkipClicked){
    //your multithreading code
    }
    //code to move...
}

or you can use dispose method if you have 2 JFrame.

or you can use multiple JPanel like while skip button clicked hide pnl1 and show pnl2 using method

pnl1.setvisible(false); pbl2.setVisible(true);

Upvotes: 0

Related Questions