开发者

Strange problem with java CardLayout in JFrame

开发者 https://www.devze.com 2023-02-10 21:50 出处:网络
hello i have a Jframe with a CardLayout and 3 cards. I have an ActionListener on a button on the first card.

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 开发者_JAVA百科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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号