Question

I'm having a strange problem with a class that extends JDialog:

class MyDialog extends JDialog {

    private static final long serialVersionUID = 11564288421L;

    public MyDialog(JFrame owner, MyObject object) {
        super(owner, true);
        setSize(300, 200);
        setLocationRelativeTo(owner);
        String title = "Object ID: " + object.getId(); 
        setTitle(title);
        setVisible(true);

        JLabel lblTitle = new JLabel(title);
        lblTitle.setBounds(0, 0, this.getWidth(), 22);
        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 18));
        lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
        getContentPane().setLayout(null);
        getContentPane().add(lblTitle);
    }
}

When I call it with new MyDialog(existingJFrameInstance, existingMyObjectInstance);, MyDialog is displayed and well-titled, but there's nothing in its ContentPane.

If I change the class into:

class MyDialog extends JFrame {

    private static final long serialVersionUID = 11564288421L;

    public MyDialog(JFrame owner, MyObject object) {
        super();
        setSize(300, 200);
        setLocationRelativeTo(owner);
        String title = "Object ID: " + object.getId(); 
        setTitle(title);
        setVisible(true);

        JLabel lblTitle = new JLabel(title);
        lblTitle.setBounds(0, 0, this.getWidth(), 22);
        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 18));
        lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
        getContentPane().setLayout(null);
        getContentPane().add(lblTitle);
    }
}

Everything works just fine. I can't imagine what's happening here :\

I need the Window to be Modal, that's why I must go with JDialog instead of JFrame.

I tried using this.pack();, this.validate();, getContentPane().revalidate();, getContentPane().repaint(); etc etc etc in every possible combination and I am in the EDT (SwingUtilities.isEventDispatchThread() returns true).

Please help :)

Was it helpful?

Solution

Just move your setVisible(true); call to the end of your constructor.

OTHER TIPS

Remove the getContentPane() invocations in the JDialog version, it should work

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top