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);