Doubt concerning Jframe Dispose()

Asked

Viewed 411 times

0

I am trying to check the login screen, to know if there is already the configuration file where the access information is set. If it doesn’t exist, it was supposed to give dispose on the login screen and open the configuration screen.

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

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 could I be doing wrong?

1 answer

1

The problem is that you barely built the class screen ViewLogin and are already wanting to close it, by the call of the method verifyConfig(), that even being another method, it is still part of the construction of the screen within the constructor of the class. The fact that it works on the button System is because the entire screen rendering process is complete at this point.

Note that the screen will only appear after the constructor is finished, as the swing thread does many other operations of listener records until the screen is declared visible.

Force this type of behavior I do not see with good eyes, even more because if a screen depends on a check to be used or not, this should be checked outside it and not in your constructor.

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

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 below:

Creating frame...
disposing...
Setting visibility...

Note that the visibility has not even been configured and the dispose() was already called, but until that moment, the screen ignored it because it was still under construction, and gave normal progress to the rest of the code.

  • That’s what I figured @diegofm... Initially the check was before the login screen opened, but for some reason, the design I created, is totally different, as for example the Abels that when they do not disappear, they get totally different fonts.

  • @Wiliansilva the ideal is to check this before giving new in class, so you reduce even the use of resources unnecessarily when not to open the screen.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.