• Post Reply Bookmark Topic Watch Topic
  • New Topic

A better way to use JButton and Listener?

 
Al Fraelich
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me explain what I am doing, because I was unable to take a class this semester I am going through the book I used for my first two programming classes (Java) and rewritting them again in GUI while trying to improve on the code itself. please understand neither of the two classes I have taken so far have gone beyond the EXTREME basics of GUI programming. I am learning on my own using several books and of course the internet. So here is what I have done so far, and it works as it should but it is extremely cumbersome, I know there must be a cleaner method for this.



As you read through, what I have done was create a program that asks a series of "yes" or "no" questions. As you answer the questions you move either to another question or the final result. To do this I have created a new set of "yes/no" JButtons so that each one could have it's own Listener(). My question is, how would I "reuse" the "yes/no" JButton, or how would I remove the previous questions "actionListener"? I have tried removeActionListener() but could not get it work properly.

I hope it was not wrong of me to post the whole thing, but I feel the code could explain my dillema.
Thank for any and all help
Al Fraelich
 
Paul Clapham
Sheriff
Posts: 21889
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need a reference to an ActionListener if you want to remove it from a JButton. Example:

and later...

 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:You will need a reference to an ActionListener if you want to remove it from a JButton. Example:

Not really, if as in this case, the objective is to remove the only ActionListener.

Much better here to use JOptionPane.showConfirmDialog with YES_NO_OPTION.
 
Al Fraelich
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much that worked. I was reading and trying all kinds of stuff, including removeAction and changeAction.
I have a follow up question, is the reason that I had to "getActionListener()" because it was outside the scope of the current method? and if so is it possible to declare the listeners globally? Just curious cause I tried all kinds of stuff trying to wrap my head around this one.

Thanks for the help
Al
 
Paul Clapham
Sheriff
Posts: 21889
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Al Fraelich wrote: if so is it possible to declare the listeners globally?

It is possible to declare the listeners right next to where you declared the JButtons. Why wouldn't it be?
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what you want is an OrganismTypeActionListener which implements ActionListener and can take the messages in its constructor.
 
mark goking
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you thought of using joptionpane? it would shorten your code
 
Al Fraelich
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunatley I am learning this on my own. I have had two programming classes so far, both in java, but I did not learn GUI until the last week of the second course and we only used a couple JButtons and JFrame. I have been trying to continue on my own but if it wasn't used in my class, I don't really know about it. I have been to the the API site (also downloaded it) but navigating it for what might be a better way of doing it is at times, confusing. But any suggestions I get, I will immediatley attempt to incorporate them in my programming. The only one I have not, as of yet, been able to use is the "button.removeListener()" method. I am not sure why, but I believe it's because I am trying to remove it from within that actual listener() and I am not sure if that is possible, but I will try you suggestion right away, Thank you for your response!

Thank you all for your input

Al Fraelich
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!