Basic Java Swing, how to exit and dispose of your application/JFrame - java

What is a good way to dispose of a JFrame with code like this? I want to handle the Window exit and window close.
I know we shouldn't use System.exit();
public class JavaCellularAutomataSquare {
public static final String TITLE = "Cellular Automata - Squaring Example";
private int maxWidth = 600;
private int maxHeight = 600;
public void launch() {
final JFrame frame = new JFrame(TITLE);
frame.setLocation(20, 20);
frame.setPreferredSize(new Dimension(maxWidth, maxHeight));
frame.setResizable(false);
frame.setFocusable(true);
final JPanel panel = new JPanel();
panel.setLocation(20, 20);
panel.setVisible(true);
panel.setPreferredSize(new Dimension(maxWidth, maxHeight));
panel.setFocusable(true);
panel.setBackground(Color.white);
// Panel setup, toggle visibility on frame
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ClosingFrame extends JFrame {
private JMenuBar MenuBar = new JMenuBar();
private JFrame frame = new JFrame();
private static final long serialVersionUID = 1L;
private JMenu File = new JMenu("File");
private JMenuItem Exit = new JMenuItem("Exit");
public ClosingFrame() {
File.add(Exit);
MenuBar.add(File);
Exit.addActionListener(new ExitListener());
WindowListener exitListener = new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
int confirm = JOptionPane.showOptionDialog(frame,
"Are You Sure to Close this Application?",
"Exit Confirmation", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (confirm == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
};
frame.addWindowListener(exitListener);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setJMenuBar(MenuBar);
frame.setPreferredSize(new Dimension(400, 300));
frame.setLocation(100, 100);
frame.pack();
frame.setVisible(true);
}
private class ExitListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
int confirm = JOptionPane.showOptionDialog(frame,
"Are You Sure to Close this Application?",
"Exit Confirmation", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (confirm == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
ClosingFrame cf = new ClosingFrame();
}
});
}
}

JFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE) frees up resources when the window is closed. You can see some of the other operations in the Java tutorials here.
The possible arguments that you can use in the method are defined in the WindowConstants interface, if you are curious about your options.

If you need to perform some operations while closing your application, propably you need a
shut down hook. Have a look at this post.

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFrame.setDefaultCloseOperation(int operation)
JFrame.EXIT_ON_CLOSE

Related

java GUI Form open other form onclick button

I am trying to do when clicked button from form1 open form2. Its sounds very simple but i coudnt find any way to do this.I am using java intellij.
When i use netbeans and swing i was doing this with :
"Form2 form2=new Form2();
form2.setVisible(true);
dispose(); "
Form1(Main):
public class Main {
private JButton b_show;
private JButton b_Add;
private JPanel jp_main;
public Main() {
b_show.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent actionEvent) {
}
});
}
public static void main(String[]args){
JFrame frame=new JFrame();
frame.setContentPane(new Main().jp_main);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}
form2(Show):
public class Show {
private JButton b_back;
public JPanel jpanelmain;
public Show() {
Show show=new Show();
geriButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent actionEvent) {
}
});
}
public static void main(String[]args){
JFrame frame=new JFrame();
frame.setContentPane(new Show().jpanelmain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}
is any one can help me ?
when click b_show open form2(Show).
Here is an mcve demonstrating it
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
private final JButton b_show;
private final JPanel jp_main;
public Main() {
jp_main = new JPanel();
b_show = new JButton("Show");
b_show.addActionListener(actionEvent -> {
new Show();
});
jp_main.add(b_show);
}
public static void main(String[]args){
JFrame frame=new JFrame();
frame.setContentPane(new Main().jp_main);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}
class Show {
private JButton b_back;
public JPanel jpanelmain;
public Show() {
createAndShowGui();
}
void createAndShowGui(){
JFrame frame=new JFrame();
frame.setLocationRelativeTo(null);
jpanelmain = new JPanel();
b_back = new JButton("Back");
jpanelmain.add(b_back);
frame.setContentPane(jpanelmain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}
However, please read The Use of Multiple JFrames: Good or Bad Practice?
The best way to do this would be using JDialogs. When actionPerformed() at 'Form1' is called, you would instantiate a new JDialog and set him visible. Here is an example:
public class Show extends JDialog {
private JButton b_back;
public JPanel jpanelmain;
public Show(Frame owner, boolean modal) {
super(owner, modal);
}
//method that creates the GUI
}
b_show.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent actionEvent) {
Show show = new Show(JOptionPane.getFrameForComponent(this), true);
show.setVisible(true);
}
});
Finally, when you want to close the dialog, implement an actionPerformed() in it, and call the dispose() method

