I Want to CLose JFrame after creating it.
(not with button).
but on Constructor
No. You don't. You do not want to attempt to display a Swing GUI object and then close it before it has been fully constructed. What I think you want to do (your question is not fully clear on this) is to display the window, display a JOptionPane, and then close the GUI window. If so, you want to do this in the code that calls the GUI object's constructor, not within the constructor, so that you're dealing with a fully realized object. So for example, something like:
ClientSideForm clientForm = new ClientSideForm();
clientForm.setVisible(true);
JOptionPane.showMessageDialog(rootPane, "Terjadi Kesalahan Dalam Membuka Aplikasi Utama",
"Kesalahan", JOptionPane.ERROR_MESSAGE);
clientForm.dispose();
Side notes:
- The button works because even though it is created in a method called by the ClientSideForm constructor, and even though its ActionListener has likely been attached in this same part of your code, it does not perform its actions within the constructor but only afterwards, after the ClientSideForm object has been fully created and displayed, when the button press stimulates its ActionListener's action.
- If your GUI has several JFrames that are displayed and then go away, consider revising the structure that is more user friendly, since most users don't want a bunch of windows pushed to them. Please have a look at The Use of Multiple JFrames, Good/Bad Practice? to see why this is important and to see the alternatives that are available.
- If you want to GUI to be displayed for a set period of time, and then closed, use a Swing Timer for this.
- You are potentially painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.
For example, the following code will show a GUI, and half of the time, will show a JOptionPane error message, and then close the GUI after the error message has been closed, and the other half of the time will show the GUI for 2 seconds and then close it automatically:
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class TestPanel extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = 400;
private static final int TIMER_DELAY = 2 * 1000; // 2 seconds
public TestPanel() {
JLabel label = new JLabel("Test GUI");
label.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 100));
setPreferredSize(new Dimension(PREF_W, PREF_H));
setLayout(new GridBagLayout());
add(label);
}
// this code is called from a main method, but could be called anywhere, from
// a JButton's action listener perhaps. If so, then I'd probably not create
// a new JFrame but rather a JDialog, and place my TestPanel within it
private static void createAndShowGui() {
TestPanel mainPanel = new TestPanel();
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
if (Math.random() > 0.5) {
// 50% chance of this happening
String message = "Error In Opening Main App";
int type = JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(frame, message, "Error", type);
frame.dispose();
} else {
// run timer
new Timer(TIMER_DELAY, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose(); // dispose gui when time's up
((Timer) e.getSource()).stop(); // and stop the timer
}
}).start();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}