问题
Following the posts Showing JDialog in taskbar not working and Show JDialog on taskbar I have tried to show my JDialog in the taskbar. Although it worked, the behaviour is strange and it does not look good.
So a button appears in the taskbar, but when I click on it a small window appears in the top left corner of my desktop. This window is so small that its content is not visible. When I enlarge it I can see it's empty. My JDialog only appears after I close this window.
Is there a way to get rid of this window or to embed my JDialog in it?
public class SelectionDialog extends JDialog{
private static final long serialVersionUID = 5677381647525913165L;
private int response = JOptionPane.NO_OPTION;
private SelectionFrame frame = null;
public SelectionDialog(String attachmentName, Long processInstance, String processName) {
super(new SelectionFrame("Selection"));
setModalityType(ModalityType.TOOLKIT_MODAL);
setVisible(true);
toFront();
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
response = JOptionPane.showConfirmDialog(this.getParent(), "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) {
((SelectionFrame)getParent()).dispose();
}
}
public int getUserSelection(){
return response;
}
}
The frame code:
public class SelectionFrame extends JFrame{
private static final long serialVersionUID = -9063300247378170855L;
SelectionFrame(String title) {
super(title);
setUndecorated(true);
setVisible(true);
setLocationRelativeTo(null);
}
}
Then, inside my main 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);
I have also included pictures here: !http://tinypic.com/view.php?pic=mslnyq&s=8#.Uvq17bTpKnc !http://tinypic.com/view.php?pic=33mlbty&s=8
回答1:
Casting null to any type is useless. Pass a real object to the constructor.
Also, try adding some content to the JDialog (a label perhaps)... So that sizing won't be an issue (use an appropriate layout-manager). For location of the JDialog, refer to How do I center a JDialog on screen? or something similar.
Hope this helps.
来源:https://stackoverflow.com/questions/21708752/showing-jdialog-on-taskbar-does-not-look-good