• Post Reply Bookmark Topic Watch Topic
  • New Topic

When RadioButton is selected, checkBoxes remain disabled why?  RSS feed

 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I'm a bit perplexed at the fact that in my program, when a radio button is selected, the checkBoxes remain disabled. Based on my code, I would assume that would change based on the specific radio button that is selected. Please shed some light. Thanks!

 
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
Your actionPerformed does nothing except print some text to the console. You need to move your en/disable logic to execute inside the actionPerformed
Note: Avoid using e.getActionCommand() Use e.getSource() instead
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Note: Avoid using e.getActionCommand() Use e.getSource() instead

Can you please give a bit more detail on this? When I was looking for some code which allowed one listener to cover multiple distinct button press events, the code I found used getActionCommand. This worked fine as it contained the text on the button. Yes, a problem if the button has an icon and not text. Is this the concern?

I just tried using both getSource and getActionCommand on two button presses. The first button was "5". The second button was "8"

getSource
javax.swing.JButton[,97,67,94x66,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@2f2eb56c,flags=296,maximumSize=,minimumSize=,
preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,
rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=5,defaultCapable=true]

getActionCommand: 5

getSource
javax.swing.JButton[,97,134,94x66,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@2f2eb56c,flags=296,maximumSize=,minimumSize=,
preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,
rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=8,defaultCapable=true]

getActionCommand: 8

I can see 5 and 8 within the 'text' attribute. How would you use this (or any of the other attributes) to ID the button pressed?
 
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
Paul Clements wrote:
Can you please give a bit more detail on this?


A code snippet speaks a thousand words. Try it out.
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, absolutely. You would clearly have an issue with getActionCommand if the button label changes. So the question remains, how does getSource handle this? My take on your original post was that getSource could ID the button, irrespective of label text. How does it do that?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you seen the EventObject#getSource() method, which is inherited by action event? When an event occurs, e.g. your pushing a button, there is some source or other and the event records that. All you are doing is checking whether it is the same as the button you happen to be using in your method. There are probably better ways to add listeners to check boxes, avoiding that dreadful block of repeated ifs and repeated code.
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, had a quick Google, and I can see how getSource works. I had called it previously and got back a whole screed of info. Didn't appreciate you could actually do this:
and if you did indeed click the JButton instance called "num1" it will print out the message.

Now whether this is how you want to code button handling is a matter for another day. For the moment I understand how getSource works, if I want 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
In professional environments it is quite a standard norm to support internationalization. In such scenarios any code which relies on a specific text in a specific language is a bad idea.
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed some stuff and everything is fine now. I have another question though. I have not worked extensively with Swing, in fact, I am more familiar with JavaFX, but for some reason my professor only wants us to use Swing and AWT for this assignment (bummer I know). Anyways lol, I have an extremely high res machine, and I'm wondering if there is a way to scale the Application itself to not look so tiny on a high resolution screen.

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