File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JToolTip displays JList selected text, later Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JToolTip displays JList selected text, later" Watch "JToolTip displays JList selected text, later" New topic

JToolTip displays JList selected text, later

Paul Fenerty

Joined: May 06, 2005
Posts: 23
Hello. I have a Jlist, with width fixed according to �setPrototypeCellValue�. This is good.

New elements can be added to the Jlist, from the GUI, and the length of any given new element may exceed �setPrototypeCellValue�. The behavior for the Jlist in such a case (DefaultListCell renderer) is to clip any text that doesn�t fit as it tries to display the new element, and then append an ellipsis.

This is OK, given the clunkiness of horizontal scroll bars, especially when there are also vertical scroll bars.

It is particularly OK because of the JToolTip facility, which can then be used to display the full text of the truncated element, on demand.

But for the implementation below, the tooltip is not displayed when the element is selected ... it is only displayed after the mouse is subsequently moved off the element. The tooltip then times out in standard fashion, and will reappear with every new mouse move, as long as the mouse is over the Jlist. That�s not OK, very non-intuitive, and I don�t see in the API how to change it. For me at least, it would make a lot more sense for the tooltip to appear at the time that the element is selected.

How to get that behavior??



protected ToolTipJList hostList = new ToolTipJList( hostListModel );

hostList.addListSelectionListener( new HostListListener() );

public class ToolTipJList extends JList
public ToolTipJList(DefaultListModel model)

class HostListListener implements ListSelectionListener {

public void valueChanged(ListSelectionEvent e) {

// code

hostList.setToolTipText( (String) hostList.getSelectedValue() );

// more code


PS: I also tried this using a JTextArea, as below, and got the same behavior.

class MListener extends MouseAdapter {

public void mouseClicked(MouseEvent e) {

ta.setToolTipText( ta.getSelectedText() );
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
seems to work OK this way

I agree. Here's the link:
subject: JToolTip displays JList selected text, later
It's not a secret anymore!