Search code examples
javaswingnetbeansjoptionpane

i have displayed a JDialog using JOptionPane.showOptionDialog , can someone tell me how to set it inVisible or Dispose it?


i have displyed this JDialog , and have passed an Object which is a JPanel on it , thus my JDialog displays my JPanel on it when Invoked as required. and on this JPanel I have a JButton, on pressing i want some operations to happen which i have written in it's ActionListener and in the end i have to dispose that JDialog, but i have no clue how to do it !!

This s my JDialog Statement and help me with HOW TO EVEN REMOVE AN ICON from JDialog as even after keeping the ICON PARAMETER NULL it displays the ICON.

JOptionPane.showOptionDialog(null, "SELECT ITEM AND THEN EDIT THE DETAILS.", 
    "EDIT ITEM DETAILS", int1, int2 , null, objEditMorePane, null);

Solution

  • JOptionPane closes its dialog when its value property changes. So, you can obtain the parent JOptionPane and set its value to close the window:

    JOptionPane optionPane = (JOptionPane)
        SwingUtilities.getAncestorOfClass(JOptionPane.class, button);
    optionPane.setValue(JOptionPane.CLOSED_OPTION);
    

    If the window wasn't created by JOptionPane, you can use the getTopLevelAncestor method of JComponent to obtain the parent window:

    Window window = (Window) button.getTopLevelAncestor();
    window.dispose();