Java,Swing - JPanel doesn't appear in JFrame as expected

I got a java class called PleaseWait and want to call it whenever a heavy task is in progress. When my program does a heavy task, in the first row in my actionListener I set a variable of this class setVisible(true) then set setVisible(true) at the end of the actionListener.
Somehow the JPanel in this class does not appear when I call it, it's just a window with title as set and white blank content. Here's my code:
public class PleaseWait extends JFrame{
public PleaseWait(){
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenDimensions = toolkit.getScreenSize();
setSize(300,100); //set size based on screen size
setTitle("Please wait");
Container container = getContentPane();
setLocation(new Point(screenDimensions.width*1/4+200, screenDimensions.height*1/4+200)); //set location based on screen size
JPanel panel = new JPanel();
JLabel wait = new JLabel("Please wait");
Dimension buttonsSize = new Dimension(300,100);
panel.setPreferredSize(buttonsSize);
wait.setPreferredSize(buttonsSize);
panel.setLayout(new BorderLayout());
panel.add(wait, BorderLayout.CENTER);
container.add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false); //unresizable
}
The key is not in the code you've posted, but in this line:
and want to call it whenever a heavy task is in progress
You're running a "heavy" task, and while you're running it, Swing is not painting this GUI, because you're likely running that task on the Swing event thread, and doing so freezes the thread, and your GUI.
Solution: use a background thread such as is obtainable through a SwingWorker, to run the "heavy" task.
Other side issues:
This appears to be a "dependent" or "sub" window off of the main application. If so, it should not be a JFrame since an application should only have one main application window, but rather it should be a JDialog.
You're using setPreferredSize(...) and hard-coding your component sizes, something fraught with problems.
e.g.,
import java.awt.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class TestPleaseWait {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MainPanel mainPanel = new MainPanel();
JFrame frame = new JFrame("Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
#SuppressWarnings("serial")
class MainPanel extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = 450;
public MainPanel() {
add(new JButton(new AbstractAction("Without Background Thread") {
{
putValue(MNEMONIC_KEY, KeyEvent.VK_O);
}
#Override
public void actionPerformed(ActionEvent e) {
final PleaseWait wait = new PleaseWait();
wait.setVisible(true);
try {
Thread.sleep(4000);
} catch (InterruptedException e1) {
}
wait.setVisible(false);
}
}));
add(new JButton(new AbstractAction("With Background Thread") {
private JDialog waitDialog = null;
private MyWaitPanel myWaitPanel = new MyWaitPanel();
{
putValue(MNEMONIC_KEY, KeyEvent.VK_W);
}
#Override
public void actionPerformed(ActionEvent e) {
if (waitDialog == null) {
Component component = MainPanel.this;
Window win = SwingUtilities.getWindowAncestor(component);
waitDialog = new JDialog(win, "Please Wait", ModalityType.APPLICATION_MODAL);
waitDialog.add(myWaitPanel);
waitDialog.pack();
waitDialog.setLocationRelativeTo(win);
}
new Thread(() -> {
try {
Thread.sleep(4000);
} catch (InterruptedException e1) {
}
SwingUtilities.invokeLater(() -> {
waitDialog.setVisible(false);
});
}).start();
waitDialog.setVisible(true);
}
}));
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
}
#SuppressWarnings("serial")
class MyWaitPanel extends JPanel {
private JProgressBar progressBar = new JProgressBar();
public MyWaitPanel() {
progressBar.setIndeterminate(true);
JLabel waitLabel = new JLabel("Please Wait", SwingConstants.CENTER);
waitLabel.setFont(waitLabel.getFont().deriveFont(Font.BOLD, 40));
int ebGap = 10;
setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
setLayout(new BorderLayout(ebGap, ebGap));
add(waitLabel, BorderLayout.PAGE_START);
add(progressBar);
}
}
#SuppressWarnings("serial")
class PleaseWait extends JFrame {
public PleaseWait() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenDimensions = toolkit.getScreenSize();
setSize(300, 100); // set size based on screen size
setTitle("Please wait");
Container container = getContentPane();
setLocation(new Point(screenDimensions.width * 1 / 4 + 200,
screenDimensions.height * 1 / 4 + 200));
JPanel panel = new JPanel();
JLabel wait = new JLabel("Please wait");
Dimension buttonsSize = new Dimension(300, 100);
panel.setPreferredSize(buttonsSize);
wait.setPreferredSize(buttonsSize);
panel.setLayout(new BorderLayout());
panel.add(wait, BorderLayout.CENTER);
container.add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false); // unresizable
}
}

