Listening on JTabbedPane - Java

This is a discussion on Listening on JTabbedPane - Java ; How do I add a listener to a JTabbedPane so I know when a tab is selected. I tried this (per suggestion in another post): jTabbedPane.addMouseListener( new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { System.out.println("Clicked"); } }); But that doesn't ...

+ Reply to Thread
Results 1 to 3 of 3

Listening on JTabbedPane

  1. Default Listening on JTabbedPane

    How do I add a listener to a JTabbedPane so I know when a tab is
    selected.

    I tried this (per suggestion in another post):

    jTabbedPane.addMouseListener( new java.awt.event.MouseAdapter()
    {
    public void mouseClicked(MouseEvent e)
    {
    System.out.println("Clicked");
    }
    });

    But that doesn't seem to recognize any clicks on the tabs. (If I click
    on the actual pane, however, it does recognize the clicks.)

    Any other suggestions? Thanks.


  2. Default Re: Listening on JTabbedPane


    "JC" <jason.cavett> wrote in message
    news:1155223606.941847.43070@h48g2000cwc.googlegroups.com...
    > How do I add a listener to a JTabbedPane so I know when a tab is
    > selected.
    >
    > I tried this (per suggestion in another post):
    >
    > jTabbedPane.addMouseListener( new java.awt.event.MouseAdapter()
    > {
    > public void mouseClicked(MouseEvent e)
    > {
    > System.out.println("Clicked");
    > }
    > });
    >
    > But that doesn't seem to recognize any clicks on the tabs. (If I click
    > on the actual pane, however, it does recognize the clicks.)
    >
    > Any other suggestions? Thanks.
    >


    ChangeListener()



  3. Default Re: Listening on JTabbedPane

    JC wrote:
    > How do I add a listener to a JTabbedPane so I know when a tab is
    > selected.
    >
    > I tried this (per suggestion in another post):
    >
    > jTabbedPane.addMouseListener( new java.awt.event.MouseAdapter()
    > {
    > public void mouseClicked(MouseEvent e)
    > {
    > System.out.println("Clicked");
    > }
    > });
    >
    > But that doesn't seem to recognize any clicks on the tabs. (If I click
    > on the actual pane, however, it does recognize the clicks.)
    >
    > Any other suggestions? Thanks.
    >


    As Michael mentioned, you want to implement a ChangeListener. WHen I did
    that I defined the following:

    public void stateChanged(ChangeEvent e) {
    try {
    if (ldapTabs.getSelectedIndex() == 2 ||
    ldapTabs.getSelectedIndex() == 0) {
    menuItemReferrals.setEnabled(true);
    }
    else if (ldapTabs.getSelectedIndex() == 1) {
    menuItemReferrals.setEnabled(false);
    }
    } catch (NullPointerException nullEx) {}
    }

    That let's me disable an option on the menu whenever a certain tab has
    been selected as the active tab. I added the JFrame the tab was in as
    the ChangeListener for the JTabbedPane.

+ Reply to Thread

Similar Threads

  1. Customizing a JTabbedPane?
    By Application Development in forum Java
    Replies: 6
    Last Post: 05-05-2007, 04:10 AM
  2. JTabbedPane
    By Application Development in forum Java
    Replies: 1
    Last Post: 10-22-2006, 04:15 PM
  3. Tabs in JTabbedPane
    By Application Development in forum Java
    Replies: 0
    Last Post: 09-12-2006, 10:28 AM
  4. JTabbedPane woes
    By Application Development in forum Java
    Replies: 2
    Last Post: 05-25-2006, 07:09 AM
  5. Buttons onto JTabbedPane?
    By Application Development in forum Java
    Replies: 1
    Last Post: 05-20-2006, 02:14 AM