0

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

Community
  • 1
  • 1
Peter
  • 213
  • 2
  • 5
  • 14
  • You should post a [Minimal, Complete, Tested and Readable example](http://stackoverflow.com/help/mcve) for faster help – Paul Samsotha Feb 11 '14 at 17:41
  • Following your advice, I have included the code and two pictures. – Peter Feb 11 '14 at 23:54
  • I have managed to fix it but then I encountered another problem. Therefore, I have created a new question http://stackoverflow.com/questions/21727652/jframe-is-not-removed-from-the-taskbar – Peter Feb 12 '14 at 12:15

1 Answers1

0

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.

Community
  • 1
  • 1
Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
  • I am passing null to the constructor of JDialog because a "Dialog must not have an owner to be visible like a top-level window" (http://stackoverflow.com/questions/19116310/showing-jdialog-in-taskbar-not-working?rq=1). My JDialog already has text content in it and is displayed in the center of the desktop. The only problem is the intermediate small window in the top-left corner. This window must be first closed in order for my JDialog to be displayed. – Peter Feb 11 '14 at 17:55
  • I have replaced null with SelectionFrame which extends JFrame. Still it does not work properly. – Peter Feb 11 '14 at 23:57
  • I have managed to fix it but then I encountered another problem. Therefore, I have created a new question http://stackoverflow.com/questions/21727652/jframe-is-not-removed-from-the-taskbar – Peter Feb 12 '14 at 12:16