JDK-4164757 : setDefaultButton() is Broken for JInternalFrame
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.1.6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 1998-08-07
  • Updated: 1998-12-12
  • Resolved: 1998-12-12
Related Reports
Duplicate :  
Description
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;
  }
}

Comments
EVALUATION It's true. For the Motif LAF, the "default" should follow the *button* with focus, but when focus leaves the button for a non-button, the original default should be restored. This is a problem in both Motif & Windows look and feel, and is really a duplicate of 4146858. amy.fowler@Eng 1998-12-11
11-12-1998