Question

hello i have a Jframe with a CardLayout and 3 cards. I have an ActionListener on a button on the first card.

This code is working well:

JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        CardLayout cl = (CardLayout)(cards.getLayout());
        cl.show(cards, "wait");
    }
}

the problem is when i add code to do the login on the server (i'm developing an xmpp client):

JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        CardLayout cl = (CardLayout)(cards.getLayout());
        cl.show(cards, "wait");
        xmppManager = new Xmpp("jabberserver", 5222);
        try {
            xmppManager.init();
        } catch (XMPPException e) {
            e.printStackTrace();
        }
        cl.show(cards, "userList");
    }
}

Basically, i need to show a "please wait" card when the user press the login button, do the login, and then show another card. But in this case, the "wait" card doesn't show, it do the login (it take around 5 seconds) and it show directly the final card "userList".

What i'm missing?

Was it helpful?

Solution

All the code is executing on the Event Dispatch Thread which is preventing the GUI from repainting itself. You need the call to the server to execute in a separate Thread so you don't block the EDT.

Read the section from the Swing tutorial on Concurrency for more information and a suggested solution.

OTHER TIPS

Perhaps it is necessary to trigger a repaint of the screen after you show the please wait? It might not be triggered automatically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top