How to run a method only after a frame has been closed? - java

So, my code right now looks like this:
Class2 className = new Class2(param1, param2);
className = null;
if (Class2 == null) {
refreshState();
}
I want the refreshState method to run once the className object is destroyed. So basically Class2 is a class that runs another frame on top of my existing frame. I want the method to run only when the new frame has been closed. How can I do this?

So basically Class2 is a class that runs another frame on top of my existing frame. I want the method to run only when the new frame has been closed. How can I do this?
The better solution is to use modal dialogs such as a JOptionPane or a modal JDialog. This will stop the processing of the Swing code in the main window until the dialog window has been dealt with and is no longer visible. Then your refreshState can run immediately after the dialog has been closed:
Pseudocode:
main window code
show modal dialog
refresh state here. This will be called only after the dialog has returned.

I would say add a java.awt.event.WindowListener to the frame:
class2.getFrame().addWindowsListener(new WindowAdapter() {
public void windowClosed(WindowEvent we) {
// The frame is closed, let's dot something else
refreshState();
}
}

Related

Pressing twice on a Jbutton

I am a beginner in GUI programming and I am working on a project with a different kind of button.
For one of my Jbutton, when pressed it calls another frame that performs a task.
However, that frame goes on the background when I am working on the main frame.
When you press again the button for the second time a null pointer error is generated.
I want to be able to just bring back the frame that is in the background when the button is pressed for the second time.
changecontrastB.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// calls the contrast adjuster function
ContrastAdjuster mycontrast= new ContrastAdjuster();
// running that function which brings that frame forward
mycontrast.run("Brightness/Contrast...");
mycontrast.setVisible(true);
if (changecontrastB.isSelected() && mycontrast.isVisible()==false )
{
changecontrastB.setEnabled(false);
mycontrast.setVisible(true);
}
}
});
changecontrastB is my actual Jbuton.
Do the following:
In the class with the button, create a member variable of type JFrame:
JFrame frame = null;
In the action listener called from the button, include an if like this (you need to adapt it to your class names):
if (frame = null)
frame = new MyFrame(); //Other initializations might be needed too.
else
frame.toFront();

Closing a JFrame Opened in Another JFrame

I am building an application which I need to open a JFrame in another JFrame while the first JFrame is disabled.
The problem is when I want to close the second JFrame I need to enable the first JFrame.
I have tried in several ways, but it is not working properly and I was not able to reach one goal in each of them. I need to reach both goals:
Disabling the first Frame when the second Frame is opened
Enabling it when the second Frame is closed.
Here is part of my code:
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
PaymentJFrame pjf = new PaymentJFrame();
//setEnableValue(false);
//enableFrame();
this.setEnabled(false);
pjf.setVisible(true);
if (!pjf.isActive()){
this.setEnabled(true);
}
}
This code dose not enable the First Frame at all.
I have tried to use it in another way by adding an enable when the second Frame is closed but it is not working:
//Class in first Frame
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
PaymentJFrame pjf = new PaymentJFrame();
//setEnableValue(false);
//enableFrame();
this.setEnabled(false);
pjf.setVisible(true);
}
//Class in second Frame
private void formWindowClosed(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
FinancialDocumentsJFrame.setEnableValue(true);
}
Dose any one know how can I reach these goals?
The first Frame is the main frame and the second frame is a class that I make the frame object from it in order to show and get more information from users.
I ma using netBeans IDE designer.
First of all, a Swing application should only have one JFrame, other windows can be JDialog or somethign else. As for your question, use this code as an example. It uses a listener to detect the closing event of the second window. The following code should be in the (first) JFrame (it looks like you have a button there)
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
JDialog paymentDialog = new JDialog();
MyFirstFrame.this.setEnabled(false);
paymentDialog.setVisible(true);
paymentDialog.addWindowListener(new WindowAdapter() {
#Override
public void windowClosed(WindowEvent e) {
MyFirstFrame.this.setEnabled(true);
}
});
}
You can create your own dialog by extending JDialog as you did with the frames, and use the custom dialog in this code. Also instead of setting the JFrame enabled or disabled, you could consider using a modal JDialog that blocks actions to the JFrame while the dialog is active.
And further still, there is the setAlwaysOnTop(boolean) for Swing windows.
use this.dispose()method JFrame has dispose() method for JFrame Close
I decided to use jDialog as it was recommended by many people on the net.
I used a jDialog and copied all the objects I had used in the second jFrame.
It worked the way I wanted it to do.
My only problem is that why java hasn't prepared a different way to answer this need.
I'm using an action listener in the first jFrame to open the second Frame which I have used jDialog for it.
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
ActivityJDialog ajdf = new ActivityJDialog(this,true);
ajdf.setVisible(true);
}
And I have copied everything I wanted to have into this jDialog.
public class ActivityJDialog extends java.awt.Dialog {
//Document Attributes
int documentStatus = -1;
int documentType = -1;
/**
* Creates new form AcrivityJDialog
* #param parent
* #param modal
*/
public ActivityJDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
/*if(false) // Full screen mode
{
// Disables decorations for this frame.
//this.setUndecorated(true);
// Puts the frame to full screen.
this.setExtendedState(this.MAXIMIZED_BOTH);
}
else // Window mode
{*/
// Size of the frame.
this.setSize(1000, 700);
// Puts frame to center of the screen.
this.setLocationRelativeTo(null);
// So that frame cannot be resizable by the user.
this.setResizable(false);
//}
}
Thank you all for helping.

