选中选项卡时会触发JTabbedPane事件

时间:2010-07-07 13:03:41

标签: java event-handling jtabbedpane

我需要触发一个事件,比方说,标签1由用户关注。关于如何做到这一点的任何提示?

1 个答案:

答案 0 :(得分:3)

这就是我使用的:

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();
    }
    }
});