Setting Tool Tip Text for items in a JList Component

In this section, you will learn how to set the tool tip
text for items present in the JList component of the Java Swing. Tool Tip text
is the help text of any component for user. When you rest the mouse cursor on
the component then at that point a message which small font and yellow
background stay there for few seconds. This text show the information about that
component.
This program has used the tool tip text for items
present in the JList component in Java Swing. In this program, you can add more
and more items. You can enter the item name in the text box and click on the
"Add" button. When you move the mouse pointer around the items in the
list, it shows the specific item name as a tool tip text like the following
image:

Following are some
methods and APIs are explained as follows:
JScrollPane:
This is the class of javax.swing.*; package of Java Swing. This class
is used to create scroll bar (Horizontal or Vertical) for any component. This
program has used this for creating scroll bar for the text area. It creates
scroll bar using it's constructor which holds the component name for which the
scroll bar has to be created.
DefaultListModel:
This is the class of javax.swing.*; package of Java. This class is
used to create a list model which is helpful for adding items for the list. This
class has used own method to add items in the list.
locationToIndex():
This is the method of the MultiListUI class which is imported from the javax.swing.plaf.multi.*;
package of Java. This method locate the item to the index where the mouse
pointer points. This method takes a integer value for locating item from the
list according to the given point.
getModel():
This is the method of JList class which holds the list of item which are
shown in the JList component of Java Swing. It returns the list model.
getElementAt(index):
This is the method of DefaultListModel class which gets the item from the
returned list model by getModel() method according to the given integer index
no. as parameter.
addElement(String):
This is the method of DefaultListModel class which adds the item into the
list.
Here is the code of the program:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TooltipTextOfList{
private JScrollPane scrollpane = null;
JList list;
JTextField txtItem;
DefaultListModel model;
public static void main(String[] args){
TooltipTextOfList tt = new TooltipTextOfList();
}
public TooltipTextOfList(){
JFrame frame = new JFrame("Tooltip Text for List Item");
String[] str_list = {"One", "Two", "Three", "Four"};
model = new DefaultListModel();
for(int i = 0; i < str_list.length; i++)
model.addElement(str_list[i]);
list = new JList(model){
public String getToolTipText(MouseEvent e) {
int index = locationToIndex(e.getPoint());
if (-1 < index) {
String item = (String)getModel().getElementAt(index);
return item;
} else {
return null;
}
}
};
txtItem = new JTextField(10);
JButton button = new JButton("Add");
button.addActionListener(new MyAction());
JPanel panel = new JPanel();
panel.add(txtItem);
panel.add(button);
panel.add(list);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class MyAction extends MouseAdapter implements ActionListener{
public void actionPerformed(ActionEvent ae){
String data = txtItem.getText();
if (data.equals(""))
JOptionPane.showMessageDialog(null,"Please enter text in the Text Box.");
else{
model.addElement(data);
JOptionPane.showMessageDialog(null,"Item added successfully.");
txtItem.setText("");
}
}
}
}
|
Download this
example.

|