Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

changing a JRadio Button selected state
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



Report this thread to moderator Post Follow-up to this message
Old Post
Russell
10-24-04 08:57 PM


Re: changing a JRadio Button selected state
>               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...



Report this thread to moderator Post Follow-up to this message
Old Post
Russell
10-24-04 08:57 PM


Re: changing a JRadio Button selected state
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 a
n
> 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.

Report this thread to moderator Post Follow-up to this message
Old Post
Paul H. van Rossem
10-24-04 08:57 PM


Re: changing a JRadio Button selected state
> 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]);
}



Report this thread to moderator Post Follow-up to this message
Old Post
Russell
10-24-04 08:57 PM


Re: changing a JRadio Button selected state
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Alex Hunsley
10-24-04 08:57 PM


Re: changing a JRadio Button selected state
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


Report this thread to moderator Post Follow-up to this message
Old Post
Alex Hunsley
10-24-04 08:57 PM


Re: changing a JRadio Button selected state
"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!!



Report this thread to moderator Post Follow-up to this message
Old Post
Russell
10-24-04 08:57 PM


Re: changing a JRadio Button selected state
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
>



Report this thread to moderator Post Follow-up to this message
Old Post
Russell
10-25-04 01:56 AM


Re: changing a JRadio Button selected state
Paul H. van Rossem wrote:

> On 24-10-2004 16:45, Russell wrote: 
an 
>
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Nigel Wade
10-25-04 01:57 PM


Re: changing a JRadio Button selected state
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





Report this thread to moderator Post Follow-up to this message
Old Post
Alex Hunsley
10-26-04 08:59 PM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:52 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.