如T: Swing中,怎样给JCombox的下拉列表做Tip提示信息,注意是它的下拉列表。点开选项值时,由于宽度不够,所以选项值显示不全,怎样才能给这些选项值加Tip信息?求高人指点方法。不管用什么方法,只要能实现在这种效果。

解决方案 »

  1.   

    要实现这个功能,要实现一个JComboBox的UI,在UI中添加鼠标事件就行了。看代码吧:package swing;import java.awt.Component;
    import java.awt.EventQueue;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;import javax.swing.JComboBox;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.UIManager;
    import javax.swing.plaf.basic.BasicComboBoxUI;public class ComboBoxTooltipTest extends JFrame { private static final long serialVersionUID = 6988055187119043053L; public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    UIManager.setLookAndFeel(UIManager
    .getSystemLookAndFeelClassName());
    ComboBoxTooltipTest frame = new ComboBoxTooltipTest();
    frame.setVisible(true);
    } catch (Exception e) {
    }
    }
    });
    } /**
     * Create the frame
     */
    public ComboBoxTooltipTest() {
    super();
    setTitle("JComboBox Tooltip Test Frame");
    setResizable(false);
    getContentPane().setLayout(null);
    setBounds(100, 100, 500, 375);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JComboBox comboBox = new JComboBox(new String[] { "000", "111",
    "222" });
    comboBox.setUI(new MyComboBoxUI());
    ((MyComboBoxUI) comboBox.getUI()).test(); comboBox.setBounds(44, 75, 106, 27);
    getContentPane().add(comboBox);
    //
    } class MyComboBoxUI extends BasicComboBoxUI {
    public MyComboBoxUI() {
    super();
    } public void test() {
    if (listBox != null) {
    listBox.addMouseMotionListener(new MouseMotionListener() {
    Component oldCom;
    Component curCom; public void mouseMoved(MouseEvent e) {
    curCom = listBox.getCellRenderer()
    .getListCellRendererComponent(listBox, null, 0,
    true, true);
    if (oldCom == null || oldCom != curCom) {
    oldCom = curCom;
    }
    if (oldCom instanceof JComponent) {
    ((JComponent) oldCom)
    .setToolTipText("combo selected "
    + listBox.getSelectedValue());
    }
    } public void mouseDragged(MouseEvent e) {
    }
    });
    }
    }
    }}
      

  2.   

    这个可以,不过需要自己渲染。
    http://www.java2s.com/Code/Java/Swing-Components/ToolTipComboBoxExample.htm
      

  3.   


    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.UIManager;
    import javax.swing.plaf.basic.BasicComboBoxRenderer;public class ToolTipComboBoxExample extends JFrame {  String[] items = { "jw", "ja", "la" };  String[] tooltips = { "Javanese ", "Japanese ", "Latin " };  public ToolTipComboBoxExample() {
        super("ToolTip ComboBox Example");    JComboBox combo = new JComboBox(items);
        combo.setRenderer(new MyComboBoxRenderer());    getContentPane().setLayout(new FlowLayout());
        getContentPane().add(combo);
      }  class MyComboBoxRenderer extends BasicComboBoxRenderer {
        public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
          if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
            if (-1 < index) {
              list.setToolTipText(tooltips[index]);
            }
          } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
          }
          setFont(list.getFont());
          setText((value == null) ? "" : value.toString());
          return this;
        }
      }  public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception evt) {}
      
        ToolTipComboBoxExample frame = new ToolTipComboBoxExample();
        frame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        frame.setSize(200, 140);
        frame.setVisible(true);
      }
    }
      

  4.   

    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.UIManager;
    import javax.swing.plaf.basic.BasicComboBoxRenderer;/**
     * @version 1.0 06/05/99
     */
    public class ToolTipComboBoxExample extends JFrame {  String[] items = { "jw", "ja", "la" };  String[] tooltips = { "Javanese ", "Japanese ", "Latin " };  public ToolTipComboBoxExample() {
        super("ToolTip ComboBox Example");    JComboBox combo = new JComboBox(items);
        combo.setRenderer(new MyComboBoxRenderer());    getContentPane().setLayout(new FlowLayout());
        getContentPane().add(combo);
      }  class MyComboBoxRenderer extends BasicComboBoxRenderer {
        public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
          if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
            if (-1 < index) {
              list.setToolTipText(tooltips[index]);
            }
          } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
          }
          setFont(list.getFont());
          setText((value == null) ? "" : value.toString());
          return this;
        }
      }  public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception evt) {}
      
        ToolTipComboBoxExample frame = new ToolTipComboBoxExample();
        frame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        frame.setSize(200, 140);
        frame.setVisible(true);
      }
    }