I want to show tooltip for the elements in JList. I use setToolTipText(str)
for that. What I want is that tool tip should be displayed at the right side of that element. I use getToolTipLocation(mouseEvent)
but can't get desired result. How can I do this?
Update
My list class:-
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.");
开发者_如何学Go 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;
}
}
My renderer class:-
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;
}
}
Code where I declare lists.
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]);
Post your http://sscce.org when you have a problem. 2 lines of code doesn't show us what you are doing.
Here is a simple 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 );
}
}
精彩评论