Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this message> 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...
Post Follow-up to this messageOn 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.
Post Follow-up to this message> 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]);
}
Post Follow-up to this messagePaul 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.
Post Follow-up to this messageRussell 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
Post Follow-up to this message"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!!
Post Follow-up to this messageHave 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 >
Post Follow-up to this messagePaul 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
Post Follow-up to this messageRussell 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.