setDefaultCloseOperation to show a JFrame instead

I am making a word processor application in order to practise Java and I would like it so that when the user attempts to close the appliction, a JFrame will come up asking to save changes.
I was thinking about setDefaultCloseOperation() but I have had little luck so far. I would also like it to appear whent he user clicks the "X" on the top right of the window aswell if possible.
You can set the JFrame DefaultCloseOperation to something like DO_NOTHING, and then, set a WindowsListener to grab the close event and do what you want. I'll post an exemple in a few minutes .
EDIT: Here's the example :
public static void main(String[] args) {
final JFrame frame = new JFrame("Test Frame");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setSize(800, 600);
frame.addWindowListener(new WindowAdapter() {
//I skipped unused callbacks for readability
#Override
public void windowClosing(WindowEvent e) {
if(JOptionPane.showConfirmDialog(frame, "Are you sure ?") == JOptionPane.OK_OPTION){
frame.setVisible(false);
frame.dispose();
}
}
});
frame.setVisible(true);
}
import java.awt.event.*;
import javax.swing.*;
public class QuickGuiTest {
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
final JFrame frame = new JFrame("Test Frame");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setSize(600, 400);
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
int result = JOptionPane.showConfirmDialog(
frame, "Are you sure?");
if( result==JOptionPane.OK_OPTION){
// NOW we change it to dispose on close..
frame.setDefaultCloseOperation(
JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(false);
frame.dispose();
}
}
});
frame.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
You have to add a WindowListener to the JFrame.
Inside the windowClosing method, you can provide required code.
For example:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ClosingFrame extends JFrame {
private JMenuBar MenuBar = new JMenuBar();
private JFrame frame = new JFrame();
private static final long serialVersionUID = 1L;
private JMenu File = new JMenu("File");
private JMenuItem Exit = new JMenuItem("Exit");
public ClosingFrame() {
File.add(Exit);
MenuBar.add(File);
Exit.addActionListener(new ExitListener());
WindowListener exitListener = new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
int confirm = JOptionPane.showOptionDialog(frame,
"Are You Sure to Close this Application?",
"Exit Confirmation", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (confirm == JOptionPane.OK_OPTION) {
System.exit(0);
}
}
};
frame.addWindowListener(exitListener);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setJMenuBar(MenuBar);
frame.setPreferredSize(new Dimension(400, 300));
frame.setLocation(100, 100);
frame.pack();
frame.setVisible(true);
}
private class ExitListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
int confirm = JOptionPane.showOptionDialog(frame,
"Are You Sure to Close this Application?",
"Exit Confirmation", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (confirm == JOptionPane.OK_OPTION) {
System.exit(0);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
ClosingFrame cf = new ClosingFrame();
}
});
}
}

Terminate running threads on JFrame close

