• Post Reply Bookmark Topic Watch Topic
  • New Topic

JButton actionListener

 
Al Fraelich
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a program that asks the user a series of yes/no questions. I created two JButtons, one "yes" and one "no". Each button has it's specified actionListener associated with it, and when the user selects yes or no, it asks a different question depending on the previous selection. Here is my problem, I would like to re-use the JButtons "yes" and "no", but the actionListener that is used for the first question performs a different action each subsequent question. I have researched this and I have found the "removeActionListener()" but have not found out how to use it or if this is even the correct way. so short of creating two new JButtons for each question, I am stumped. Any help on this would be appreciated.

Thanks alot
Al Fraelich
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Al Fraelich wrote:I'm writing a program that asks the user a series of yes/no questions. I created two JButtons, one "yes" and one "no". Each button has it's specified actionListener associated with it, and when the user selects yes or no, it asks a different question depending on the previous selection. Here is my problem, I would like to re-use the JButtons "yes" and "no", but the actionListener that is used for the first question performs a different action each subsequent question. I have researched this and I have found the "removeActionListener()" but have not found out how to use it or if this is even the correct way. so short of creating two new JButtons for each question, I am stumped. Any help on this would be appreciated.

Thanks alot
Al Fraelich


Well, presumably you can have a variable that keeps track of how many questions are asked, or which question is being asked. Then the event handlers ( or action listener or whatever you are calling it) could do an if then on this variable and act accordingly. But your code could get someone long-winded if you have lots of questions. Keep in mind that you don't have to have all this done by the event handler itself, your event handler could call a method somewhere if that makes the code more manageable.

Anyways, that's one way of looking at it, someone else might handle it differently.
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found the "removeActionListener()" but have not found out how to use it


 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Al,
Please read http://faq.javaranch.com/java/CarefullyChooseOneForum

We have a forum dedicated fro Swing. I will move this thread there for you.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Fred Hamilton. His suggestion looks like the Object-Oriented way to do it.

Create a Quiz class, and a Question class. The quiz can have a List<Question> and the question can have an answer attached. Every answer, right or wrong, allows your quiz to move to the next question. You can even create an enum:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!