• Post Reply Bookmark Topic Watch Topic
  • New Topic

ActionListener Issues On JRadioButton  RSS feed

 
Thiago Braun
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, my frieds. I´m currently coding a little game and I want to display on a TextArea the active player based on a Radio Button Selection. That selection should also set a boolean Player to true if the button is selected.

Here are some of my variables:



Here is where I instantiate the text fields and buttons:





And this is the ActionPerformed method:



Problem is the if conditions above doesnt seem to be working, since the text area is not altered and the booleans are not correctly set when the Radio Buttons are selected.

Thanks for your time helping me finding the problem here.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't mix AWT and Swing components so rather than using TextArea you should use JTextArea.
You shouldn't use a catch-all actionPerformed method (unless the same code can handle multiple component's actions), create a separate ActionListener object for each component.

To solve your problem the first step is to add some System.out.println("some message here") statements to your code so you can see which code is being executed and what state objects are in.
 
Thiago Braun
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, Tony. I did some changes as you suggested and this is the radio buttons code I have now:



Even with those changes, the situation is the same. I only get the first print outside the conditional test on each button. The ifs are never executed, even when the buttons are pressed.
 
Thiago Braun
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found the problem! The ending bracket for the first loop was not at the end of the constructor so it was creating new objects multiple times.

Thanks!
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done for solving the problem yourself and thanks for posting the solution.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!