Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4517913

ButtonGroup allows multiple selected items in 1.4

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.1
    • 1.4.0
    • client-libs
    • hopper
    • x86, sparc
    • linux, solaris, windows_2000
    • Verified

      When constructing a GUI, adding multiple buttons which are setSelected(true) to a ButtonGroup can result in a group with multiple selected items - breaking the contract of a ButtonGroup.

      The behavior is not clearly defined in the javadoc - is it the programmer's or library's responsibility? In JDK 1.3, the group seems to select one of the buttons and unselects the rest. In JDK 1.4, it leaves them all in their original selection state.

      I'm attaching a modified version of the radio button demo from the Java Tutorial. Notice the setSelected() on both lines 21 and 38. Compile and run this under both JDK 1.3 and 1.4 to see the difference.

      My questions:
      - is this an intended change?
      - is there at least a spec bug here?

      Name: jk109818 Date: 12/03/2001


      java version "1.4.0-beta3"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
      Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)

      In the following example, the first two JRadioButtons within the one ButtonGroup
      come up selected when the user-interface is first displayed. It is necessary to
      deliberately deselect/select some items to get into a state where only one is
      selected, before this consistent state is maintained.

      This bug was not present in 1.3.1, but it was in 1.4.0beta2.

      import javax.swing.*;
      import java.awt.*;
      import javax.swing.event.*;
      import java.awt.event.*;

         class DialogExpandFiles extends JDialog implements ActionListener
         {
            protected JRadioButton toPreview, toWindow, toFile, toPrinter;
            private boolean ok;
            private Frame parent;
            
            public DialogExpandFiles( Frame parent )
            {
               super(parent,"Expand several files",true);
               this.parent = parent;
               
               Container cpane = getContentPane();

               JButton bok = new JButton("Ok");

               GridBagLayout gb = new GridBagLayout();
               cpane.setLayout(gb);
               GridBagConstraints gbc = new GridBagConstraints();
               gbc.insets = new Insets(1,3,1,3);
         
               gbc.anchor = GridBagConstraints.WEST;
               gbc.weightx = 100;
               gbc.weighty = 100;
               gbc.gridwidth = gbc.REMAINDER;
               gbc.gridheight = 1;
         
               ButtonGroup where = new ButtonGroup();
               
               toPreview = new JRadioButton("Preview", true);
               where.add(toPreview);
               cpane.add(toPreview,gbc);
               
               toWindow = new JRadioButton("To Window", true);
               where.add(toWindow);
               cpane.add(toWindow,gbc);
               
               toFile = new JRadioButton("To File", false);
               where.add(toFile);
               cpane.add(toFile,gbc);
               
               toPrinter = new JRadioButton("To Printer", false);
               where.add(toPrinter);
               cpane.add(toPrinter,gbc);
               
               JPanel p4 = new JPanel();
               p4.setLayout( new FlowLayout() );
               gbc.anchor = GridBagConstraints.CENTER;
               p4.add(bok);
               bok.addActionListener(this);
               JButton bcl = new JButton("Cancel");
               p4.add(bcl);
               bcl.addActionListener(this);
               cpane.add(p4,gbc);
               pack();
            }

            public void actionPerformed(ActionEvent evt)
            {
               String s = evt.getActionCommand();
               if (s.equals("Ok"))
                  ok = true;
               dispose();
            }

            public boolean showDialog()
            {
               Point pt = parent.getLocation();
               setLocation(pt.x+10,pt.y+40);
               pack();
               setModal(true);
               ok = false;
               show();
               return ok;
            }
            public static void main( String[] s)
            {
               final JFrame f = new JFrame("Test radio buttons");
               Container cpane = f.getContentPane();
               JButton b = new JButton("Show");
               cpane.add(b);
               b.addActionListener( new ActionListener()
               {
                  public void actionPerformed( ActionEvent e )
                  {
                     DialogExpandFiles def = new DialogExpandFiles( f );
                     def.showDialog();
                  }
               } );
               f.pack();
               f.show();
            }
         }

      [I am using fvwm2 as my window manager on Linux, in case this is relevant. In
      the past, there have been bugs specific to this window manager.]
      (Review ID: 135606)
      ======================================================================

            shickeysunw Shannon Hickey (Inactive)
            bkurotsu Brian Kurotsuchi (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: