2017-04-25 57 views
0

我有一个组合框,其中项目是Objects,其字符串值可能很长。而不是让箱子变得更长,我希望全文出现时鼠标悬浮在上面。我怎样才能做到这一点?将Swing combobox tiptool设置为选定项目

+1

看一看[如何使用组合框(https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html )和[提供自定义渲染器](https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer)。自定义渲染器允许您将工具提示应用于应该显示的组件(假设非直接应用于组合框) – MadProgrammer

回答

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属性设置为你需要

DefaultListCellRendererJLabel扩展了合适的值,所以你可以简单地使用它来提供过什么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; 
    } 

} 

How to use comboboxesProviding a custom renderer更多细节

相关问题