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

setDefaultButton() is Broken for JInternalFrame

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.6
    • client-libs

      The test case below fails for both Solaris and Windows. The JDK is 1.1.6, the
      Swing version 1.0.2.

      When the application is initialized, the default button Button2 displays and
      functions properly. However, when the user tabs to Button1, Button1 is erroneously highlighted and acts as the default button (it is activated when
      the return key is pressed). Button1 continues to act as the default button when tabbing to field2. When tabbing to Button2, Button2 again acts as the default button.

      --------------------------------- UnDefaultify.java ----------------------------

      import java.awt.*;
      import java.awt.event.*;
      import com.sun.java.swing.*;


      public class UnDefaultify extends JInternalFrame {

        public UnDefaultify() {
          setSize(200, 200);
          setResizable(true);
          createComps();
          removeEnterMapping();
          layoutComps();
          getRootPane().setDefaultButton(button2);
        }


        protected void createComps() {
          field1 = new JTextField("field 1");
          field2 = new JTextField("field 2");
          button1= new JButton("Button 1");
          button2= new JButton("Button 2 (DEF)");
          combo1 = new JComboBox();
        }

        protected void layoutComps() {
          GridBagConstraints f1GBC = createGBConstraints(0, 0, 1, 1,
          GridBagConstraints.NONE,
          GridBagConstraints.CENTER,
          1.0, 1.0);
          GridBagConstraints b1GBC = createGBConstraints(1, 0, 1, 1,
          GridBagConstraints.NONE,
          GridBagConstraints.CENTER,
          1.0, 1.0);

          GridBagConstraints f2GBC = createGBConstraints(0, 1, 1, 1,
          GridBagConstraints.NONE,
          GridBagConstraints.CENTER,
          1.0, 1.0);

          GridBagConstraints b2GBC = createGBConstraints(1, 1, 1, 1,
          GridBagConstraints.NONE,
          GridBagConstraints.CENTER,
          1.0, 1.0);

          GridBagConstraints c1GBC = createGBConstraints(0, 2, 2, 1,
          GridBagConstraints.NONE,
          GridBagConstraints.CENTER,
          1.0, 1.0);

          getContentPane().setLayout(new GridBagLayout());
          getContentPane().add(field1, f1GBC);
          getContentPane().add(button1, b1GBC);
          getContentPane().add(field2,f2GBC);
          getContentPane().add(button2, b2GBC);
          getContentPane().add(combo1, c1GBC);
        }

        protected void removeEnterMapping() {
          KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
          field1.getKeymap().removeKeyStrokeBinding(enter);
          field2.getKeymap().removeKeyStrokeBinding(enter);
        }

        public static void main(String[] args) {
          try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          } catch (Exception e) {
            System.out.println("Could not set L&F: " + e);
            e.printStackTrace();
          }
          JFrame frame = new JFrame();
          frame.setSize(400, 400);
          JDesktopPane dt = new JDesktopPane();
          frame.getContentPane().add(dt);
          frame.setVisible(true);
          UnDefaultify tt = new UnDefaultify();
          dt.add(tt);
        }


        JTextField field1;
        JTextField field2;
        JComboBox combo1;
        JButton button1;
        JButton button2;


        public static GridBagConstraints
        createGBConstraints(int gridx, int gridy, int gridwidth,
              int gridheight, int fill, int anchor,
              double weightx, double weighty) {
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.gridx = gridx;
          gbc.gridy = gridy;
          gbc.gridwidth = gridwidth;
          gbc.gridheight = gridheight;
          gbc.fill = fill;
          gbc.anchor = anchor;
          gbc.weightx = weightx;
          gbc.weighty = weighty;
          return gbc;
        }
      }

            amfowler Anne Fowler (Inactive)
            nrodinsunw Nick Rodin (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: