Java Swing:JTraboBox项目上的鼠标悬停文本? [英] Java Swing: Mouseover text on JComboBox items?
本文介绍了Java Swing:JTraboBox项目上的鼠标悬停文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Swing中,有没有办法为JComboBox中的每个项目定义鼠标悬停文本(或工具提示文本)?
In Swing, is there a way to define mouseover text (or tool tip text) for each item in a JComboBox?
推荐答案
有一种比 ToolTipComboBox
更好的方法答案已经给出。
There is a much better way to do this than the ToolTipComboBox
answer already given.
首先,自定义 ListCellRenderer
:
package com.example;
import javax.swing.*;
import java.awt.*;
import java.util.List;
public class ComboboxToolTipRenderer extends DefaultListCellRenderer {
List<String> tooltips;
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
JComponent comp = (JComponent) super.getListCellRendererComponent(list,
value, index, isSelected, cellHasFocus);
if (-1 < index && null != value && null != tooltips) {
list.setToolTipText(tooltips.get(index));
}
return comp;
}
public void setTooltips(List<String> tooltips) {
this.tooltips = tooltips;
}
}
然后像这样使用:
JComboBox comboBox = new JComboBox();
ComboboxToolTipRenderer renderer = new ComboboxToolTipRenderer();
comboBox.setRenderer(renderer);
...
//make a loop as needed
comboBox.addItem(itemString);
tooltips.add(tooltipString);
...
renderer.setTooltips(tooltips);
这篇关于Java Swing:JTraboBox项目上的鼠标悬停文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文