For Programmers: Free Programming Magazines  


Home > Archive > Java Help > October 2004 > changing a JRadio Button selected state









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author changing a JRadio Button selected state
Russell

2004-10-24, 3:57 pm

I have 6 JRadioButtons within a ButtonGroup and everything works fine
"until"
I want to reset the state of any of the Radio Buttons.
It refuses to update the state of any of the Radio Buttons when fired by an
ActionListener
from a different set/ buttonGroup of RadioButtons.
Within that ActionListener I am referencing to the set I want to have
de-selected with the following code:

for (int k = 0; k < 6; k++)
if(srtBut[k].isSelected() == true)
srtBut[k].setSelected(false);

why wont this de-select the JRadioButtons?

thankyou


Russell

2004-10-24, 3:57 pm

> for (int k = 0; k < 6; k++)
> if(srtBut[k].isSelected() == true)
> srtBut[k].setSelected(false);
>
> why wont this de-select the JRadioButtons?
>
> thankyou
>

PS: the full code is a bit too lengthy to copy and paste into here...


Paul H. van Rossem

2004-10-24, 3:57 pm

On 24-10-2004 16:45, Russell wrote:
> I have 6 JRadioButtons within a ButtonGroup and everything works fine
> "until"
> I want to reset the state of any of the Radio Buttons.
> It refuses to update the state of any of the Radio Buttons when fired by an
> ActionListener
> from a different set/ buttonGroup of RadioButtons.
> Within that ActionListener I am referencing to the set I want to have
> de-selected with the following code:
>
> for (int k = 0; k < 6; k++)
> if(srtBut[k].isSelected() == true)
> srtBut[k].setSelected(false);
>
> why wont this de-select the JRadioButtons?
>
> thankyou


Swing is not thread-safe! The button's state is reset, but you don't
see it if you call setSelected() from the ActionListener, as the GUI
is not updated correctly.

See the documentation on SwingUtilities.invokeLater() for an easy
solution.

Good luck, Paul.
Russell

2004-10-24, 3:57 pm

> Swing is not thread-safe! The button's state is reset, but you don't
> see it if you call setSelected() from the ActionListener, as the GUI
> is not updated correctly.
>
> See the documentation on SwingUtilities.invokeLater() for an easy
> solution.
>
> Good luck, Paul.


Following your advise I've tried the following without any differences...
any Radio Button selected doesn't de-select.

groupArry = new ButtonGroup();

for(int i=0; i<3; i++) { //Array of 3 Radio Buttons
final int j = i;
aryBut[j] = new JRadioButton(arrayStr[j]);
aryBut[j].setActionCommand(arrayStr[j]);
ActionListener act = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == arrayStr[j]) {
if(aryBut[j].isSelected() == true) {
messStr = "Choose Sorting Method";
message.setText(""+messStr);
Thread runner = new Thread() {
public void run() {
Runnable run = new Runnable() {
public void run() {
// set/reset all runtime vars
for (int k = 0; k < 6; k++) {
if(srtBut[k].isSelected() == true) {
srtBut[k].setSelected(false);
dsply1.append(sortStr[k]+"\n");
}
srtBut[k].setEnabled(true);
}
}
};
SwingUtilities.invokeLater(run);
}
};
runner.start();
sortIndex = 0;
arryIndex = 0;

if (count != -1) { //count initialized by checkbox's
input.setEnabled(true);
start.setEnabled(true);
}
else {
input.setEnabled(false);
start.setEnabled(false);
}
arryIndex = j+1;//selected array index grabbed at button
press
}
}
}
};
aryBut[j].addActionListener(act);
groupArry.add(aryBut[j]);
radPan.add(aryBut[j]);
}


Alex Hunsley

2004-10-24, 3:57 pm

Paul H. van Rossem wrote:
> On 24-10-2004 16:45, Russell wrote:
>
>
>
> Swing is not thread-safe!


True...

> The button's state is reset, but you don't
> see it if you call setSelected() from the ActionListener, as the GUI
> is not updated correctly.


Actually, ActionListeners tend to be called in the GUI thread anyway
(which is what you're referring to when you mention invokeLater()) so
that wouldn't be the problem.
Also, in my experience, although altering/reading GUI items from a
non-GUI thread is not advisable, in many of the cases where I've seen it
done, there haven't been obvious problems. (Of course they can and do
sometimes pop up as a result.)

> See the documentation on SwingUtilities.invokeLater() for an easy
> solution.
>
> Good luck, Paul.

Alex Hunsley

2004-10-24, 3:57 pm

