How to set tooltip for a tab in JTabbedPane container?

Many times we need to set the tooltip for a tab in JTabbedPane container , lets see how we can set the tooltip for the tab in JTabbedPane. There are two ways to set the tooltip for a tab i.e. while creating a tab we can set the tooltip when we create tab itself and the other method is to set the tooltip using method to set the tooltip. Lets see the two ways and how we can implement both the ways.

1. Setting tooltip while creating the tab :
First way is while creating a tab itself we can set the tooltip using the addTab method which takes the tooltip string as an argument.
tabEx.addTab( "1st Tab", null, button, "tooltip for 1st tab" );

In above method as you can see it takes tooltip as an argument to the addTab method.

2. Setting the tooltip using setToolTipTextAt method

We can set the tooltip using the setToolTipText method as shown in following example. In example we are getting the tooltip for the first index and then changing it and again setting it using setToolTipText method which takes tooltip string as an argument and index value of the tab.

      JFrame tabbedFrame = new JFrame( "Tabbed Pane Example" );
      tabbedFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      JTabbedPane tabEx = new JTabbedPane();
      tabbedFrame.add( tabEx, BorderLayout.CENTER );
      JButton button = new JButton( "1st tab selected" );
      //First method
      tabEx.addTab( "1st Tab", null, button, "tooltip for 1st tab"); // add a tooltip
      button = new JButton( "2nd tab selected" );
      tabEx.add( "2nd Tab", button );
      //Second method
      String toolTip = tabEx.getToolTipTextAt( 0 );// Getting the tooltip for first tab
      toolTip = "Changed tooltip for first tab";
      tabEx.setToolTipTextAt( 0, toolTip);// Changing the tooltip for first tab
      tabbedFrame.setSize( 300, 200);
      tabbedFrame.setVisible( true );




You can also check the post to set color of a tab in JTabbedPane container
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment