How do i restrict access to other JFrame?
if I open my main frame and when click the button to display other jframe, the user should not be able to go back to the main frame.
how do I do that?
if i open my main frame and click the add button,
When you click the button you display a modal JDialog. Then until the user closes the dialog they won't be able to access the main frame.
try this method...
this.setEnabled(false);
Your question is not clear to me but as per my understanding I believe You want to open a dialog on button click but when you click the button a new JFrame is displayed and it becomes impossible for you to get back to the original frame.
Use a dialog /pop-up on your button click like JOptionPane.
If you want to open a JFrame on button click, making a HOME button on the newly created/opened JFrame and linking that button to main JFrame might be a good option.Closing the newly created JFrame will display the originally created JFrame anyway.
Use a dialog (JDialog class) instead and make it modal.
Here is some help on how to do this:
http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html
Regards,
Related
When two JFrames are enabled, I want the user to be able to use only one in the top (think of that like an error pop up on your screen, when you can't press anything but the popup itself). I am aware of the class JInternalFrame and I chose not to use it for my program. Thanks in advance :)
Use JDialog, you can set your main frame as the JDialog's parent frame, so that whenever your main frame and JDialog will display, you will be able to click only the JDialog, not your main frame.
You want a modal behaviour, then. I think you can try using JDialog instead of JFrame, something like:
JDialog dialog = new JDialog(parentFrame, title, true); //parameters: owner, title and modal
dialog.getContentPane().add(somePanel);
dialog.pack();
dialog.setVisible(true);
You can read more about it here: http://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html
Let me first explain the situation,
I have a class with a JPanel called panelclass.
It's method getPanel() returns the JPanel.
In a JFrame class called frameclass, I create a new object of panelclass, got its panel and added it to the frame pane.
What I am trying to achieve is, when a button in paneclass is clicked, It should close this JFrame ie.frameclass.
I donot know how a panelclass can communicate back to the frameclass to close.
I tried this.dispose() and super.dispose() but was not successful even after extending JFrame
Is there a simpler way?
Please do help.
There are a few was to achieve this, but the simplest is probably through the use of SwingUtilities.getWindowAncestor(Component)
This will return the Window that the component was added to or null if it has no parent window. From there you can simply call Window#dispose to close the frame.
when a button in paneclass is clicked, It should close this JFrame
See Closing an Application. I prefer using something like the `ExitAction' described there. The reason is that your application will behave just like the user clicked on the close button of the frame which means that if you have any WindowListeners added to the window they will be invoked.
I want to disable clicking on the background panel or frame while showing a dialogue. And I want the dialogue to appear on top of this panel or frame constantly until it is closed.
How can I do this?
Make Dialog/JDialog modal by calling dialog.setModal(true);. This will solve both issues of clicking background panel and remaining on top of panel.
It seems like this method is obsolete so better you should use dialog.setModalityType(Dialog.ModalityType type)
You can use JOptionPane for the message dialog.
Does exist in the JDialog class a way to prevent that a child window (JDialog) would be displayed more than once when the button from the main window (JFrame) used to open it, is pressed several times? Many thanks in advance!
Yes, and you don't need to make the box modal to do it (although making it modal would be the easiest way).
Simply do something like the following
In your member delcarations:
private final MyDialog dialog = new MyDialog();
In your code:
private void showDialog() {
dialog.setVisible(true);
dialog.requestFocus(); // May be needed to bring window to front
}
This will ensure that you only instantiate the box once. Simply call showDialog() whenever the button is pressed.
Another way that I've done in the past with Swing is that when the button is pressed the first thing I do is disable the button. Then I use the observable pattern to look at the child window and re-enable the button when the child window is closed. That way if it takes a while to display the child window for some reason the user can't click on it multiple times and mess things up.
You could make the JDialog modal, then the parent window would not react until it is closed.
Or you could initialize the JDialog before, and just make it visible when your button is pressed. Making it visible twice will not display it twice.
we use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) for JFrame and those classes that implements JFrame.
what can we do like this for JFileChooser?(the window that suddenly when you push browse button will pups up)
On a closing JFileChooser you normally don't want to exit your application, at first you want to get the inputs the user made from the dialog. After that you can call System.exit() directly.
You can put a JFileChooser on a custom JFrame and set the default close operation on that JFrame.