Doubt regarding dispose () in JFrame

0

I'm trying to check the login screen to see if the configuration file where the access information is already set exists. If it does not exist, it was supposed to give dispose on the login screen and open the setup.

For some reason, dispose does not work when I put the method on startup, like this:

public class ViewLogin extends javax.swing.JFrame {

public String login;
public String nomeDoUser;
public String usern;

public ViewLogin() {
    initComponents();
    verifyConfig(); <--
    setIcon();
    colorOverlay.setBackground(new Color(51, 51, 51, 155));
    passField.requestFocus();
    this.nomeDoUser = nomeDoUser;
    this.usern = usern;
}

public void verifyConfig() {
    File f = new File("config.ini");
    if (!f.exists()) {
        new ViewConfig().setVisible(true);
        this.dispose();
    }
}

However, if I put on a button, it works perfectly:

private void btnLoginMousePressed(java.awt.event.MouseEvent evt) {                                      
    File f = new File("config.ini");
    if (!f.exists()) {
        new ViewConfig().setVisible(true);
        this.dispose();
    } else {
        UserDAO dao = new UserDAO();
        if (dao.checkLogin(usrField.getText(), passField.getText())) {
            new ViewHome(dao.nomeDoUser, dao.usern).setVisible(true);
            this.dispose();
        } else {
            new ViewLoginError().setVisible(true);
        }
    }
}                

What can I be doing wrong?

    
asked by anonymous 12.07.2017 / 17:39

1 answer

1
The problem is that you have barely built the screen of class ViewLogin and are already wanting to close it, by calling the verifyConfig() method, which, although being another method, is still part of the screen construction inside the constructor. class. The fact that it works in the button listener is because the entire rendering process of the screen is complete at this point.

Note that the screen will only be displayed after the builder finishes because the Swing Thread does many other listeners' record operations until the screen is declared visible.

Forcing this kind of behavior I do not see with good eyes, even more so because if a screen depends on a check to be used or not, it should be checked out of it and not in its constructor.

To show in practice how your code is running, see the example below, where I added some printlns to different phases of class construction, including dispose() in constructor:

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class FrameBlank extends JFrame {

    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            System.out.println("creating frame...");
            FrameBlank frame = new FrameBlank();
            System.out.println("setting visibility...");
            frame.setVisible(true);
        });
    }

    public FrameBlank() {
        initComponents();
        System.out.println("disposing...");
        this.dispose();
    }

    public void initComponents() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(450, 300));
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        pack();
    }
}

The result when opening the screen is always the one below:

  

creating frame ...
  disposing ...
  setting visibility ...

Notice that visibility has not been set and dispose() has already been called, but until that time, the screen ignored it because it was still under construction, and proceeded normally to the rest of the code.

    
12.07.2017 / 17:45