从 JComboBox 渲染器获取工具提示
Get tooltip from JComboBox Renderer
我有一个 ComboBox 渲染器,它扩展了 JPanel 并且有两个标签。
在这里,我只需要在鼠标转到 iconLabel 时显示工具提示。如果鼠标在 labelItem 中,工具提示不应显示。
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.plaf.metal.MetalIconFactory;
public class MyItemRenderer extends JPanel implements ListCellRenderer
{
private JLabel labelItem = new JLabel();
private JLabel iconLabel = new JLabel();
public MyItemRenderer() {
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.weightx = 1.0;
constraints.insets = new Insets(2, 2, 2, 0);
labelItem.setOpaque(true);
labelItem.setHorizontalAlignment(JLabel.LEFT);
iconLabel.setOpaque(true);
iconLabel.setHorizontalAlignment(JLabel.RIGHT);
iconLabel.setPreferredSize(new Dimension(14, 16));
iconLabel.setMaximumSize(new Dimension(14, 16));
add(labelItem, constraints);
GridBagConstraints constraints1 = new GridBagConstraints();
constraints1.weightx = 0;
constraints1.insets = new Insets(0, 0, 0, 2);
add(iconLabel, constraints1);
setBackground(Color.LIGHT_GRAY);
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
String item = (String) value;
labelItem.setText(item);
// set icon
iconLabel.setIcon(MetalIconFactory.getFileChooserDetailViewIcon());
if (isSelected) {
labelItem.setBackground(Color.BLUE);
labelItem.setForeground(Color.YELLOW);
iconLabel.setBackground(Color.BLUE);
iconLabel.setForeground(Color.YELLOW);
} else {
labelItem.setForeground(Color.BLACK);
labelItem.setBackground(Color.LIGHT_GRAY);
iconLabel.setForeground(Color.BLACK);
iconLabel.setBackground(Color.LIGHT_GRAY);
}
return this;
}
}
我的工具提示只有当鼠标悬停在图标上时才需要出现area.That意味着用户需要得到工具提示只有他们want.Please有帮助。
重写 MyItemRenderer
的 getToolTipText(MouseEvent event)
方法,将 MouseEvent
转换为 iconLabel
的坐标 space(或者你想要的任何组件) ).如果 MouseEvent
落在组件的边界内,则 return 不同的结果
类似...
@Override
public String getToolTipText(MouseEvent event) {
String tooltip = super.getToolTipText(event);
Point p = SwingUtilities.convertPoint(this, event.getPoint(), iconLabel);
if (iconLabel.contains(p)) {
tooltip = "I'm a banana";
}
return tooltip;
}
我有一个 ComboBox 渲染器,它扩展了 JPanel 并且有两个标签。 在这里,我只需要在鼠标转到 iconLabel 时显示工具提示。如果鼠标在 labelItem 中,工具提示不应显示。
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.plaf.metal.MetalIconFactory;
public class MyItemRenderer extends JPanel implements ListCellRenderer
{
private JLabel labelItem = new JLabel();
private JLabel iconLabel = new JLabel();
public MyItemRenderer() {
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.weightx = 1.0;
constraints.insets = new Insets(2, 2, 2, 0);
labelItem.setOpaque(true);
labelItem.setHorizontalAlignment(JLabel.LEFT);
iconLabel.setOpaque(true);
iconLabel.setHorizontalAlignment(JLabel.RIGHT);
iconLabel.setPreferredSize(new Dimension(14, 16));
iconLabel.setMaximumSize(new Dimension(14, 16));
add(labelItem, constraints);
GridBagConstraints constraints1 = new GridBagConstraints();
constraints1.weightx = 0;
constraints1.insets = new Insets(0, 0, 0, 2);
add(iconLabel, constraints1);
setBackground(Color.LIGHT_GRAY);
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
String item = (String) value;
labelItem.setText(item);
// set icon
iconLabel.setIcon(MetalIconFactory.getFileChooserDetailViewIcon());
if (isSelected) {
labelItem.setBackground(Color.BLUE);
labelItem.setForeground(Color.YELLOW);
iconLabel.setBackground(Color.BLUE);
iconLabel.setForeground(Color.YELLOW);
} else {
labelItem.setForeground(Color.BLACK);
labelItem.setBackground(Color.LIGHT_GRAY);
iconLabel.setForeground(Color.BLACK);
iconLabel.setBackground(Color.LIGHT_GRAY);
}
return this;
}
}
我的工具提示只有当鼠标悬停在图标上时才需要出现area.That意味着用户需要得到工具提示只有他们want.Please有帮助。
重写 MyItemRenderer
的 getToolTipText(MouseEvent event)
方法,将 MouseEvent
转换为 iconLabel
的坐标 space(或者你想要的任何组件) ).如果 MouseEvent
落在组件的边界内,则 return 不同的结果
类似...
@Override
public String getToolTipText(MouseEvent event) {
String tooltip = super.getToolTipText(event);
Point p = SwingUtilities.convertPoint(this, event.getPoint(), iconLabel);
if (iconLabel.contains(p)) {
tooltip = "I'm a banana";
}
return tooltip;
}