JTabbedPane tab selection - How to detect when a user selects a tab

Summary: A JTabbedPane tab selection example.

Here's an example of some code where I'm detecting when a user selects a tab in a JTabbedPane. There are pretty much just three important pieces of code, and I've labeled those with comments and the numbers 1, 2, and 3.

package com.devdaily.myapp.view;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class MyWidgetPanel extends JPanel {
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel northPanel = new JPanel();
  FlowLayout flowLayout1 = new FlowLayout();
  MyWidgetController controller;
  JTabbedPane mainTabbedPanel = new JTabbedPane();
  MyWidgetMapPanel myWidgetMapPanel;
  MyWidgetTablePanel myWidgetTablePanel;

  private String TAB_TITLE_MAP = "Map";
  private String TAB_TITLE_TABLE = "Table";

  //JPanel palettePanel = new JPanel();

  public MyWidgetPanel(MyWidgetController controller,
                      MyWidgetMapPanel myWidgetMapPanel,
                      MyWidgetTablePanel myWidgetTablePanel) {
    this.controller = controller;
    this.myWidgetMapPanel = myWidgetMapPanel;
    this.myWidgetTablePanel = myWidgetTablePanel;
    this.setLayout(borderLayout1);
    northPanel.setLayout(flowLayout1);
    flowLayout1.setAlignment(FlowLayout.LEFT);
    mainTabbedPanel.setTabPlacement(JTabbedPane.LEFT);
    mainTabbedPanel.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
    
    // 1
    mainTabbedPanel.addChangeListener(new MyWidgetPanel_mainTabbedPanel_changeAdapter(this));
    this.add(northPanel, BorderLayout.NORTH);
    this.add(mainTabbedPanel,  BorderLayout.CENTER);
    northPanel.add(formsPalettePanel, null);
    mainTabbedPanel.add(myWidgetMapPanel, TAB_TITLE_MAP);
    mainTabbedPanel.add(myWidgetTablePanel, TAB_TITLE_TABLE);
  }
  
   // 2
  void mainTabbedPanel_stateChanged(ChangeEvent e) {
    JTabbedPane tabSource = (JTabbedPane) e.getSource();
    String tab = tabSource.getTitleAt(tabSource.getSelectedIndex());
    if (tab.equals(TAB_TITLE_TABLE)) {
      controller.updateMapTable();
    }
    if (tab.equals(TAB_TITLE_MAP)) {
      controller.updateMapMap();
    }
  }

}

// 3
class MyWidgetPanel_mainTabbedPanel_changeAdapter implements javax.swing.event.ChangeListener {
  MyWidgetPanel adaptee;

  MyWidgetPanel_mainTabbedPanel_changeAdapter(MyWidgetPanel adaptee) {
    this.adaptee = adaptee;
  }
  public void stateChanged(ChangeEvent e) {
    adaptee.mainTabbedPanel_stateChanged(e);
  }
}

Hopefully you can dig through this "JTabbedPane tab selection" code to see what's really happening here. Here are some brief notes:

  1. Create a JTabbedPane.
  2. Add a change listener to that JTabbedPane (see comment 1).
  3. Create the change listener, which in my case is the class I created by comment 3.
  4. Implement the desired behavior in the stateChanged method. In my case I call the method defined by comment #2.

I hope that's enough to get you going whenever you need to detect that a user has selected a JTabbedPane tab.