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.
Related
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 have a java swing application. This application contains a mainframe window.
When the user clicks the close (X button on top right of window), my application pops up a JOPtionPane Confirm dialog with yes, no and cancel operations. Clicking on yes saves some files and closes the application, No closes the application without saving the results. This all has been implemented and working fine.
Now i need to implement cancel operation which should typically do something like close the ConfirmDialog and keep the application still open (contrary to this yes and no option closes the application)". I need to implement the idea of "application should not be closed upon clicking the cancel button". For any existing example one can consider the closing of excel sheet (after you edit the excel and try closing without saving).
Set the default close operation for the frame to do nothing:
frame.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE )
Your windowClosing event handler can then literally just return if the user clicks cancel and the program will continue as if nothing has happened.
If the user clicks yes or no then your code will need to programmatically close the frame.
Is there a way to add/override the action event of THIS button ? I couldn't find anywhere how to acces this button and override it's action. In my case I need to do this because I need to save the resources before I exit my window, and if I press the x button, It will exit automatically.
On the JFrame:
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
Then add a WindowsListener and in the windowClosing(WindowEvent event) methods do your job and then call dispose()
I'd use a shut-down hook instead that way it'll save even if you close the app via another method.
Looky here
You can try using a WindowListener interface. If I remember correctly, it should allow you to do things when something on the frame is clicked on(like exiting).
See http://docs.oracle.com/javase/6/docs/api/java/awt/event/WindowListener.html
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,
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.