-3

I Want to CLose JFrame after creating it.

(not with button).

but on Constructor

public class ClientSideForm extends javax.swing.JFrame {

    LocalInformation li = new LocalInformation();       
    ClientSetting cs = new ClientSetting();

    public ClientSideForm() throws UnknownHostException {
        initComponents();

        setLocalInfo();
        setDefault();

        try {
            if (!StartApp()) {
                JOptionPane.showMessageDialog(rootPane, "Terjadi Kesalahan Dalam Membuka Aplikasi Utama", "Kesalahan", JOptionPane.ERROR_MESSAGE);                
            } else {

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        this.dispose();
    }
}

If I implemented on button it work perfectly

But I want to close this JFrame after creating it.

Thanks

Denni S
  • 134
  • 1
  • 15
  • 2
    So -- when exactly is it supposed to close, and what event is to trigger its closure? Please tell the details of just what it is you're trying to do. You really don't want to close a GUI object within its own constructor, before it is fully created. – Hovercraft Full Of Eels Aug 29 '17 at 20:00
  • 1
    Also, you will want to read: [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636) – Hovercraft Full Of Eels Aug 29 '17 at 20:01
  • Is your goal to display the JFrame, then the JOptionPane, then when the JOptionPane is no longer visible, dispose of the JFrame? If so, again this should not be done within the JFrame's constructor, before the GUI object, the JFrame, has been fully created, but rather elsewhere within the code that calls the constructor. – Hovercraft Full Of Eels Aug 29 '17 at 20:03
  • Again if so, then the order would be -- 1) create and display your ClientSideForm by calling its constructor and setting it visible, 2) create the JOptionPane by calling the static method -- again not in the ClientSideForm constructor but rather in the code that calls the constructor -- 3) then dispose of the ClientSideForm object after displaying the JOptionPane. – Hovercraft Full Of Eels Aug 29 '17 at 20:08

1 Answers1

2

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());
    }

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373