How do I invoke extra operations when the user closes a JFrame window? I have to stop existing threads.
As I understand it, setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); causes the frame to be closed and its thread to be stopped. Should threads be closed after JFrame.EXIT_ON_CLOSE?
Client:
static boolean TERMINATE = false;
public static void main(String[] args) {
// some threads created
while(true) {
if(TERMINATE){
// do before frame closed
break;
}
}
}
private static JPanel startGUI(){
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel gui = new JPanel();
f.add( gui);
f.setSize(500,500);
f.setVisible(true);
return gui;
}
I need to close sockets the thread's working with. What is the best practice to do that?
Using JFrame.EXIT_ON_CLOSE actually terminates the JVM (System.exit). All running threads will automatically be stopped.
If you want to perform some action when a JFrame is about to close, use a WindowListener.
JFrame frame = ...
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
// close sockets, etc
}
});
You have to add a WindowListener to the JFrame.
Inside the windowClosing method, you can provide required code.
For example:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ClosingFrame extends JFrame {
private JMenuBar MenuBar = new JMenuBar();
private JFrame frame = new JFrame();
private static final long serialVersionUID = 1L;
private JMenu File = new JMenu("File");
private JMenuItem Exit = new JMenuItem("Exit");
public ClosingFrame() {
File.add(Exit);
MenuBar.add(File);
Exit.addActionListener(new ExitListener());
WindowListener exitListener = new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
int confirm = JOptionPane.showOptionDialog(frame,
"Are You Sure to Close this Application?",
"Exit Confirmation", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (confirm == 0) {
System.exit(1);
}
}
};
frame.addWindowListener(exitListener);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setJMenuBar(MenuBar);
frame.setPreferredSize(new Dimension(400, 300));
frame.setLocation(100, 100);
frame.pack();
frame.setVisible(true);
}
private class ExitListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
int confirm = JOptionPane.showOptionDialog(frame,
"Are You Sure to Close this Application?",
"Exit Confirmation", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (confirm == 0) {
System.exit(1);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
ClosingFrame cf = new ClosingFrame();
}
});
}
}
You can set the default close operation on the JFrame
JFrame frame = new JFrame("My Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Java : Swing : Hide frame after button pressed

I have a button in a java frame that when pressed it reads a value from a text field and uses that string as a port name attempting to connect to a serial device.
If this connection is successful the method returns true if not it returns false. If it returns true I want the frame to disappear. A series of other frames specifed in other classes will then appear with options to control the serial device.
My problem is: the button is connected to an action listener, when pressed this method is invoked. If I try to use the frame.setVisible(true); method java throws a abstract button error because I'm effectively telling it to disappear the frame containing the button before the button press method has exited. Removing the frame.setVisible(true); allow the program to run correctly however I am left with a lingering connection frame that is no longer any use.
How to I get the frame to disappear upon pressing a the button?
package newimplementation1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* #author Zac
*/
public class ConnectionFrame extends JPanel implements ActionListener {
private JTextField textField;
private JFrame frame;
private JButton connectButton;
private final static String newline = "\n";
public ConnectionFrame(){
super(new GridBagLayout());
textField = new JTextField(14);
textField.addActionListener(this);
textField.setText("/dev/ttyUSB0");
connectButton = new JButton("Connect");
//Add Components to this panel.
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(connectButton, c);
connectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
boolean success = Main.mySerialTest.initialize(textField.getText());
if (success == false) {System.out.println("Could not connect"); return;}
frame.setVisible(false); // THIS DOES NOT WORK!!
JTextInputArea myInputArea = new JTextInputArea();
myInputArea.createAndShowGUI();
System.out.println("Connected");
}
});
}
public void actionPerformed(ActionEvent evt) {
// Unimplemented required for JPanel
}
public void createAndShowGUI() {
//Create and set up the window.
frame = new JFrame("Serial Port Query");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
//Add contents to the window.
frame.add(new ConnectionFrame());
frame.setLocation(300, 0);
//Display the window.
frame.pack();
frame.setVisible(true);
frame.addComponentListener(new ComponentAdapter() {
#Override
public void componentHidden(ComponentEvent e) {
System.out.println("Exiting Gracefully");
Main.mySerialTest.close();
((JFrame)(e.getComponent())).dispose();
System.exit(0);
}
});
}
}
Running your snippet (after removing/tweaking around the custom classes), throws an NPE. Reason is that the frame you'r accessing is null. And that's because it's never set. Better not rely on any field, let the button find its toplevel ancestor and hide that, like in
public void actionPerformed(final ActionEvent e) {
boolean success = true;
if (success == false) {
System.out.println("Could not connect");
return;
}
Window frame = SwingUtilities.windowForComponent((Component) e
.getSource());
frame.setVisible(false); //no problem :-)
}
Your problem is with this line:
frame.add(new ConnectionFrame());
You're creating a new ConnectionFrame object, and so the frame that your button tries to close on is not the same as the one being displayed, and this is the source of your problem.
If you change it to,
//!! frame.add(new ConnectionFrame());
frame.add(this);
so that the two JFrames are one and the same, things may work more smoothly.
But having said that, your whole design smells bad and I'd rethink it in a more OOP and less static fashion. Also, use dialogs where dialogs are needed, not frames, and rather than dialogs consider swapping views (JPanels) via CardLayout as a better option still.
Myself, I'd create a "dumb" GUI for this, one that creates a JPanel (here in my example it extends a JPanel for simplicity, but I'd avoid extending if not necessary), and I'd let whoever is calling this code decide what to do with the information via some control. For e.g.,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
#SuppressWarnings("serial")
public class ConnectionPanel extends JPanel {
private JTextField textField;
private JButton connectButton;
private ConnectionPanelControl control;
public ConnectionPanel(final ConnectionPanelControl control) {
super(new GridBagLayout());
this.control = control;
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (control != null) {
control.connectButtonAction();
}
}
};
textField = new JTextField(14);
textField.addActionListener(listener);
textField.setText("/dev/ttyUSB0");
connectButton = new JButton("Connect");
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(connectButton, c);
connectButton.addActionListener(listener);
}
public String getFieldText() {
return textField.getText();
}
}
Again, something outside of the simple GUI would make decisions on what to do with the text that the textfield contains and what to do with the GUI that is displaying this JPanel:
public interface ConnectionPanelControl {
void connectButtonAction();
}
Also, you will likely do any connecting in a background thread so as to not freeze your GUI, probably a SwingWorker. Perhaps something like this:
import java.awt.event.ActionEvent;
import java.util.concurrent.ExecutionException;
import javax.swing.*;
#SuppressWarnings("serial")
public class MyMain extends JPanel {
public MyMain() {
add(new JButton(new ConnectionAction("Connect", this)));
}
private static void createAndShowGui() {
JFrame frame = new JFrame("My Main");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyMain());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
#SuppressWarnings("serial")
class ConnectionAction extends AbstractAction {
private MyMain myMain;
private ConnectionPanel cPanel = null;
private JDialog dialog = null;
public ConnectionAction(String title, MyMain myMain) {
super(title);
this.myMain = myMain;
}
#Override
public void actionPerformed(ActionEvent e) {
if (dialog == null) {
dialog = new JDialog(SwingUtilities.getWindowAncestor(myMain));
dialog.setTitle("Connect");
dialog.setModal(true);
cPanel = new ConnectionPanel(new ConnectionPanelControl() {
#Override
public void connectButtonAction() {
final String connectStr = cPanel.getFieldText();
new MySwingWorker(connectStr).execute();
}
});
dialog.getContentPane().add(cPanel);
dialog.pack();
dialog.setLocationRelativeTo(null);
}
dialog.setVisible(true);
}
private class MySwingWorker extends SwingWorker<Boolean, Void> {
private String connectStr = "";
public MySwingWorker(String connectStr) {
this.connectStr = connectStr;
}
#Override
protected Boolean doInBackground() throws Exception {
// TODO: make connection and then return a result
// right now making true if any text in the field
if (!connectStr.isEmpty()) {
return true;
}
return false;
}
#Override
protected void done() {
try {
boolean result = get();
if (result) {
System.out.println("connection successful");
dialog.dispose();
} else {
System.out.println("connection not successful");
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
Your code would be much more readable if you named JFrame instances xxxFrame, and JPanel instances xxxPanel. Naming JPanel instances xxxFrame makes things very confusing.
It would also help if you pasted the stack trace of the exception.
I suspect the problem comes from the fact that frame is null. This is due to the fact that the frame field is only initialized in the createAndShowGUI method, but this method doesn't display the current connection panel, but a new one, which thus have a null frame field:
ConnectionFrame firstPanel = new ConnectionFrame();
// The firstPanel's frame field is null
firstPanel.createAndShowGUI();
// the firstPanel's frame field is now not null, but
// the above call opens a JFrame containing another, new ConnectionFrame,
// which has a null frame field
The code of createAndShowGUI should contain
frame.add(this);
rather than
frame.add(new ConnectionFrame());
for Swing GUI is better create only once JFrame and another Top-Level Containers would be JDialog or JWindow(un-decorated by default),
simple example here
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SuperConstructor extends JFrame {
private static final long serialVersionUID = 1L;
public SuperConstructor() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300, 300));
setTitle("Super constructor");
Container cp = getContentPane();
JButton b = new JButton("Show dialog");
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
FirstDialog firstDialog = new FirstDialog(SuperConstructor.this);
}
});
cp.add(b, BorderLayout.SOUTH);
JButton bClose = new JButton("Close");
bClose.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
add(bClose, BorderLayout.NORTH);
pack();
setVisible(true);
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
SuperConstructor superConstructor = new SuperConstructor();
}
});
}
private class FirstDialog extends JDialog {
private static final long serialVersionUID = 1L;
FirstDialog(final Frame parent) {
super(parent, "FirstDialog");
setPreferredSize(new Dimension(200, 200));
setLocationRelativeTo(parent);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
JButton bNext = new JButton("Show next dialog");
bNext.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
SecondDialog secondDialog = new SecondDialog(parent, false);
}
});
add(bNext, BorderLayout.NORTH);
JButton bClose = new JButton("Close");
bClose.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
setVisible(false);
}
});
add(bClose, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}
private int i;
private class SecondDialog extends JDialog {
private static final long serialVersionUID = 1L;
SecondDialog(final Frame parent, boolean modal) {
//super(parent); // Makes this dialog unfocusable as long as FirstDialog is visible
setPreferredSize(new Dimension(200, 200));
setLocation(300, 50);
setModal(modal);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setTitle("SecondDialog " + (i++));
JButton bClose = new JButton("Close");
bClose.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
setVisible(false);
}
});
add(bClose, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}
}
better would be re-use Top-Level Containers, as create lots of Top-Level Containers on Runtime (possible memory lack)

Categories

Resources