JDialog Modality Behavior - java

I have a message I want to display with some information in a modeless JDialog. The parent class will call the dispose method to close it at the right point. Here is the code I have:
private static void waitMessage() {
JOptionPane msg=new JOptionPane("Trying to get probes. Please wait ...",
JOptionPane.INFORMATION_MESSAGE,JOptionPane.DEFAULT_OPTION,
null,new Object[]{},null);
waitDialog=msg.createDialog("Probe Scan");
waitDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
waitDialog.setModalityType(ModalityType.MODELESS);
waitDialog.setSize(300,100);
waitDialog.setVisible(true);
}
The problem is that when I set ModalityType.DOCUMENT_MODAL the message displays as expected. Of course the JDialog blocks which is not what I need.
However, when I set ModalityType.MODELESS, I get this:
The JDialog does not block but the message text does not show up. Can someone explain why the ModalityType munges up the pane? TIA.

Manually adding a JOptionPane as the content for a JDialog, doesn't look right to me.
JOptionPane has a method designed to create a relevant JDialog instance, so better try that :
waitDialog = msg.createDialog("Probe Scan");

The reason you cannot close the dialog programmatically is that you do not keep a reference to the dialog. One way to do that is to have the waitMessage() method return the waitDialog instance. Or you can do as #Berger suggests.
Also,
waitDialog.setSize(300,100);
waitDialog.setVisible(true);
waitDialog.pack();
Why call setSize() and also pack() ? Make up your mind - do you want it a specifie size, or do you want it to appear its preferred size? And you should call setVisible() AFTER calling either setSize or pack.
There is no 'SetDefaultCloseOperation' method.
But there is a setDefaultCloseOperation method. (note the lower case first letter)

Related

Make a JDialog always on top of parent (JFrame) but user still can interact with parent

I'm working on building a text editor program like NotePad.
I want to make FindDialog always on top of MainFrame but user still can edit the text at JTextArea in MainFrame as NotePad.
Please help me!!!
I have used method jdialog.setModal(true). It make dialog always on top of parent but user can't edit the text at parent.
Edit: method setAlwaysOnTop() make dialog on top of all windows (include browsers,other programs..) so i can't use it
I have detected that we can use super(parent) to achieve this.
class MyDialog extends JDialog {
public MyDialog(JFrame parent) {
super(parent);
}
/* Other codes */
}
There are various modality types (can be) supported. Use JDialog.setModalityType method and choose the relevant modality type. For more information check out the javadoc here: https://docs.oracle.com/javase/7/docs/api/java/awt/Dialog.html#setModalityType(java.awt.Dialog.ModalityType)
BTW, calling setModal(true) is equivalent to setModalityType(Dialog.ModalityType.MODELESS). In this case user can edit the parent.
So you can try either:
setModalityType(Dialog.ModalityType.DOCUMENT_MODAL)
or as you creating your JDialog pass the modality
new JDialog(parent, "Title", Dialog.ModalityType.DOCUMENT_MODAL);
There is also this useful tutorial about the modality from Oracle:
https://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html
Notice that there is a slight difference between Document and Application modal.
Choose the appropriate one for your case.

Setting JTextField value after panel.setInvisible does not update value

I noticed a behavior that I can't explain. In my GUI, on a button click I display a custom Jdialog that has panel and bunch of textfield. I populate these textfields.
Here is the scenario I am seeing using pseduo code.
public void actionPerformed(ActionEvent e) {
CustomDialog viewDialog = new CustomDialog (Jframe, true);
viewDialog.setVisible(true);
viewDialog.populateInfo();
}
When the code above runs then all textfields are empty. However if I move the setVisible to after the populateInfo method then all the textFields are populated. Basically the JTextField.setText inside the populate info does not seem to have an affect if the setVisible happens before
Why is this!
Likely your CustomDialog class is a modal JDialog (also as suggested by the true 2nd constructor parameter). If so, then program flow in the calling code is blocked by the setVisible(true) call, and so your populateInfo() method will only be called after the dialog is no longer visible. The solution is as you already know -- call the method before displaying the dialog.
This is not a bug but a feature. :)
Seriously, since now you know for a fact when program code flow will be halted and when it will resume, and so you can safely query the dialog for its state after the setVisible(true) has been called, and feel confident that in the very least the dialog has been presented to the user, and the user has had time to interact with it and dispose of it.

How can I pass the parent to use in the JOptionPane that is inside a common method?

I have a JFrame with 2 buttons.
Each of the buttons displays a dialog specific to that button: JDialog A and JDialog B. Each of these dialogs use a class with method M. Inside Method M, I display a JOptionPane.
I tried passing the dialogs as components, but that did not work. I can't use null in the JOptionPane since I need it on top.
How can I pass the parent dialog (A or B) to be used in the JOptionPane?
I was able to fix my problem by reviewing the comments, especially from #Radiodef. The problem was that my object was getting corrupted by another thread so it only worked if I did not pass the object and hard-coded it instead.

Disable publishing JOptionPane on application

Greetings everyone. I have a problem which i can solve. I need that JOptionPane does not showing in my application maybe there some way to make this. Best regards Alejandro Del Rio.
Normally a JOptionPane is shown by using one the the showXXX static methods. Using this approach you don't have a reference to the actual dialog so you can't just hide the option pane.
Read the JOptionPane API documentation. There you will find a "Direct Use" example of using a JOptionPane. In this case you are responsible for more code to handle the showing of the dialog and for handling the selected option button. But you do have a reference to the actual dialog so you can use setVisible( false ) as required.
Of course option panes are modal so you still need to somehow schedule the closing of the dialog, maybe by starting a Swing Timer before the option pane is displayed.
Have you tried setVisible() ?
JOptionPane optionPane = new JOptionPane();
...
...
...
optionPane.setVisible(false);
I just cant to setVisiable();
I have a class which i does not have a source code and there are many not needed Joptionpane shows i want not one of them is not showing in application.

how to ensure that JDialog always stays on top

I have a JDialog that takes a name from the user. Behind the JDialog, is an applet. I dont want the user to access that applet until he has entered the name. I tried JDialog.setAlwaysOnTop(true), but the applet throws an AccessException error. So what I did was keep a while loop that will execute JDialog.setVisible(true) till the JtextField(input for user name) is empty (""). But for some reason this works really slow, meaning the JDialog loads, but it takes time to focus on the JTextField and even when the user types his name, it comes really slow... like one character in 2 seconds... Is there any other way for me to force the user to enter the name before accessing the applet?
Use a modal JDialog. For example the code in your init(...) method of JApplet might include:
JDialog dialog = new JDialog(SwingUtilities.windowForComponent(this));
dialog.setModal(true);
dialog.setSize(...);
dialog.setVisible( true );
Or you can just use a JOptionPane.showInputDialog(). Again you would just specify "this" as the parent component of the option pane.
Another option would be:
frame.setAlwaysOnTop(true);
It forces the dialog on top of any other.
It runs slowly because the program is processing that foo loop
What you can do is to add a window listener and then the jdialog lost it's focus ( or the applet gains it ) return the focus to the jdialog.
This should perform much better than the for loop you're using right now

Categories