Minimize the window (and not to close it) when the user click on the “x” button? What is wrong in this Java Swing code?

I have to change the normal behavior of a class that extend *JFrame.
The new behavior consists in the fact that when the user click on the "X" button the window will not close but it is minimized in the toolbar or the operating system.
So I have a class named MainFrame that extend JFrame and originally is something like this:
public class MainFrame extends JFrame {
public MainFrame() {
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
// do something
}
// OTHER METHODS()
}
So from what I have understand to obtain this behavior I have to change the previous line:
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
into:
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
and in this way the window is not closed when the user click on the X button, then I have to add a listener that minimize the window when the user click on the X button, something like this one:
addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
((JFrame)e.getSource()).setState(JFrame.ICONIFIED);
}
});
I think that this should work but my problem is that I don't know where I have to put it !!!
I tryed to put it into my constructor but Eclipse mark me some error message:
- WindowAdapter cannot be resolved to a type
- The method addWindowListener(WindowListener) in the type Window is not applicable for the arguments (new WindowAdapter()
{})
Why? What am I missing? How can I solve?
Did you import both of the following?
java.awt.event.WindowAdapter;
java.awt.event.WindowEvent;
Especially WindowAdapter...
Also, to your question of where to put the addWindowListener() method, the constructor would be appropriate.

Opening a new GUI window on a main GUI using GUI builder on netbeans

I am playing about with GUI builder and i was wondering if there is an easy way to open the register window through the current main window (in reference to the page below). I am trying to do this through the menu bar.
I've been trying all day, because GUI Builder generates some code, its not possible to edit this code.
Thanks For the help!
Create a separate class which extends JDialog class and add your GUI components:
public Register extends JDialog {
//Make GUI
setModalityType(ModalityType.APPLICATION_MODAL); //Make it modal
}
Add ActionListener to that menu item which is supposed to open a register window:
mnuItmRegisteration.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Register r = new Register();
r.setVisible(true);
}
});
Right click on that shortcut button, click Events, click ActionPreformed.
There you should write codes to make your register window appear.
An example:
private void RegisterationEventActionPerformed(java.awt.event.ActionEvent evt) {
JFrame Register = new Register();
Register.setVisible(true);
}
Remember to make another JFrame called ("Register" assuming u are using the code i gave) at the same package as your current JFrame
Maybe u would probably should use the run button (The button with a Green Triangle or Arrow), run it try to press the menu item, it should appear the register window.

How do i find if a window is opened on swing

I have a problem with my application where the user will open more than one window at a time. And i have added dispose() method to call on closing the window. Now i should keep at-least one window open all the time so that the application does not hides without closed fully. If you don't understand read the following scenario:
I have window A and window B opened at the same time. Now i can close either window A or Window B but not both. In other words window B should be allowed to close only if window A is opened and vice versa. How do i do this in swing ??
A simple kind-of windowManger is not really tricky, all you need is
WindowListener which keeps tracks of the Windows it's listening to
a defined place to create the windows and register the the listener
make the windows do-nothing-on-close and make the listener responsible for the decision of whether to close or not (will do so for all except the last)
Some snippet:
// the listener (aka: WindowManager)
WindowListener l = new WindowAdapter() {
List<Window> windows = new ArrayList<Window>();
#Override
public void windowOpened(WindowEvent e) {
windows.add(e.getWindow());
}
#Override
public void windowClosing(WindowEvent e) {
if (windows.size() > 1) {
windows.remove(e.getWindow());
e.getWindow().dispose();
}
}
};
// create the first frame
JFrame frame = createFrame(l);
frame.setVisible(true);
// a method to create a new window, config and add the listener
int counter = 0;
private JFrame createFrame(final WindowListener l) {
Action action = new AbstractAction("open new frame: " + counter) {
#Override
public void actionPerformed(ActionEvent e) {
JFrame frame = createFrame(l);
frame.setVisible(true);
}
};
JFrame frame = new JFrame("someFrame " + counter++);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.add(new JButton(action));
frame.addWindowListener(l);
frame.pack();
frame.setLocation(counter * 20, counter * 10);
return frame;
}
Just a possible approach...
Create a class, call it WindowManager, that manages creation and disposal of windows.
It could for example retain the count of the windows currently open, and allow a dispose operation only if there are more than one windows "alive", otherwise show a confirm message with JOptionPane telling the user "Really close? That would terminate the application." or something like that.
The "tricky" part is that you have to do this kind of window-related operations throughout the WindowManager, otherwise everything would screw up.
Dunno if Swing has something like this built-in, I've never seen such a scenario.
simply check if the other window is open before closing with window.isVisible();

Categories

Resources