1

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 :)

4

2 に答える 2

4

setVisible(true);呼び出しをコンストラクターの最後に移動するだけです。

于 2012-09-27T17:29:03.110 に答える
0

getContentPane()バージョンの呼び出しを削除します。JDialog機能するはずです

于 2012-09-27T17:25:39.147 に答える