秋千上的工具提示位置

时间:2011-05-19 09:54:17

标签: java swing location tooltip jlist

我想在JList中显示元素的工具提示。我使用setToolTipText(str)。我想要的是工具提示应该显示在该元素的右侧。我使用getToolTipLocation(mouseEvent)但无法得到理想的结果。我怎么能这样做?


更新

我的列表类: -

public class MyList extends JList {

    private static final long serialVersionUID = 2170990919011800182L;

    DefaultListModel model;

    public MyList(DefaultListModel model){
        super(model);
        this.model = model;
    }

    public Point getToolTipLocation(MouseEvent e) {
        return new Point(75, locationToIndex(e.getPoint()));
    }

    @Override
    public int getNextMatch(String prefix, int startIndex, Position.Bias bias) {
        if (prefix == null)
            throw new IllegalArgumentException("'prefix' must not be"
                    + " null.");
        if (startIndex < 0)
            throw new IllegalArgumentException("'startIndex' must not"
                    + " be less than zero.");

        if (startIndex >= model.getSize())
            throw new IllegalArgumentException("'startIndex' must not"
                    + " be greater than the number of"
                    + " elements.");

        int result = -1;
        int current = startIndex;
        int delta = -1;
        int itemCount = model.getSize();
        boolean finished = false;
        prefix = prefix.toUpperCase();

        if (bias == Position.Bias.Forward)
            delta = 1;
        while (!finished)
        {
            String itemStr = ((User) model.getElementAt(current)).getName().toString().toUpperCase();
            if (itemStr.indexOf(prefix)!=-1)
                return current;
            current = (current + delta);
            if (current == -1)
                current += itemCount;
            else
                current = current % itemCount;
            if(current == 0)
                finished = true;
            else                    
                finished = current == startIndex;
        }
        return result;
    }
}

我的渲染器类: -

public class MyRender extends JPanel implements ListCellRenderer {

    //--class vars
    public MyRender(Atalk atalk) {
        //--initialize class var
    }

    @Override
    public JComponent getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        //-- put vales in class vars and add them in JPanel
        setToolTipText(user.getName()+"<br/>"user.getAddress());
        return null;
    }
}

我声明列表的代码。

JList[] list = new JList[5];
DefaultListModel[] model = new DefaultListModel[5];
model[0] = new DefaultListModel();
list[0] = new MyList(model[0]);
model[1] = new DefaultListModel();
list[1] = new MyList(model[0]);
model[2] = new DefaultListModel();
list[2] = new MyList(model[0]);
model[3] = new DefaultListModel();
list[3] = new MyList(model[0]);
model[4] = new DefaultListModel();
list[4] = new MyList(model[0]);

1 个答案:

答案 0 :(得分:2)

遇到问题时发布http://sscce.org。 2行代码没有告诉我们你在做什么。

这是一个简单的SSCCE:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ListToolTip extends JFrame
{
    public ListToolTip()
    {
        DefaultListModel model = new DefaultListModel();
        model.addElement("one");
        model.addElement("two");
        model.addElement("three");
        model.addElement("four");
        model.addElement("five");
        model.addElement("six");
        model.addElement("seven");
        model.addElement("eight");
        model.addElement("nine");
        model.addElement("ten");

        JList list = new JList( model )
        {
            public String getToolTipText( MouseEvent e )
            {
                int row = locationToIndex( e.getPoint() );
                Object o = getModel().getElementAt(row);
                return o.toString();
            }

            public Point getToolTipLocation(MouseEvent e)
            {
                int row = locationToIndex( e.getPoint() );
                Rectangle r = getCellBounds(row, row);
                return new Point(r.width, r.y);
            }
        };

        JScrollPane scrollPane = new JScrollPane( list );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
        ListToolTip frame = new ListToolTip();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.setSize(400, 100);
        frame.setVisible( true );
    }
}