1

I need an event to be fired when, let's say, tab 1 is focused by the user. Any tips on how to do this?

gfe
  • 734
  • 5
  • 19
  • possible duplicate of [JTabbedPane: Actions performed before displaying selected tab](http://stackoverflow.com/questions/1063006/jtabbedpane-actions-performed-before-displaying-selected-tab) – Andreas Dolk Jul 07 '10 at 13:07
  • 3
    Sample code can be found here: http://stackoverflow.com/questions/1063006/jtabbedpane-actions-performed-before-displaying-selected-tab - just add a listener to the tabbed pane and receive the change events. – Andreas Dolk Jul 07 '10 at 13:09
  • The answer I previously write was like that... – sly7_7 Jul 07 '10 at 13:16

1 Answers1

3

This is what I used:

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();
    }
    }
});
gfe
  • 734
  • 5
  • 19