Detecting Mouse Over Tab on JTabbedPane

Discussion in 'Java' started by Hal Vaughan, Sep 3, 2006.

  1. Hal Vaughan

    Hal Vaughan Guest

    I tried finding this and hope I've missed the obvious or that it's something
    easy I just didn't find.

    I know I can set tool tips for a tab on a JTabbedPane object. I'd like to
    have a "help" JTextArea off to the side of my main interface to use to
    provide more info than I can in a tool tip. Ideally, when the user puts
    the mouse pointer over a tab, I'd like to have a listener that will detect
    that and change the text in the help section. In other words, I need to be
    able to make an actionlistener that will know when the pointer is over a
    tab (not when it changes or is clicked on).

    Is that possible?

    Thanks!

    Hal
     
    Hal Vaughan, Sep 3, 2006
    #1
    1. Advertising

  2. Hal Vaughan schrieb:
    > I tried finding this and hope I've missed the obvious or that it's something
    > easy I just didn't find.
    >
    > I know I can set tool tips for a tab on a JTabbedPane object. I'd like to
    > have a "help" JTextArea off to the side of my main interface to use to
    > provide more info than I can in a tool tip. Ideally, when the user puts
    > the mouse pointer over a tab, I'd like to have a listener that will detect
    > that and change the text in the help section. In other words, I need to be
    > able to make an actionlistener that will know when the pointer is over a
    > tab (not when it changes or is clicked on).
    >
    > Is that possible?


    Sure. It's a MouseMotionListener you'll have to add to the JTabbedPane
    instance, e. g. something like this one:

    MouseMotionListener listener = new MouseMotionAdapter() {
    private int oldIx = -1;

    public void mouseMoved( MouseEvent e ) {
    JTabbedPane tp = (JTabbedPane)e.getSource();
    int ix = tp.indexAtLocation( e.getX(), e.getY() );

    if ( ix == oldIx )
    return;

    oldIx = ix;

    if ( ix == -1 )
    return;

    doSomethingUseful();
    }
    };


    Bye
    Michael
     
    Michael Rauscher, Sep 3, 2006
    #2
    1. Advertising

  3. Hal Vaughan

    Hal Vaughan Guest

    Michael Rauscher wrote:

    > Hal Vaughan schrieb:
    >> I tried finding this and hope I've missed the obvious or that it's
    >> something easy I just didn't find.
    >>
    >> I know I can set tool tips for a tab on a JTabbedPane object. I'd like
    >> to have a "help" JTextArea off to the side of my main interface to use to
    >> provide more info than I can in a tool tip. Ideally, when the user puts
    >> the mouse pointer over a tab, I'd like to have a listener that will
    >> detect
    >> that and change the text in the help section. In other words, I need to
    >> be able to make an actionlistener that will know when the pointer is over
    >> a tab (not when it changes or is clicked on).
    >>
    >> Is that possible?

    >
    > Sure. It's a MouseMotionListener you'll have to add to the JTabbedPane
    > instance, e. g. something like this one:
    >
    > MouseMotionListener listener = new MouseMotionAdapter() {
    > private int oldIx = -1;
    >
    > public void mouseMoved( MouseEvent e ) {
    > JTabbedPane tp = (JTabbedPane)e.getSource();
    > int ix = tp.indexAtLocation( e.getX(), e.getY() );
    >
    > if ( ix == oldIx )
    > return;
    >
    > oldIx = ix;
    >
    > if ( ix == -1 )
    > return;
    >
    > doSomethingUseful();
    > }
    > };
    >
    >
    > Bye
    > Michael


    I tried adding this like this:

    addMouseMotionListener(new MouseMotionAdapter() {...});

    (The ... contained all the code above.) I replaced "doSomethingUseful()"
    with "System.out.println("Tab index: " + ix)" and it never printed
    anything, so I finally added "System.out.println("Movement...");" before
    the line to get the JTabbedPane from the event and never got a printout
    from there.

    Is there another step I need to do to make sure I actually get the info or
    that the listener is called?

    Thanks!

    Hal
     
    Hal Vaughan, Sep 3, 2006
    #3
  4. Hi,

    Hal Vaughan schrieb:
    >
    > (The ... contained all the code above.) I replaced "doSomethingUseful()"
    > with "System.out.println("Tab index: " + ix)" and it never printed
    > anything, so I finally added "System.out.println("Movement...");" before
    > the line to get the JTabbedPane from the event and never got a printout
    > from there.


    Most probably didn't add the mouse motion listener to the tabbed pane...

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

    public class Test {
    private MouseMotionListener listener = new MouseMotionAdapter() {
    private int oldIx = -1;

    public void mouseMoved( MouseEvent e ) {
    JTabbedPane tp = (JTabbedPane)e.getSource();
    int ix = tp.indexAtLocation( e.getX(), e.getY() );

    if ( ix == oldIx )
    return;

    oldIx = ix;

    if ( ix == -1 )
    return;

    System.out.println("You're over tab " + ix );
    }
    };

    private void createAndShowGUI() {
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addMouseMotionListener( listener );

    tabbedPane.addTab( "Tab 1", new JPanel() );
    tabbedPane.addTab( "Tab 2", new JPanel() );

    JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    frame.getContentPane().add( tabbedPane );
    frame.pack();
    frame.setVisible(true);
    }

    public static final void main( String args[] ) {
    new Test().createAndShowGUI();
    }
    }

    Bye
    Michael
     
    Michael Rauscher, Sep 3, 2006
    #4
  5. Hal Vaughan

    Hal Vaughan Guest

    Hal Vaughan wrote:

    > Michael Rauscher wrote:
    >
    >> Hal Vaughan schrieb:
    >>> I tried finding this and hope I've missed the obvious or that it's
    >>> something easy I just didn't find.
    >>>
    >>> I know I can set tool tips for a tab on a JTabbedPane object. I'd like
    >>> to have a "help" JTextArea off to the side of my main interface to use
    >>> to
    >>> provide more info than I can in a tool tip. Ideally, when the user puts
    >>> the mouse pointer over a tab, I'd like to have a listener that will
    >>> detect
    >>> that and change the text in the help section. In other words, I need to
    >>> be able to make an actionlistener that will know when the pointer is
    >>> over a tab (not when it changes or is clicked on).
    >>>
    >>> Is that possible?

    >>
    >> Sure. It's a MouseMotionListener you'll have to add to the JTabbedPane
    >> instance, e. g. something like this one:
    >>
    >> MouseMotionListener listener = new MouseMotionAdapter() {
    >> private int oldIx = -1;
    >>
    >> public void mouseMoved( MouseEvent e ) {
    >> JTabbedPane tp = (JTabbedPane)e.getSource();
    >> int ix = tp.indexAtLocation( e.getX(), e.getY() );
    >>
    >> if ( ix == oldIx )
    >> return;
    >>
    >> oldIx = ix;
    >>
    >> if ( ix == -1 )
    >> return;
    >>
    >> doSomethingUseful();
    >> }
    >> };
    >>
    >>
    >> Bye
    >> Michael

    >
    > I tried adding this like this:
    >
    > addMouseMotionListener(new MouseMotionAdapter() {...});
    >
    > (The ... contained all the code above.) I replaced "doSomethingUseful()"
    > with "System.out.println("Tab index: " + ix)" and it never printed
    > anything, so I finally added "System.out.println("Movement...");" before
    > the line to get the JTabbedPane from the event and never got a printout
    > from there.
    >
    > Is there another step I need to do to make sure I actually get the info or
    > that the listener is called?


    Nevermind this last part. I was using an example I downloaded from online
    for a quick JTabbedPane example and between cutting and pasting in my
    browser to my editor, the indentation got messed up and I lost track of
    which object I was adding the listener to. (HINT: Never name objects, even
    in short code, "myObject" or something too generic and always keep
    indentations lined up!)

    Okay, I had seen the indication of indexAtLocation(), but misunderstood it.
    I thought it would report the entire pane and not just a tab, which would
    be useless, since whenever the mouse was over the pane, it would report the
    number of the active pane. I was also hoping there would be something a
    bit more direct, but this works perfectly. It only reports anything when
    the pointer is directly over a tab, or a -1 if it's not over one. I
    changed the method to:

    int lastIdx = -1;
    JLabel tabIndex = new JLabel("");

    public void mouseMoved( MouseEvent e ) {
    JTabbedPane tp = (JTabbedPane)e.getSource();
    int idx = tp.indexAtLocation( e.getX(), e.getY() );
    if (idx == lastIdx) return;
    tabIndex.setText(String.valueOf(idx));
    return;
    }

    I also just added "implements MouseMotionListener" to the main class I was
    using and added a mouseDragged() method so that'd work so it'd all be in
    one class and the variables and components were easier to track.

    This works as a dry run for what I want to do because it changes the text of
    a component to something specific based on the tab the cursor is over.
    When I get the real thing working, I'll make the JTextArea I'm using for a
    help section start with the help text for the default selected panel. That
    way the user has instant help and the help will change whenever they change
    the tab or mouse over one to change it.

    Thanks for the help! This does exactly what I wanted and is easy to
    implement.

    Hal
     
    Hal Vaughan, Sep 3, 2006
    #5
  6. Hal Vaughan schrieb:
    >
    > I also just added "implements MouseMotionListener" to the main class I was
    > using and added a mouseDragged() method so that'd work so it'd all be in
    > one class and the variables and components were easier to track.


    I'd suggest to create a separate class for that job - without any
    components. This way you can reuse it easily.

    Bye
    Michael
     
    Michael Rauscher, Sep 3, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vertigo

    JTabbedPane and active tab

    vertigo, May 13, 2004, in forum: Java
    Replies:
    1
    Views:
    7,961
    Thomas Weidenfeller
    May 13, 2004
  2. Vincent Cantin
    Replies:
    3
    Views:
    1,578
    Vincent Cantin
    Aug 6, 2004
  3. Mike Bosschaert
    Replies:
    0
    Views:
    628
    Mike Bosschaert
    Jan 7, 2005
  4. Scott Steiner
    Replies:
    2
    Views:
    934
  5. Kent Paul Dolan
    Replies:
    1
    Views:
    457
    Kent Paul Dolan
    Dec 21, 2005
Loading...

Share This Page