JTabbedPane:在显示所选选项卡之前执行的操作 [英] JTabbedPane: Actions performed before displaying selected tab

查看:145
本文介绍了JTabbedPane:在显示所选选项卡之前执行的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当点击 JTabbedPane 中的某个面板时,我需要在开始时执行一些操作。比如说,我需要检查用户名和密码。只有当这些匹配时,才需要执行特定的面板操作。你能建议任何方法吗?

When one of the panels present in a JTabbedPane is clicked, I need to perform a few actions at the start. Say, for example, I need to check the username and password. Only if those match, the particular panel operations need to be performed. Can you suggest any methods?

推荐答案

我不确定我是否完全理解你的问题,但我会这样做:

Not sure I fully understand your question, but I would do something like:


  • 将一个ChangeListener添加到JTabbedPane以监听第一个标签点击。

  • 当发生ChangeEvent时执行登录在使用SwingWorker的后台线程上。

  • 如果登录成功,请在事件派发线程上执行所需的UI操作。

例如:

    tabbedPane.addChangeListener(new ChangeListener() {
    private boolean init;

    public void stateChanged(ChangeEvent e) {
        if (!init) {                                        
            init = true;

            new SwingWorker<Boolean, Void>() {
                @Override
                protected void done() {
                    try {
                        boolean loggedIn = get();

                        if (loggedIn) {
                            // Success so perform tab operations.
                        }
                    } catch (InterruptedException e1) {
                        e1.printStackTrace(); // Handle this.
                    } catch (ExecutionException e1) {
                        e1.printStackTrace(); // Handle this.
                    }
                }

                protected Boolean doInBackground() throws Exception {
                    // Perform login on background thread.  Return true if successful.
                    return true;
                }
            }.execute();
        }
        }
    });

这篇关于JTabbedPane:在显示所选选项卡之前执行的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