Page 1 of 1

All about tool tips in java(adding and getting tooltips) A step by step tutorial... Rate Topic: ***** 1 Votes

#1 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Post icon  Posted 01 January 2009 - 03:25 AM

In almost every program we use, tooltips play an important role in making the program more user freindly, helpful and attractive.
In this tutorial i am going to show you how tooltips can be added to buttons and labels in java programs which is easy...
We are going to use the setToolTipText() method to set the text of a component's tooltip and the getToolTipText() to get the tool tip text of a certain component as this method returns the string that was previously specified by setToolTipText() method.

To add a tool tip text for a certain button or any component method, find the declaration of that component and just under its last declaration add the following code(we will be taking a button as an example for this tutorial):
b1.setToolTipText("This is a tool tip text!");

The addition of the code must be at the declaration of the button:
		b1.setBounds(new Rectangle(213, 180, 118, 24));
		b1.setToolTipText("This is button1");

But sometimes you may want to add a tooltip after a certain action occurs or a button is clicked, you would then use the same method used above but add it under the action of the button(b2 for example) that was clicked to set the tooltip. for example:
  public void b2_actionPerformed(ActionEvent e) {//the action of b2 that will set the tool tip of b1
	b1.setToolTipText("The text of this button was set by an action of b2!");//sets the tool tip of b1

Also the tool tip that is being set for a certain button can be a variable(provided that it is of type string) that is chosen by the user.

Now you will see how we can use the getToolTipText() method to a return a tool tip text as a variable of type String.
The following code will demonstrate a button 3 being clicked to get the tool tip text of button 1 and display it in a jLabel1.
  public void b3_actionPerformed(ActionEvent e) { //action of b3(button 3) that gets and displays tool tip text of b1
	String ToolTip = b1.getToolTipText(); //sets a variable ToolTip with the value of the tool tip text of b1
	jLabel1.setText("The tool tip text of b1 is: " + ToolTip );//displays the tool tip text of b1 into a jLabel

So above you have seen the role of a variable in getting the tool tip text of a certain button, and the role of setting is nearly the same, as in that case the user will have to set a value to the variable via input and then set the tooltip text of a component with the variable's value for example:
 public void b3_actionPerformed(ActionEvent e) { //action of b3(button 3) in this case is getting input from user and setting the tool tip 
//of b1 from the variable that holds the input of the user.
String UserInput =JOptionPane.showInputDialog(this,"Enter the tool tip text you would like to assign to  b1:");
	b1.setToolTipText(UserInput);//sets the tool tip text of b1 as the user has chosen

I hope that this has served as a good tutorial for setting tool tip text in java for components, please do not hesitate to comment and tell me what you think about it, ask questions and report some mistake ;) , Good Luck in your programming and happy new year :)

Is This A Good Question/Topic? 0
  • +

Page 1 of 1