Pregunta

I have a JFrame class where it's constructor will take a little bit of time (in some occasions) to create all the appropriate objects.

What I want to do is before the main panel shows I will have a popup named toaster where it is going to say at which phase of the program execution I am. Here is my constructor:

public class GUI extends javax.swing.JFrame {        
    public GUI() {
        JOptionPane toasterPane = new JOptionPane(null,
                JOptionPane.INFORMATION_MESSAGE,
                JOptionPane.DEFAULT_OPTION,
                null,
                new Object[]{},
                null);

        JDialog toaster = new JDialog();
        toaster.setTitle("Loading...");
        toaster.setModal(false);
        toaster.setContentPane(toasterPane);
        toaster.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        toaster.pack();
        toaster.setVisible(true);

        toasterPane.setMessage("Initializing Components...");
        initComponents();

        toasterPane.setMessage("Redirecting Console...");
        _mc = new MessageConsole(jMessageConsoleTextPane, true);
        _mc.redirectOut(null, null);
        _mc.redirectErr(Color.RED, null);
        _mc.setMessageLines(100);

        toasterPane.setMessage("Connecting To Database...");
        _user = new User("root", "");
        _connector = new Connector(_user, "localhost", 3306);
        _database = new SQLDatabase("testing", _connector);        
        updateComboBoxes();

        toaster.setVisible(false);
    }
//rest of the code here
}

The problem now is, that instead of showing me the messages it shows just this:

blank

and the moment the program has loaded then it will show the last message (you can see that by commenting out the toaster.setVisible(false)) like this:

message

So any idea what am I doing wrong here?

¿Fue útil?

Solución

You are blocking the Event Dispatch Thread (EDT) so the GUI can't repaint itself until the database access has finished executing. Basically you need to use a separate Thread to access the database.

Read the section from the Swing tutorial on Concurrency for more information and a solution that uses a Swing Worker.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top