Default button after uninstall and setVisible

given the following code:

public class DialogTest implements ActionListener {
  public static void main(String[] args) {DialogTest g = new DialogTest();}

  public DialogTest() {
    JButton b1 = new JButton("Button A");
    b1.addActionListener(this);
    JDialog d = new JDialog();
    d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    JPanel p = new JPanel();
    p.add(b1);
    d.add(p);
    d.getRootPane().setDefaultButton(b1);
    d.pack();
    d.setVisible(true);
    d.dispose();
    d.pack();
    d.setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {System.out.println("hello");}
}

Should I not press Enter, write something to the console? According to the docs ( http://java.sun.com/javase/7/docs/api/java/awt/Window.html#dispose () ):

The window and its subcomponents can again be displayed restoring their own resources using a subsequent call to the package or display. The states of the recreated window and its subcomponents will be identical to the states of these objects at the point where the Window was located

Is this the intended behavior?

+3
source share
1 answer

, JButton.removeNotify (, , dispose), DefaultButton reset

JComponent.removeNotify, , ​​ RootPane, , RootPane null, , RootPane .


public void removeNotify() {
    JRootPane root = SwingUtilities.getRootPane(this);
    if (root != null && root.getDefaultButton() == this) {
        root.setDefaultButton(null);
    }
    super.removeNotify();
}
+1

Source: https://habr.com/ru/post/1743173/


All Articles