3

I have a panel inside a jtabbed pane that I only want to refresh if the tab is visible. I tried, isShowing(), isDisplayable() and isVisible() and none of them seem to work as they check whether or not the component COULD be showing, displayable or visible.

Preferably from the context of the JPanel inside of the JTabbedPane, how do I tell if the tab the JPanel is in, is the active tab?

Moonbeam
  • 2,283
  • 1
  • 15
  • 17
davidahines
  • 3,976
  • 16
  • 53
  • 87
  • What do you mean "preferably from the context of the JPanel inside of the JTabbedPane"? The JTabbedPane's model itself is what receives notification of a change in selected tab. You can broadcast this to the contained JPanels any way you wish. You may wish to post an [sscce](http://sscce.org) demonstrating your set up and problem. – Hovercraft Full Of Eels Jul 29 '11 at 11:58
  • You could have the JPanel's listen in on the JTabbedPane's model via its ChangeListener and the observer/observable pattern and this will provide your JPanels with the information at the time it happens. – Hovercraft Full Of Eels Jul 29 '11 at 12:13
  • For whatever it's worth, this behavior may have changed sometime in the 8 years between when this was posted and now. The `JTabbedPane` itself calls `setVisible` on the individual tabs when a different tab is selected. Calling `isVisible()` from inside a component tab should properly indicate whether it's the active tab, without needing to interact with the containing tabbed frame itself. – Ti Strga Jun 09 '20 at 20:19

1 Answers1

5

You could get the JTabbedPane's model and add a ChangeListener to it. e.g.,

import java.awt.Dimension;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TabbedListener {
   private static void createAndShowUI() {
      final JTabbedPane tabbedPane = new JTabbedPane();
      for (int i = 0; i < 5; i++) {
         tabbedPane.add("Tab " + i, new JLabel("Label " + i, SwingConstants.CENTER));
      }

      tabbedPane.getModel().addChangeListener(new ChangeListener() {
         @Override
         public void stateChanged(ChangeEvent e) {
            JLabel label = (JLabel) tabbedPane.getSelectedComponent();
            System.out.println(label.getText());
         }
      });

      tabbedPane.setPreferredSize(new Dimension(500, 300));

      JFrame frame = new JFrame("TabbedListener");
      frame.getContentPane().add(tabbedPane);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 3
    You can call `addChangeListener()` directly on the tabbed pane; no need to get the model and add it there. As to what the ChangeListener should do, you can have it `enable` the JPanel associated with the tab. Then configure the JPanel to act differently if it's enabled or disabled. – AngerClown Jul 29 '11 at 13:49