Java: suspend frame while other frame getting information from user - java

I need to get some information from user by showing a JFrame
I need the first frame pause process until user enter data from the second frame
I thought about using wait() and notify() but I don't know how
How can I do this?
Thanks

It seems to that it would be a lot easier for you is you just use a modal JDialog, which you present to the user. The inputs whatever is needed there and the JFrame that popped the dialog will carry on after the dialog's closed. wait() and notify() are used for thread synchronization btw...

Related

When closing the JFrame main method execution should not be stopped [duplicate]

In my program it opens a window if an action is happened. After I have filled out information in this window, and entered a button, the window dispose().
The window is picked up in a program outside my main program, but when I close this window, my main program stops. How can I prevent this from happening?
Thanks for your help!
You can set the defalaultCloseOperation property of the second frame to DO_NOTHING_ON_CLOSE or DISPOSE_ON_CLOSE
Don't even use two frames. Use a modal JDialog instead for a secondary frame. See more at How to Use Dialogs. Read more about Modality. And for a good read, see The Use of Multiple JFrames, Good/Bad Practice?
Forget about number 1. and go straight to 2.
If using JFrame or extending it you can use setDefaultCloseOperation() method like:
frame.setDefaultCloseOperation(HIDE_ON_CLOSE);
// or
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
The dispose command is from the AWT Bundle, and this may cause problems, as you are attempting to "close" a swing window with an AWT command.
You can close the window with this command:
windowName.setVisable(false);
windowName is the name of the object representing the window. If you are extending a class, and have no object, you can use this
More Information on the Dispose Method:
"In general java.awt.Window.dispose() is used on a GUI component that is
a subclass of Window, in order for that specific GUI Window object (and
its children) to properly release and destroy native UI resources (such
as the screen). Garbage collection (and the eventual exiting of the
entire VM) may happen as a result of dispose(), but is not directly
connected with the dispose() call itself." From: https://www.java.net/node/684412
windowName.setVisable(false);
doesn't seems to be a good choice. What if user want to exit the program?
check this question - How to hide a JFrame in system tray of taskbar

Java How to wait for task to finish before returning control

This is a general question because i don't know what to search. But i have the following.
JPanel, put in some user information and press FINISH button, this goes to a JChooser to save the file. After they press save the program goes back to the JPanel and then closes. If there is a lot of user data, the JPanel will come back before the write is finished!
After they give a filename and press save, i want a progress/load bar to indicate the status of the save. How do i do this, how do it wait to finish the save before going back to the JPanel?
What is this called?
You should do several things:
First and foremost, do all the file writing and reading in a background thread so as not to freeze the Swing event thread. A SwingWorker would work well for this.
If you need to display the progress of a long-running process being run in the SwingWorker, then update its progress property within its doInBackground() method as the process runs.
Then add a PropertyChangeListener to the SwingWorker and listen for changes to this "bound" property. The property's name is "progress", so this should be easy.
Then in the above listener, update the value of your JProgressBar.
Next display the progress of the JProgressBar in a modal JDialog or JOptionPane (which is a variant of a modal JDialog). This will prevent the parent window from getting focus or running code until the dialog is no longer visible.

Close window - but don't stop program - JAVA

In my program it opens a window if an action is happened. After I have filled out information in this window, and entered a button, the window dispose().
The window is picked up in a program outside my main program, but when I close this window, my main program stops. How can I prevent this from happening?
Thanks for your help!
You can set the defalaultCloseOperation property of the second frame to DO_NOTHING_ON_CLOSE or DISPOSE_ON_CLOSE
Don't even use two frames. Use a modal JDialog instead for a secondary frame. See more at How to Use Dialogs. Read more about Modality. And for a good read, see The Use of Multiple JFrames, Good/Bad Practice?
Forget about number 1. and go straight to 2.
If using JFrame or extending it you can use setDefaultCloseOperation() method like:
frame.setDefaultCloseOperation(HIDE_ON_CLOSE);
// or
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
The dispose command is from the AWT Bundle, and this may cause problems, as you are attempting to "close" a swing window with an AWT command.
You can close the window with this command:
windowName.setVisable(false);
windowName is the name of the object representing the window. If you are extending a class, and have no object, you can use this
More Information on the Dispose Method:
"In general java.awt.Window.dispose() is used on a GUI component that is
a subclass of Window, in order for that specific GUI Window object (and
its children) to properly release and destroy native UI resources (such
as the screen). Garbage collection (and the eventual exiting of the
entire VM) may happen as a result of dispose(), but is not directly
connected with the dispose() call itself." From: https://www.java.net/node/684412
windowName.setVisable(false);
doesn't seems to be a good choice. What if user want to exit the program?
check this question - How to hide a JFrame in system tray of taskbar

Java Swing JFrame diposition

I have one JFrame. That JFrame has a window listener for closing events which are intercepted and provide the user with options prior to closing the entire program.
Must I call dipose() on a JFrame or am I safe to call only System.exit(0)? Secondly, should I only call dipose() on a JFrame or should I call dipose() followed by System.exit(0)?
My question is must I call dipose() on my JFrame object or can I safely call System.ext(0)?
Why would you ask this? You should solve you compile problem first, and then decide what approach you want to use. Don't use System.exit() just because you don't know how to get rid of a compile error
am using a WindowAdapter as
There is no need for you to keep a variable in order to reference your frame. The best approach to access the frame is to get the frame from the WindowEvent:
JFrame frame = (JFrame)event.getSource();
To answer your original question I would use dispose(). It will eventually invoke System.exit() if it is the last frame open in your application.
Edit:
I want to give the user an option to do something productive before closing the program down
You can check out Closing an Appliction for more ideas on this topic.

Java wait for JFrame to finish

I have a login frame that i have to wait for from another thread. Upon successful login frame disposes itself. And i want to pop up the main frame for the application. Right now i am watching a boolean value to determine when to fire up the main frame. What is the correct way of doing this? Watching a boolean value just does not feel elegant.
If you have Java 5 or later available, you could use a CountDownLatch. For example, assuming the main frame is in control initially, have the main frame create the CountDownLatch with a count down of 1 and pass this latch to the login frame. Then have the main frame wait for the latch to become 0:
CountDownLatch loginSignal = new CountDownLatch(1);
... // Initialize login frame, giving it loginSignal
... // execute login frame in another Thread
// This await() will block until we are logged in:
loginSignal.await();
Have the login frame, when done, decrement the Latch:
loginSignal.countDown();
Ensure that there is no way for your login frame to exit where it forgets to decrement the latch! As soon as the CountDownLatch reaches 0, the main frame becomes runnable.
You could also use a Semaphore or Condition (or any of a few other choices from java.util.concurrent), but for this purpose, a CountDownLatch seems easier to use.
What you really must understand with dealing with Swing (and in fact AWT), is that you need to keep all interaction with the components of the AWT Event Dispatch Thread (EDT).
So, do the login off the EDT. Use a new Thread, or better a java.util.concurrent.ExecutorService. When you discover that you have been logged in, use java.awt.EventQueue.invokeLater to get back onto the EDT. Anonymous inner class are great for capturing context and, despite their horrendously verbose syntax, making the code shorter.

Categories