
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set a tooltip text for each item of a JList in Java?
In this article, we will learn to set a tooltip text for each item of a JList in Java. Tooltips give meaningful information as users hover over UI components. Swing's JList does not support tooltips on a per-item basis, so it will involve some customization.
What is JList?
A JList is a subclass of the JComponent class, and it can be used to display a list of objects that allows the user to select one or more items. A JList can generate a ListSelectionListener interface and needs to implement the abstract method valueChanged().
Syntax
The following is the syntax for JList initialization:
JList<String> list = new JList<>(model);
JToolTip class
A JToolTip class is used to display a text or a tip of the component, we can set a tooltip text for each item of a list by implementing the getToolTipText() method of JToolTip class.
Setting a Tooltip Text for a JList
The following is the step-by-step for setting a tooltip text for each item of a JList in Java:
Class Declaration and Imports
The javax.swing.* provides Swing components (JToolTip, JFrame, etc.) while the java.awt.* provides AWT classes (Color, layout managers), and the class extends JFrame to create a window.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class JListTooltipTest extends JFrame {
Instance Variable & Constructor
Declares a Vector to hold the data that will populate the JList. The constructor sets the JFrame window titled "JListTooltip Test" and uses FlowLayout as the layout manager.
private Vector vector; public JListTooltipTest() { setTitle("JListTooltip Test"); setLayout(new FlowLayout());
JList Implementation
Creates an anonymous subclass of JList that overrides getToolTipText().
final JList list = new JList(vector) { public String getToolTipText(MouseEvent me) {
JList Configuration
Sets an empty initial tooltip, wraps the JList in a JScrollPane, and adds the scroll pane to the JFrame.
list.setToolTipText(""); add(new JScrollPane(list));
Main Method
The main method launches the application by creating an object of JListTooltipTest.
public static void main(String[] args) { new JListTooltipTest(); }
Example
Below is an example to set a tooltip text for each item of a JList in Java:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class JListTooltipTest extends JFrame { private Vector vector; public JListTooltipTest() { setTitle("JListTooltip Test"); setLayout(new FlowLayout()); vector = new Vector(); for (int i=1; i < 15; i++) { vector.addElement("Item #" + i); } final JList list = new JList(vector) { public String getToolTipText(MouseEvent me) { int index = locationToIndex(me.getPoint()); if (index > -1) { String item = (String) getModel().getElementAt(index); return "Tooltip for " + item; } return null; } }; list.setToolTipText(""); add(new JScrollPane(list)); setSize(375, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new JListTooltipTest(); } }