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!
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
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"
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?
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.
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 :-)
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.