Reputation: 339
I have the following code:
timeLeftFinished = false;
boolean agreed = jCheckBox1.isSelected();
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
timeLeftFinished = true;
}
};
Timer timer = new Timer(0, taskPerformer);
timer.setInitialDelay(5000);
timer.setRepeats(false);
timer.start();
while (timeLeftFinished == true && agreed == true) {
jButton3.setEnabled(true);
I would like jButton3 to be enabled if both timeLeftFinished
and agreed
are true how do i go about checking if both are true and only enabling jButton3 when Both are true ?
Side note:I'm using netbeans Gui Builder perhaps the IDE could generate the code to bind the events?
Is creating another ActionListener
to check if both are true at every second a feasible implementation ?
Upvotes: 1
Views: 113
Reputation: 347314
You need to check and monitor the distinct states of both the check box and the timer...
jCheckBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
agreed = jCheckBox1.isSelected();
updateState();
}
});
//...
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
timeLeftFinished = true;
updateState();
}
};
Timer timer = new Timer(0, taskPerformer);
//...
protected void updateState() {
jButton3.setEnabled(timeLeftFinished && agreed);
}
Remember, you are operating in an event driven environment, it is very rare that anything will occur in a linear fashion
Also, remember, the UI operates within a single thread of it's own. All interactions and modifications to the UI state are expected to occur within the context of this the thread. If you do anything that stops this thread from running (like using long running loops for example), the Event Dispatching Thread won't be able to process new events (including repaint request), which means your loop is likely to never exit and your program will appear to have hung, because, basically it has...
Upvotes: 2