Reputation:
I have a parent JDialog opened from button on a JFrame. The parent JDialog itself has a child JDialog that is opened from a button on the parent dialog. When I close the parent dialog and open it again using the button on the frame, I do not want to child dialog to also open.
Is there a way to prevent the child dialog from opening, unless the user explicitly presses on the button on the parent dialog?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MultiDialog {
public static void main(String[] args) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
final JDialog d1 = new JDialog();
final JDialog d2 = new JDialog(d1);
d1.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
d2.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
d1.setSize(new Dimension(150, 100));
d1.setTitle("Parent");
d1.setLocation(50, 50);
d2.setTitle("Child");
d2.setSize(new Dimension(100, 100));
d2.setLocation(150, 150);
JFrame f = new JFrame("App");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b1 = new JButton("Show Parent Dialog");
f.add(b1);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
d1.setVisible(true);
}
});
JButton b2 = new JButton("Show Child Dialog");
d1.add(b2);
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
d2.setVisible(true);
}
});
f.pack();
f.setVisible(true);
}
}
);
}
}
Upvotes: 2
Views: 1204
Reputation: 22005
Add a WindowListener
to your Parent JDialog
and use the windowClosing
method by overiding it.
From now on, when you're closing the Parent, the Child's visible
attribute becomes false until we're clicking on the Parent's button again.
d1.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent arg0) {
d2.setVisible(false);
};
});
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MultiDialog {
public static void main(String[] args) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
final JDialog d1 = new JDialog();
final JDialog d2 = new JDialog(d1);
d1.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
d2.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
d1.setSize(new Dimension(150, 100));
d1.setTitle("Parent");
d1.setLocation(50, 50);
d2.setTitle("Child");
d2.setSize(new Dimension(100, 100));
d2.setLocation(150, 150);
JFrame f = new JFrame("App");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b1 = new JButton("Show Parent Dialog");
f.add(b1);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
d1.setVisible(true);
}
});
d1.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent arg0) {
d2.setVisible(false);
};
});
JButton b2 = new JButton("Show Child Dialog");
d1.add(b2);
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
d2.setVisible(true);
}
});
f.pack();
f.setVisible(true);
}
}
);
}
}
Upvotes: 1