Java with many Forms - java

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

Related

Focussing in Java swing

I'm developing an application using java swing. When I click a button, I want another window to be opened. This works fine. But it is possible to alt+tab to the original window and then interact with it even after the new window is open. Is there any way not to let the user focus the original window after the new window appears? By window I'm referring to Jframe/Jdialog.
Assuming the instance of your main JFrame window is called mainWindow:, the following code will prevent switching the focus.
// the second parameter makes the dialog modal and will prevent
// switching the focus to the mainWindow
JDialog dialog = new JDialog(mainWindow, true);
...
dialog.setVisible(true);
Documentation on JDialog: http://docs.oracle.com/javase/6/docs/api/javax/swing/JDialog.html
You may try to use a JDialog instead of JFrame and pass the instance of JFrame to JDialog constructor
You may also try to check
frame.setAlwaysOnTop(true);
or may be like this:-
frame.toFront();
frame.setState(Frame.NORMAL);

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.

how to know if JDialog is closed or not?

I have used a JDialog to display a form ( I could have used JFrame, but I have my reasons). There is an event in my application that will cause a function to generate and display the said JDialog. Now, I want to know if the user has closed that JDialog. How do I find this out?
P.S. My defaultCloseOperation is JDialog.DISPOSE_ON_CLOSE.
Register a window listener on the dialog, and implement the windowClosed method, see The Java Tutorial on Window Listeners

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