I am stuck at a point where I need a Timer which displays some time remaining when i click a button on the Jframe. But the timer frame should be a different frame and should be unclosable. Is there a way to do it?
different frame and should be unclosable.
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
The same can be done for a JDialog.
In both cases the close button will appear on the window, it just won't do anything.
Using a dialog is better than a frame because in general applications should only have a single JFrame and the child dialogs for other windows.
How about creating an undecorated non-modal JDialog?
Related
Is it possible to access a modeless JFrame while a modal JDialog is visible?
I have a JFrame which shows my log lines. When i display a modal JDialog, for example to login, the user can't click the JFrame.
The JFrame isn't a parent/owner of the JDialog.
The JFrame is the first JFrame being created.
Note that when i do something in the JDialog that causes a log line to be added to the log i can see it appear in the JFrame.
Is this how it supposed to work or is it possible to let the user click the JFrame while the modal JDialog is visible?
If the dialog is application modal, e.g., ModalityType.APPLICATION_MODAL, then it will block user interaction with all other top-level windows in the application while the dialog is visible. This does not prevent the application itself from changing the other top level windows states, including any text that they display.
It seems that using ModalityType.DOCUMENT_MODAL is the answer.
Give the JDialog ModalityType.DOCUMENT_MODAL (setModalityType) and make sure setModal is false. The JDialog should also have owner/parent Window.
The JFrame that should always be accessible should have no relation to the JDialog, so don't use it as owner/parent for the JDialog.
Now the JDialog blocks the owner/parent Window but the JFrame is still accessible while the modal JDialog is displayed.
Thanks go out to #Hovercraft Full Of Eels for pointing me to ModalityType.
I have a very simple JFrame window that contains one button: No.
In the main function I set setVisible(true); my JFrame and in the No button listener I want to close the window so I set the visibility to false: setVisible(false); and after that I do System.exit(0); in order to prevent possible memory leaks when running the program many times.
I have two questions:
Do I really need to System.exit(0); in the above case?
If I have this JFrame as a popup window, I can't really use System.exit(0); because this will terminate the whole program. So how can I properly close the popup window and stay in the main JFrame window? (Now I close it only by setVisible(false); and when I do it several times through the program execution, the program turns very slow).
use CardLayout
if is there real reason for another popup container
use JDialog with parent to JFrame, with setModal / ModalityTypes
create only one JDialog and to reuse this one JDialog by getContentPane#removeAll()
use JOptionPane for simple users interaction
put both together, above two points, to use CardLayout for popup JDialog with parent to JFrame, notice after switch from one card to another could be / is required to call JDialog.pack()
setVisible will cause slowdown
dispose will cause slowdown
System.exit will close entire JVM
Therefore, you should reuse a single JFrame or JDialog.
In the button's ActionListener, invoke frame.setVisible(false);. Then instead of creating a new frame just do frame.setVisible(true);. If you want to change the contents of the frame, there is the function frame.getContentPane().removeAll();.
Just add this: JFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE).
Note: The default option for JFrame is HIDE_ON_CLOSE.
You can use the dispose() method of the JFrame class to close the frame and release all resources associated with it, including its child components.
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.
If I have two JFrames where one is the main JFrame, and the other one pops up when I click something. Now what I want is that when the new frame pops up, the main one should be minimized. Also when I click on this popup frame to close it, the main should be restored back.
Essentially I want to know, how can we maximize and minimize a JFrame apart from the default click operation. Is there any function for doing this on custom click for example?
1) setDefaultCloseOperation to NOTHING_ON_CLOSE
2) addWindowListener to JFrame
3) overrive windowsClosing() method with proper Action for iconify ...
4) don't forget to set to the JMenuItem/JButton System.exit(1), because in this form current JVM instence never gone from PC RAM or swap area until PC restarted or switch off
5) better would be to change 2nd. JFrame to the JDialog because in most cases is too hard manage lots of methods betweens two JFrames
setPatent
setModal
setModalityTypes
I have two JFrames. Both are visible at same time.
One JFrame takes the whole screen..its just plain white. (it is acting as a background). And other JFrame is a small box with buttons/texts and other swing components.
The problem I get is when I click the big JFrame area, the JFrame box minimizes. So how do I specify java to make sure the JFrame box is always on top of the JFrame background?
Use a JInternalFrame
Make the JFrame box a JPanel box.
Your application should only have one JFrame.
JFrame is a TopLevel Component and therefore usually you don't put a JFrame into another. If you want to put your smaller jframe into your bigger I would subclass either JDialog or a JPanel.
In general, an application should only have a single JFrame. Other windows should be dialogs.
The problem I get is when I click the big JFrame area, the JFrame box minimizes.
When you use the dialog make sure you specify the frame as the owner of the dialog:
JFrame frame = new JFrame();
JDialog dialog = new JDialog(frame);
If the main frame is ever minimized, the dialog will also be minimized. When the frame is restored the dialog will always display on top of the frame.
use Jdialog with setModal(false) for your small window ,
probably you want something similar to gimp
look at gimp toolbox , only X at title , means its a Dialog.
hope that's help