Create a JDialog from a JDialog - java

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.

Related

Java with many Forms

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

Java: Dispose ActionEvent from different JFrame

I want one JFrame to have a method like this:
private void someEvent(java.awt.event.ActionEvent evt){
//initialize another JFrame
//set the new JFrame to be visible
//set this JFrame to be disabled
}
Which is possible, but I also want the main JFrame to perform something when the newly created JFrame is disposed. I don't want to pass in the main JFrame to the new JFrame however. Is this possible?
Instead, use CardLayout to switch between the two desired content panes. There's an example here.
Don't have one JFrame create and display another JFrame. Instead the second window should be a JDialog, either modal if you want the first window frozen until the second has been dealt with or non-modal if otherwise. If modal then the first window's code will resume once the JDialog has been disposed of, and the code flow will start right after the setVisible(true) call on the dialog. If non-Modal, you'll likely want to add a WindowListener to the dialog.
For example, please check out my code here, here, and here.

What is the difference between a JFrame and a JDialog?

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.

Java Swing: JInternalFrame: need a dialog popup

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

Java: If a custom JDialog is hidden, is focus returned back to its parent?

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.

Categories