JFrame is not removed from the taskbar

a 夏天 提交于 2019-12-12 01:05:07

问题


This question is related to my previous question Showing JDialog on taskbar does not look good. I have modified my code so that the dialog is displayed correctly but now the problem is that the button does not disappear from the taskbar when the dialog is closed. So the application runs, a dialog is displayed and a button is created in the taskbar for it. When I choose YES or NO, the dialog is closed but the button remains in the taskbar. For this reason, buttons add up in the taskbar each time I open and close a dialog. Can someone please help?

The code for the dialog:

public class SelectionDialog extends JDialog{
    private static final long serialVersionUID = 5677381647525913165L;
    private int response = JOptionPane.NO_OPTION;

    public SelectionDialog(String attachmentName, Long processInstance, String processName) {
     super();
     try {
         UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
     } catch (ClassNotFoundException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     } catch (UnsupportedLookAndFeelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }  
     response = JOptionPane.showConfirmDialog(new SelectionFrame("Selection"), "Would you like to apply the policy attachment " + attachmentName + " to current instance (" + processInstance + ") of process " + processName + " ?", "Confirm",
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
     }

    public void setVisible(boolean visible) {
        super.setVisible(visible);
        if (!visible) {
         Frame owner = JOptionPane.getFrameForComponent(this);
         owner.setVisible(false);
         owner.dispose();
        }
    }

    public int getUserSelection(){
        return response;
    }
}

The code for the frame:

public class SelectionFrame extends JFrame{
 private static final long serialVersionUID = -9063300247378170855L;
  SelectionFrame(String title) {
  super(title);
  setUndecorated(true);
  setVisible(true);
  setLocationRelativeTo(null);
 }
}

Then, in my application I use it like this:

SelectionDialog dialog = new SelectionDialog(attachmentDAO.getAttachmentName(), inst.getInstanceId(), this._processId);
int response = dialog.getUserSelection();
if (response == JOptionPane.NO_OPTION) {
    System.out.println("No button clicked");
} else if (response == JOptionPane.YES_OPTION) {
    System.out.println("Yes button clicked");
} else if (response == JOptionPane.CLOSED_OPTION) {
    System.out.println("JOptionPane closed");
}
dialog.setVisible(false);

回答1:


The reason you have an extra icon, has nothing to do with the JDialog. In you JOptionPane, you're creating a new SelectionFrame()

response = JOptionPane.showConfirmDialog(new SelectionFrame("Selection"), 
                         "Would you like to apply the policy attachment " + attachmentName + " to current instance (" + processInstance + ") of process " + processName + " ?", "Confirm",
                          JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

You don't need to do that. You can just pass the SelectionFrame to it. Here's a simple refactor of the constructor for SelectionDialog. It works fine. Also, it looks the the setVisible method is unnecessary. Just call dispose() on it.

public SelectionDialog(final JFrame frame, boolean modal, String attachmentName, Long processInstance, String processName) {
    super(frame, modal);
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }

    response = JOptionPane.showConfirmDialog(frame, "Would you like to apply the policy attachment " + attachmentName + " to current instance (" + processInstance + ") of process " + processName + " ?", "Confirm",
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
}

When you instantiate it, just do it like this

SelectionDialog dialog = new SelectionDialog(SelectionFrame.this, true,...)

Side Note

I honestly see no reason for the JDialog at all if you're just going to use a JOptionPane. You need to decide between one or the other. If you go with the JDialog then don't use the JOptionPane at all, and vice versa.

When you're creating a JDialog, you're ultimately just creating a custom JOptionPane, so when you use a JOptionPane inside a JDialog you're defeating the purpose of using one or the other.



来源:https://stackoverflow.com/questions/21727652/jframe-is-not-removed-from-the-taskbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!