JRootPane: setDefaultButton(JButton defaultButton) : JRootPane « javax.swing « Java by API






JRootPane: setDefaultButton(JButton defaultButton)

 

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

public class Main extends JPanel {
  public static void main(String[] a) {
    JDialog f = new JDialog();
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    JButton btOK = new JButton("Press Enter to click me, I am the default.");
    btOK.setToolTipText("Save and exit");
    f.getRootPane().setDefaultButton(btOK);

    JPanel p = new JPanel();
    p.add(btOK);
    p.add(new JButton("I am NOT the default."));
    f.getContentPane().add(p);

    f.pack();
    f.setSize(new Dimension(300, 200));

    f.setVisible(true);

  }
}

   
  








Related examples in the same category

1.JRootPane.FRAME
2.JRootPane: addAncestorListener(AncestorListener listener)
3.JRootPane: getAccessibleContext()
4.JRootPane: getContentPane()
5.JRootPane: setWindowDecorationStyle(int style)