John Kramer
John Kramer

Reputation: 11

activate JFrame when opened from modal JDialog

I need to launch a JDialog from parent JFrame and make to modal. from JDialog I need to view another JFrame (sub frame) and access it. but once we have modal dialog open we cannot access so is there any workaround to access sub frame opened from modal dialog.

here is code snippet. I need to access f1 frame opened from j modal dialog.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class TestDialog {

    public static void main(String[] args) {

        TestDialog t=new TestDialog();
        t.show();

    }

    void show(){
        JFrame f = new JFrame("Simple Frame");
        JTextField tf = new JTextField("Text");
        tf.setPreferredSize(new Dimension(200, 100));
        JButton b=new JButton("click me");

        f.getContentPane().setLayout(new BorderLayout());
        f.getContentPane() .add(tf, BorderLayout.NORTH);
        f.getContentPane() .add(b, BorderLayout.CENTER);
        f.setBounds(500, 400, 500, 500);
        f.setVisible(true);
        f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);

        final JDialog j=new JDialog();
        j.setSize(200,300);
        JPanel p=new JPanel();
        JButton b1=new JButton("check me");
        p.add(new JLabel("label me"));
        p.add(b1);
        j.getContentPane().add(p);
        j.setDefaultCloseOperation(j.DISPOSE_ON_CLOSE);
        j.setModal(true);
        j.setLocationRelativeTo(f);

        b.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                j.show(true);               
            }

        });     

        final JFrame f1 = new JFrame("Sub Frame");
        f1.getContentPane().setLayout(new BorderLayout());
        f1.getContentPane() .add(new JLabel("label me"), BorderLayout.NORTH);       
        f1.setBounds(600, 500, 300, 300);       
        f1.setDefaultCloseOperation(f1.DISPOSE_ON_CLOSE);

        b1.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                f1.setVisible(true);                
            }

        });     
    }
}

Upvotes: 1

Views: 1985

Answers (2)

Snicolas
Snicolas

Reputation: 38168

Another option would be to recreate j (you modal dilaog) as a non modal dialog and show it after it's clicked. But you risk the user will loose the dialog somewhere between your 2 frames.

I also agree with @Ludowijk : what you are trying to do sounds more or less like a Multiple Document Interface, and it's not such a convenient design...

Stéphane

Upvotes: 1

Loduwijk
Loduwijk

Reputation: 1977

If you have frames f1 and f2 and dialog d where f1 is parent to d is parent to f2, can you just make f2 a dialog instead of a frame? This would give frame f and dialogs d1 and d2, f parent to d1 parent to d2.

Otherwise, it seems you are trying to use the swing tools contrary to how they were intended, so it seems a revisit to the design might be better than trying to force a square peg into a round hole.

Upvotes: 3

Related Questions