Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting the value of a radio button declared in another class  RSS feed

 
Saikrishnan Srivatsan
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create a tic tac toe GUI that needs to be set up as follows:

1) The frame is a grid made of 2 panes, left pane with a 9 button grif that acts as the board
2) The right pane pane is divided into two more panes 1 below the other. Based on the symbol the user chooses using the radio buttons, a click on the gameboard must display either "X" or "O".

Since my mouse listener code is in a separate class I am not sure how I can get what the user clicked.

Here is my code. Please give me a nudge in the right direction to overcome this issue.

 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saikrishnan,
Welcome to the Ranch!

First of all, why are you using a MouseListener? For buttons, ActionListener is the best one to use. Your problem comes when you use the same class as the listener for all buttons (this has a actionPerformed() method which takes an ActionEvent object as a parameter). You might see many examples which use getActionCommand() method of the ActionEvent object and compare using the button text. But, this is not a good practice. You can use the getSource() method, which will return you the source object (in this case, the button that was clicked on). However, to use the getSource(), you would need access to the actual source object.

So, a better way would be to use an anonymous class as the actionListener which can in turn call a method within the class. Or, use one listener class per button. You can also use actions for this purpose.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!