我想显示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]);
你能确定JList的项目长方形可见视区(从JScrollPane的) – mKorbel 2011-05-19 09:59:37
@mKorbel:如何确定? – 2011-05-19 10:06:07
@哈利·乔伊:你是否通过我的回答:) – developer 2011-05-19 10:18:16