Reputation: 42987
I am very new in Java Swing development and I have the following problem:
I have this GUI
class that contains the main()
method:
package com.test.login3;
import org.jdesktop.application.SingleFrameApplication;
public class GUI extends SingleFrameApplication {
public static void main(String[] args) {
launch(GUI.class, args);
}
@Override
protected void startup() {
// TODO Auto-generated method stub
System.out.println("GUIBis ---> startUp()");
MainFrame mainFrame = new MainFrame();
mainFrame.setVisible(true);
}
@Override
protected void shutdown() {
System.out.println("Entered into GUI ---> shutdown()");
}
}
As you can see in this class there is the main()
method that simply performs this operation:
launch(GUI.class, args);
Reading on the official documentation: launch doc
Creates an instance of the specified Application subclass, sets the ApplicationContext application property, and then calls the new Application's startup method. The launch method is typically called from the Application's main. The applicationClass constructor and startup methods run on the event dispatching thread.
So the startup()
method is executed and there is created and show a new MainFrame
object
2) This is the MainFrame code (it extends a classic Swing JFrame):
package com.test.login3;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import com.test.login.LoginFrame;
import net.miginfocom.swing.MigLayout;
public class MainFrame extends JFrame {
private static final int FIXED_WIDTH = 1000;
private static final Dimension INITAL_SIZE = new Dimension(FIXED_WIDTH, 620);
public MainFrame() {
super();
setPreferredSize(INITAL_SIZE);
setResizable(false);
setTitle("My Application");
setLayout(new MigLayout("fill"));
JButton logOutButton = new JButton("LogOut");
logOutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// Execute when button is pressed:
System.out.println("You clicked the button");
}
});
add(logOutButton);
pack();
setLocationRelativeTo(null); // Center the window
}
}
My problem is this:
As you can see in the GUI.java
class, a shutdown()
method is definied (that is defined in the SingleFrameApplication
abstact class). Here is the documentation for this method: shutdown doc
Reading the documentation:
Save session state for the component hierarchy rooted by the mainFrame.
When the user clicks on the JButton
that is declared inside the MainFrame
class, I would like the shutdown()
method (that is declared into the GUI
class) to be executed.
Do you have a solution to implement this behavior?
Thank you
Andrea
Upvotes: 0
Views: 572
Reputation: 649
You could use PropertyChanges. Have GUI implement PropertyChangeListener. Then have MainFrame fire a property change when the button is clicked. In GUI, this property change is caught and the shut-down command is executed. See this example for more info.
Something like:
in class GUI:
public class GUI extends SingleFrameApplication implements PropertyChangeListener {
...
MainFrame mainFrame = new MainFrame();
mainFrame.addPropertyChangeListener(this);
...
@Override
public void propertyChange(PropertyChangeEvent arg0) {
if (arg0.getPropertyName().equals("buttonClicked")) {
shutdown();
}
}
Then in MainFrame
public void actionPerformed(ActionEvent e)
{
// Execute when button is pressed:
System.out.println("You clicked the button");
firePropertyChange("buttonClicked", false, true);
}
Upvotes: 2