Reputation: 167
I have a JFileChooser
.
I would like it to display a ToolTipText
(which will be the filename) for each file in the JList
, when the mouse points over it.
It looks that I will need to override JList::getToolTipText()
, but I'm having trouble obtaining file chooser's list (and then overriding the getToolTipText()
method).
I mean, do I have to create a new class that extends a JLis
t, then override the method in that class, then replace file chooser's JList
by the new class instance I made?
Do I need to access the JList ListModel
attribute?
I made some research. These links might be useful:
Upvotes: 0
Views: 168
Reputation: 167
This resolved my problem. Thanks to camickr for helping.
1- Use SwingUtils getDescendantOfType
to obtain the list
2- Add a mouse listener to display ToolTipText
jList.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
JList l = (JList)e.getSource();
ListModel m = l.getModel();
int index = l.locationToIndex(e.getPoint());
if( index>-1 ) {
l.setToolTipText(m.getElementAt(index).toString());
}
}
});
Upvotes: 1