我有一个组合框,其中项目是Objects,其字符串值可能很长。而不是让箱子变得更长,我希望全文出现时鼠标悬浮在上面。我怎样才能做到这一点?将Swing combobox tiptool设置为选定项目
0
A
回答
1
下面是使用custom cell renderer的一个示例。
ToolTipRenderer.addRenderer(jComboBox, foo -> foo.getTheLongString());
(或者你可以复制和重构它,如果你不希望通用/ Java的8东东)
:,因为它是写的,你可以说下面用我的例子
我基本上在这里使用装饰模式,而不是像扩展DefaultListCellRenderer
这样的东西。这种方式如果JComboBox
已经在使用不同的行为保持不变。您真正关心的是设置工具提示的是组件是否为JComponent
的某个子类。
import java.util.Objects;
import java.util.function.Function;
import javax.swing.ListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JComponent;
import java.awt.Component;
public class ToolTipRenderer<E> implements ListCellRenderer<E> {
private final ListCellRenderer<? super E> delegate;
private final Function<E, String> toStringFn;
public ToolTipRenderer(ListCellRenderer delegate,
Function<E, String> toStringFn) {
this.delegate = Objects.requireNonNull(delegate);
this.toStringFn = Objects.requireNonNull(toStringFn);
}
@Override
public Component getListCellRendererComponent(JList<? extends E> list,
E value,
int index,
boolean isSelected,
boolean cellHasFocus) {
Component result =
delegate.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus);
if (result instanceof JComponent) {
// if you don't want to use generics,
// replace this with
// = value.getTheLongString();
String ttText = toStringFn.apply(value);
((JComponent) result).setToolTipText(ttText);
// I was not sure if you wanted
// something like this too.
// if (result instanceof JLabel) {
// ((JLabel) result).setText(value.getTheShortString());
// }
}
return result;
}
// This is an example of how it should be used.
// Pass the ToolTipRenderer the previous renderer
// from comboBox.getRenderer().
public static <E> void addRenderer(JComboBox<E> comboBox,
Function<E, String> toStringFn) {
ListCellRenderer<? super E> delegate = comboBox.getRenderer();
comboBox.setRenderer(new ToolTipRenderer<E>(delegate, toStringFn));
}
}
1
基本的解决方案是提供一个ListCellRenderer
其返回Component
小号toolTipText
属性设置为你需要
DefaultListCellRenderer
从JLabel
扩展了合适的值,所以你可以简单地使用它来提供过什么toolTipText
方法例如,您想要的“扩展”文本。
public class ToolTipListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
// I'd extract the basic "text" representation of the value
// and pass that to the super call, which will apply it to the
// JLabel via the setText method, otherwise it will use the
// objects toString method to generate a representation
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
setToolTipText("To what ever you need based on the value that has been passsed");
return this;
}
}
相关问题
- 1. 将WPF Combobox的选定项目设置为用户设置
- 2. 刷新ListCollectionView将ComboBox中所选项目的值设置为空
- 3. jquery ui combobox设置变量作为选择器名称的选定项目
- 4. Android:在ListView打开时将项目设置为选定项目?
- 5. Sitecore SPEAK UI以编程方式设置ComboBox的选定项目
- 6. 设置ComboBox中选定的项目高亮颜色
- 7. 如何在C#Windows窗体中设置ComboBox的选定项目?
- 8. 将TextWrapping属性设置为ComboBox项目(wpf)
- 9. Combobox - 设置数据源清除项目
- 10. 一个ComboBox的项目如何由另一个ComboBox的选定项目确定?
- 11. 设置所选项目
- 12. 为DataGridView组合框设置选定的项目失败
- 13. 将背景颜色设置为Android中的选定ListView项目
- 14. 为自定义类填充的ComboBox分配选定的项目
- 15. 如何设置XAML Combobox的选定值?
- 16. WPF Combobox - 预选项目
- 17. Combobox绑定和空项目
- 18. 设置WPF组合框的选定项目
- 19. 在GridView中设置选定的项目
- 20. 如何将数据绑定的ComboBox选定值设置为空或默认值?
- 21. 将WPF中Combobox的SelectedIndex设置为0
- 22. 将DataRow设置为ComboBox的值成员
- 23. 将值运行时间设置为combobox
- 24. 将ComboBox Box项目绑定到另一个Combobox的SelectedItem上
- 25. ComboBox SelectedItem未设置为x:绑定
- 26. 设置DataGridComboBoxColumn所选项目
- 27. 选择组合框中的项目并将组合框文本设置为不同的项目?
- 28. Html.DropDownList设置选择项目
- 29. 在组合框中动态设置选定的项目wpf
- 30. 将值设置为ComboBoxs项目#C
看一看[如何使用组合框(https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html )和[提供自定义渲染器](https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer)。自定义渲染器允许您将工具提示应用于应该显示的组件(假设非直接应用于组合框) – MadProgrammer