Note that there are some explanatory texts on larger screens.

plurals
  1. POprogrammatically close a JPanel which is displayed in JDialog
    primarykey
    data
    text
    <p>I have a main application frame (<code>MainFrame class</code>). On actionperformed event of a <code>JButton</code>, a <code>JPanel (MyJPanel class)</code> is opened by placing it in <code>JDialog</code>. I am not extending <code>JDialog</code> to create <code>MyJPanel</code> class because I might need MyJPanel at other purposes too.</p> <p>My Problem is I cannot programmatically close the <code>MyJPanel</code> which is displayed in <code>JDialog</code>. Is there anything that I missing? Could you please figure it out?</p> <pre><code>import java.awt.EventQueue; 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.JPanel; import javax.swing.WindowConstants; public class MainFrame extends JPanel { public MainFrame() { JButton btnOpenJdialog = new JButton("Open JDialog"); add(btnOpenJdialog); btnOpenJdialog.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JDialog jd = new JDialog(); MyJPanel mjp = new MyJPanel(true);//showing in JDialog jd.setTitle("JDialog"); jd.add(mjp); jd.pack(); jd.setVisible(true); } }); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } public static void createAndShowGUI() { JFrame frame = new JFrame("Test-JFrame"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.getContentPane().add(new MainFrame()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } } </code></pre> <p>MyJPanel Class :</p> <pre><code>import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JPanel; import javax.swing.JButton; public class MyJPanel extends JPanel { private boolean isShownInJDialog = false; public MyJPanel() { JButton btnCloseMe = new JButton("Finish Action"); add(btnCloseMe); btnCloseMe.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (isShownInJDialog) { MyJPanel.this.setVisible(false); //how to close the JDialog too. } else { //just hide the content, MyJPanel.this.setVisible(false); } } }); } public MyJPanel(boolean isShownInJDialog) { this(); this.isShownInJDialog = isShownInJDialog; } } </code></pre> <p><strong>UPDATE</strong> I was able to solve this using Howard's answer as :</p> <pre><code>... if (isShownInJDialog) { Window w = SwingUtilities.getWindowAncestor(MyJPanel.this); w.setVisible(false); } ... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload