Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Effect of super() in JDialog constructor on focusability/modality

JörgJul 15 2010 — edited Jul 17 2010
Hello again,

this is a JFrame which calls a JDialog which calls a JDialog.
Using super(...) in the constructor of SecondDialog makes this dialog
unfocusable as long as FirstDialog is shown. Without super(...) one can freely
move between the dialogs.
Can somebody exlain what "super" does to produce this difference?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SuperConstructor extends JFrame {
  
  public SuperConstructor() {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setSize(300,300);
    setTitle("Super constructor");
    Container cp= getContentPane();
    JButton b= new JButton("Show dialog");
    b.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
	new FirstDialog(SuperConstructor.this);
      }
    });
    cp.add(b, BorderLayout.SOUTH);
    setVisible(true);
  }


  public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
	new SuperConstructor();
      }
    });
  }


  class FirstDialog extends JDialog {
    public FirstDialog(final Frame parent) {
      super(parent, "FirstDialog");
      setSize(200,200);
      setLocationRelativeTo(parent);
      setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
      JButton bNext= new JButton("Show next dialog");
      bNext.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent evt) {
	  new SecondDialog(parent, false);
	}
      });
      add(bNext, BorderLayout.SOUTH);
      setVisible(true);
    }
  }


  int i;
  class SecondDialog extends JDialog {
    public SecondDialog(Frame parent, boolean modal) {
      super(parent); // Makes this dialog unfocusable as long as FirstDialog is  

shown
      setSize(200,200);
      setLocation(300,50);
      setModal(modal);
      setTitle("SecondDialog "+(++i));
      JButton bClose= new JButton("Close");
      bClose.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent evt) {
	  dispose();
	}
      });
      add(bClose, BorderLayout.SOUTH);
      setVisible(true);
    }
  }

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 14 2010
Added on Jul 15 2010
3 comments
1,078 views