I have a JInternalFrame window that needs to popup a modal dialog box when a JButton is pressed. At first, I tried using JDialog, but I found that JDialog constructor needs:
JFrame
boolean modal
I tried passing JInternalFrame to it, but the type didn't match.
Should I use JDialog with JInternalFrame? What if I dont specify the owner (i.e. passing a null)? Is there anything wrong with that?
SwingUtilities.getAncestorOfClass(Window.class, myButton)
The abvove method will return the Window that contains your button.
Once you have it you can pass it to the constructor of the dialog :)
EDIT1:
I misread the question I guess. How can you have a JinternalFrame as the topmost window??
EDIT2:
Also if you pass null to Jdialog constrcutor then by default it uses a shared owner i.e SwingUtilities.getSharedOwnerFrame()
Related
I want to create this element in swing:
As you can see the element is a small grid of buttons which appears when i click on another button. I've tried to use JComboBox to create this element. But as far as i know, JComboBox can just render an image of the some button, but it will not behave itself as a button. Also I couldn't set a GridLayout to JComboBox.
I also tried to create some JDialog, but I suppose that's bad idea.
So the question is: Which swing's component should I use to create mentioned element?
You could use dialog in the best way to achieve this.
JDialog dialog = new JDialog(owner);
dialog.setModalityType(Dialog.ModalityType.MODELESS);
dialog.setUndecorated(true);
You could set Modality type to Modeless to avoid parent frame lock and set undecorated true to make jdialog without close option. But note that you need to close the dialog from program.
How can you create a JDialog when you're in a JDialog?
DialogTest dialog = new DialogTest(this,true);
The above code doesn't work because the first parameter has to be a JFrame, but in this case it's a JDialog.
Thanks in advance!
There are many constructors for a JDialog, one of which, will allow you to pass a Dialog. JDialog is derived from Dialog.
If I am going about this all wrong please let me know.
What I have:
JFrame
JDialog
Main executable
What I want to do
Have a way to open the JDialog (the class extends the JDialog) and have the JFrame pause and wait for the JDialog to be closed before doing anything else. The JDialog is actually just hidden so I can then call a method to pull the data that the user input.
My Issues
Cant figure out how to make the JFrame wait until the JDialog is closed before executing more code
If possible I would like the JFrame to be disabled until the JDialog is closed.
What you're describing is nothing more than using a modal JDialog. When you construct the dialog, when you call its constructor, be sure to set its modality type to ModalityType.APPLICATION_MODAL.
e.g.,
JDialog myDialog = new JDialog(myJFrame, "Dialog",
Dialog.ModalityType.APPLICATION_MODAL);
Please read: How to Use Modality in Dialogs
What is the difference between a JFrame and a JDialog?
Why can't we use setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE); for a JDialog?
JFrame is a normal window with its normal buttons (optionally) and decorations. JDialog on the other side does not have a maximize and minimize buttons and usually are created with JOptionPane static methods, and are better fit to make them modal (they block other components until they are closed).
But both inherit from Window, so they share much functionality.
Why we can't use setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); for JDialog?
Sure you can.
Post your SSCCE that demonstrates the problem you are having when using this value.
However you can't use EXIT_ON_CLOSE for a JDialog because that value is not supported which makes sense since a JDialog is a "child" or "helper" window for your application which is represented by a JFrame. Closing a dialog should not close the application.
There are some JDialog constructors with a owner parameter which can be a Frame, a Dialog or a Window. A non-null value also makes the JDialog stay above his owner. This is complementary of the modal behavior described by Fortran.
You can also use setModal(boolean t);
This only works on JDialog. User must operate on JDialog not other window. If they wanna operate owner windows, they must shut down this JDialog.
I am creating a custom JDialog. I need to hide the JDialog (without removing it from memory) so that its parent can call a method on the JDialog (getResults()).
JDialog dialog = new JDialog(.....);
///Code WITHIN JDialog:
{
//JDialog opens and its actions are performed
this.setVisible(false); //Does this allow the parent to gain focus once more?
}
It depends: whether JDialog modaless is or not. And also if you extend JDialog then:
Yes.
If it will disable focusing other windows, it will release this constraint when the JDialog is hidden. If the JDialog is visible again, it will be impossible to focus the other windows again.