Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to let Modal Dialog not block my second top-level Frame

I have a main frame and a second frame and a modal dialog with the main frame as parent.

But now are both frames blocked by the modal dialog.
How can I let the second frame accessable while the main frame has a modal dialog?

public class Example extends JFrame {

    public Example() {
        super("MainFrame");

        JButton btn1 = new JButton( new AbstractAction( "Frame" ) {
            @Override
            public void actionPerformed( ActionEvent e ) {
                EventQueue.invokeLater( new Runnable() {
                    @Override
                    public void run() {
                        JFrame f = new JFrame( "Frame" );
                        f.getContentPane().add( new JLabel("This shoud be not blocked by ModalDialog.") );
                        f.setLocation( 50, 200 );
                        f.setSize( 300, 200 );
                        f.setVisible( true );
                    }
                } );
            }
        } );

        JButton btn2 = new JButton( new AbstractAction( "Modal" ) {
            @Override
            public void actionPerformed( ActionEvent e ) {
                EventQueue.invokeLater( new Runnable() {
                    @Override
                    public void run() {
                        JDialog d = new JDialog( Example.this, "Dialog" );
                        d.getContentPane().add( new JLabel("This shoud block only MainFrame.") );
                        d.setModal( true );
                        d.setLocation( 50, 100 );
                        d.setSize( 300, 200 );
                        d.setVisible( true );
                    }
                } );
            }
        } );

        setDefaultCloseOperation( EXIT_ON_CLOSE );
        getContentPane().setLayout( new BorderLayout() );
        getContentPane().add( btn1, BorderLayout.NORTH );
        getContentPane().add( new JLabel("MainFrame"), BorderLayout.CENTER );
        getContentPane().add( btn2, BorderLayout.SOUTH);
        setLocation( 50, 50 );
        setSize( 200, 150 );

        btn1.doClick();
        btn2.doClick();
    }

    public static void main( String[] args ) {
        EventQueue.invokeLater( new Runnable() {
            @Override
            public void run() {
                new Example().setVisible( true );
            }
        } );
    }

}
like image 262
oliholz Avatar asked Mar 19 '12 08:03

oliholz


1 Answers

you need to set modality field to document.

JDialog d = new JDialog( Example.this, "Dialog" ,Dialog.ModalityType.DOCUMENT_MODAL); 
like image 136
digitebs Avatar answered Oct 19 '22 06:10

digitebs