Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 27-09-2006, 10:40 PM   #1 (permalink)
In The Zone
 
tweety_bird_bunny's Avatar
 
Join Date: Oct 2003
Posts: 211
Default reset jradiobuttton in a group to NULL !!!


i created a buttongroup and added 4 jradiobuttons to it initially none selected....
however when i select any one of them on execution of the program,,,
as the buttongroup does,,it requires that atleast one button will always be in ON state....
i want that if i unselect that jradiobutton,,, it shoud be unselected ....

so it means dat if i made selection 1 of the four radiobuttons,,on unselecting it ,, it shud be reset to OFF state....without requring me to select any other button to reset my previous selected button...

tried to use button.setSelected(false) but nothing happens....
plz help me to reset the buttongroup to null somehow
tweety_bird_bunny is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 28-09-2006, 03:50 PM   #2 (permalink)
Wise Old Owl
 
JGuru's Avatar
 
Join Date: Dec 2005
Location: Space-time continuum
Posts: 1,646
Default Re: reset jradiobuttton in a group to NULL !!!

@Tweety, In a JRadioButton if you select one(On state), select another( the previous JRadioButton is unselected( Off state),
and present one is selected (On state).
It's called a Toggle. ie., JRadioButton toggles between On & Off state.
Among a group of JRadioButtons only one can be selected
at a time. Also you can't unselect all!!
If you want to use a Component where you
can select all or unselect all, you must use
JCheckBox
__________________
* Imagination is more important than knowledge.
-Albert Einstein

Last edited by JGuru; 28-09-2006 at 03:56 PM.
JGuru is offline  
Old 28-09-2006, 11:01 PM   #3 (permalink)
Back to School Mr. Bean !
 
mod-the-pc's Avatar
 
Join Date: Apr 2004
Location: Chennai
Posts: 343
Default Re: reset jradiobuttton in a group to NULL !!!

Just add another invisible JRadioButton to that ButtonGroup and programatically select it when user clicks on an already selected JRadioButton in the ButtonGroup. This would give an illusion of deselection.

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class test extends JFrame implements ActionListener
{
 JRadioButton jrbArr[]=new JRadioButton[5];
 int selected;
 public test()
 {
  super("Test");
  ButtonGroup bg=new ButtonGroup();
  Container c=getContentPane();
  c.setLayout(new GridLayout(5,1));
  for(int i=0;i<jrbArr.length;i++)
  {
   jrbArr[i]=new JRadioButton("Option "+i);
   if(i!=0)
   {
    jrbArr[i].addActionListener(this);
    c.add(jrbArr[i]);
   }
   if(i==0)
   {
    jrbArr[i].setVisible(false);
    jrbArr[i].setSelected(true);
    selected=i;
   }
   bg.add(jrbArr[i]);
 
  }
  setSize(352,280);
  setVisible(true);
 }
 public static void main(String arg[])
 { 
   test b=new test();
  }
  public void actionPerformed(ActionEvent ce)
  { 
   JRadioButton clk=(JRadioButton)ce.getSource();
   System.out.println("Previously Selected button : "+jrbArr[selected].getText());
   for(int i=1;i<jrbArr.length;i++)
   {
     if(clk.equals(jrbArr[i]) && selected==i)
     {
      selected=0;
      jrbArr[0].setSelected(true);
     }
     else if (clk.equals(jrbArr[i]) && selected!=i)
      selected=i;
    }
  }
}
__________________
Desktop: P4 2.8E, Intel 865GBF, 512MB Hynix PC3200, Samsung SV1204H 120GB, Samsung SW-248F, Creative Inspire 4400, Samsung 793MB, Compro PVR/FM, WinLIRC

Laptop: HP Pavilion dv8210us

Last edited by mod-the-pc; 29-09-2006 at 07:45 PM.
mod-the-pc is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by topgear
- by abhidev
- by clmlbx
- by Sarath

Advertisement




All times are GMT +5.5. The time now is 04:40 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2