Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 06-02-2013, 08:21 PM   #11
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java btn.getclicked?

Quote:
Originally Posted by jpmasta12666 View Post
so how can i write if(btnAnswer1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblAnswer.setText("correct");
}
}
});
You're seriously misunderstanding what's going on here - I'd seriously advise you to have a look in detail about swing, the events framework and the observer design pattern at a minimum, otherwise you're going to be hugely struggling from here on through misunderstanding.

The piece of code I gave you essentially calls the actionPerformed() method (where you write your setText) when that button is clicked. So whatever code you put there will execute whenever a user clicks the button. The concept here isn't really one of "if the button is clicked, do this" it's more "register the following piece of code to execute whenever the button is clicked." As for why it needs to be done this way, think of the control flow of your program. If you had something like:

Code:
lblQuestion.setText("What is a jump on a skateboard called?");
btnAnswer1.setText("Ollie");
if(btnAnswer1.isClicked()) {
      //Do something here when button is clicked
}
btnAnswer2.setText("Face plant");
btnAnswer3.setText(" Jump");
...as you originally proposed, then the code would execute, probably not execute the if statement (unless the user happened to be clicking on the button at the exact time that if statement was evaluated) and then carry on. This is why we need the concept of events, that switch from the concept of "if the button is pressed down now then do this" to "when the button is pressed, whether that's now or some time in the future, then execute this block of code."

Of course, the above pattern isn't baked in at language level, it's actually achieved by using a combination of other language level features that allow it to happen - but for now, just try and grasp the concept.
__________________

__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 06-02-2013, 09:09 PM   #12
Baseband Member
 
Join Date: Jan 2013
Location: Canada
Posts: 26
Default Re: Java btn.getclicked?

Ok, I sorta get this better now Ill research some more. Sorry for the inconvenience. I'm a beginner at learning this and don't understand everything the best
__________________

jpmasta12666 is offline   Reply With Quote
Old 06-02-2013, 09:24 PM   #13
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java btn.getclicked?

Quote:
Originally Posted by jpmasta12666 View Post
Ok, I sorta get this better now Ill research some more. Sorry for the inconvenience. I'm a beginner at learning this and don't understand everything the best
No problem - just pointing you in the right direction
__________________

__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 09:20 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0