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

setDefaultButton() broken under JDialog

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.2.2
    • 1.1.6
    • client-libs
    • 1.2.2
    • x86
    • windows_nt



      Name: diC59631 Date: 09/08/98


      When creating a JDialog with a label and two buttons,
      I set one button to be the default button. Once the dialog
      is shown, pressing enter should call the actionPerformed()
      method for the ActionListeners on the defaultButton, but
      it does not. The Button is darkened to indicate it is the
      default. This is using swing 1.0.3.

      If I change the code to inherit from JFrame
      instead of JDialog it works.

      I'm resubmitting since I forgot to attach source.

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

      public class Default extends JDialog implements ActionListener {
        public Default() {
          super(null);
          setTitle("Default Test");

          JLabel msg = new JLabel ("This is a message in a dialog box. Select Ok or Cancel", JLabel.CENTER);
          msg.setForeground(Color.black);
          JButton b1 = new JButton("Ok");
          JButton b2 = new JButton("Canel");
          getContentPane().setLayout(new GridLayout(2, 1, 10, 10));
          getContentPane().add(msg);
          JPanel buttonPanel = new JPanel();
          buttonPanel.setLayout(new FlowLayout());
          b1.setActionCommand("ok");
          b2.setActionCommand("cancel");
          b1.addActionListener(this);
          b2.addActionListener(this);
          buttonPanel.add(b1);
          buttonPanel.add(b2);
          getContentPane().add(buttonPanel);

          getRootPane().setDefaultButton(b1);
          setDefaultCloseOperation(DISPOSE_ON_CLOSE);
          pack();
        }
        public void actionPerformed(ActionEvent e) {
          JButton b = (JButton)e.getSource();
          System.out.println("Button " + b.getActionCommand() + " pressed");
        }
        public static void main(String args[]) {
          Default dialog = new Default();
          dialog.addWindowListener(new WindowAdapter() {
            public void windowClosed(WindowEvent e) {
              System.exit(0);
            }
          });
          dialog.show();
        }
      }
      (Review ID: 38357)
      ======================================================================

            amfowler Anne Fowler (Inactive)
            dindrigo Daniel Indrigo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: