Setting a Tool Tip for an Item in a JList Component
import java.awt.event.MouseEvent;
import javax.swing.JList;
publicclass Main {
publicstaticvoid main(String[] argv) throws Exception {
String[] items = { "A", "B", "C", "D" };
JList list = new JList(items) {
// This method is called as the cursor moves within the list.
public String getToolTipText(MouseEvent evt) {
// Get item index
int index = locationToIndex(evt.getPoint());
// Get item
Object item = getModel().getElementAt(index);
// Return the tool tip text
return "tool tip for " + item;
}
};
}
}