Russell wrote:
>
>
> Following your advise I've tried the following without any differences...
> any Radio Button selected doesn't de-select.
>
> groupArry = new ButtonGroup();
>
> for(int i=0; i<3; i++) { //Array of 3 Radio Buttons
> final int j = i;
> aryBut[j] = new JRadioButton(arrayStr[j]);
> aryBut[j].setActionCommand(arrayStr[j]);
> ActionListener act = new ActionListener() {
> public void actionPerformed(ActionEvent e) {
> if (e.getActionCommand() == arrayStr[j]) {
> if(aryBut[j].isSelected() == true) {
> messStr = "Choose Sorting Method";
> message.setText(""+messStr);
> sortIndex = 0;
> arryIndex = 0;
>
> if (count != -1) { //count initialized by checkbox's
> input.setEnabled(true);
> start.setEnabled(true);
> }
> else {
> input.setEnabled(false);
> start.setEnabled(false);
> }
> arryIndex = j+1;//selected array index grabbed at button
> press
> }
> }
> }
> };
> aryBut[j].addActionListener(act);
> groupArry.add(aryBut[j]);
> radPan.add(aryBut[j]);
> }


Btw, you don't really need that Thread called runner - given that all
you're doing is firing off a call to invokeLater(), which doesn't take
an awful lot of time, you can do without that. However, if you wanted to
do something time consuming in the ActionListener, it is the right thing
to do.


In other words, replace the following:

> Thread runner = new Thread() {
> public void run() {
> Runnable run = new Runnable() {
> public void run() {
> // set/reset all runtime vars
> for (int k = 0; k < 6; k++) {
> if(srtBut[k].isSelected() == true) {
> srtBut[k].setSelected(false);
> dsply1.append(sortStr[k]+"\n");
> }
> srtBut[k].setEnabled(true);
> }
> }
> };
> SwingUtilities.invokeLater(run);
> }
> };
> runner.start();


with this:

> Runnable run = new Runnable() {
> public void run() {
> // set/reset all runtime vars
> for (int k = 0; k < 6; k++) {
> if(srtBut[k].isSelected() == true) {
> srtBut[k].setSelected(false);
> dsply1.append(sortStr[k]+"\n");
> }
> srtBut[k].setEnabled(true);
> }
> }
> };
> SwingUtilities.invokeLater(run);



I'd like to try running this code but I really need some complete
compilable code. Can you either extract the essential parts that
dmeonstrate the problem and post them here so it's compilable, or can
you put the code (if it's large) on a web site or ftp server?

alex

Russell

2004-10-24, 3:57 pm


"Alex Hunsley" wrote :
> I'd like to try running this code but I really need some complete
> compilable code. Can you either extract the essential parts that
> dmeonstrate the problem and post them here so it's compilable, or can
> you put the code (if it's large) on a web site or ftp server?
>
> alex
>


I have uploaded the 4 files to :

http://www.webruss.com/temp/temp.htm

please respond here immediately when you have downloaded them as i will then
take them back offline

this app is for a rather large assessment at college.

thanks for your help!!


Russell

2004-10-24, 8:56 pm

Have now taken those files down

"Alex Hunsley"wrote:
> I'd like to try running this code but I really need some complete
> compilable code. Can you either extract the essential parts that
> dmeonstrate the problem and post them here so it's compilable, or can
> you put the code (if it's large) on a web site or ftp server?
>
> alex
>



Nigel Wade

2004-10-25, 8:57 am

Paul H. van Rossem wrote:

> On 24-10-2004 16:45, Russell wrote:
an[color=darkred]
>
> Swing is not thread-safe! The button's state is reset, but you don't
> see it if you call setSelected() from the ActionListener, as the GUI
> is not updated correctly.
>
> See the documentation on SwingUtilities.invokeLater() for an easy
> solution.
>
> Good luck, Paul.


If his code is in an actionListener then it will be executed by the EDT so
its not necessary to use invokeLater() (provided the actionListener is
invoked in response to some GUI action and not from user code in another
thread).

As for the original problem, I don't think radioButtons work that way. Once
one is selected from the group then there will always be one, and only one,
selected. You don't deselect radioButtons, selecting one deselects the
current one.

A quote from the Java Swing Tutorial on RadioButtons "Once the user has made
a selection, exactly one button is selected from then on. There's no
supported API for unselecting all the buttons."

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
Alex Hunsley

2004-10-26, 3:59 pm

Russell wrote:
> "Alex Hunsley"wrote:
>
> Have now taken those files down


[fixed your top-posting and made it bottom posted; please bottom post in
future]

I didn't get a chance to access these files I'm afraid! I've just seen
your reply.

alex




Alex Hunsley

2004-10-26, 3:59 pm

Russell wrote:
>
>
> Following your advise I've tried the following without any differences...
> any Radio Button selected doesn't de-select.
>


btw, you could try calling the doClick() method, which programmatically
'clicks' the button. Not ideal perhaps, but it may get it working for now!

alex
Russell

2004-10-27, 3:59 pm

> > "Alex Hunsley"wrote:
> [fixed your top-posting and made it bottom posted; please bottom post in
> future]
>
> I didn't get a chance to access these files I'm afraid! I've just seen
> your reply.
>
> alex
>


Alex,

Email me using the address listed with this message and I will email you the
4 java files.

Russell


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com