如T: Swing中,怎样给JCombox的下拉列表做Tip提示信息,注意是它的下拉列表。点开选项值时,由于宽度不够,所以选项值显示不全,怎样才能给这些选项值加Tip信息?求高人指点方法。不管用什么方法,只要能实现在这种效果。
解决方案 »
- eclipse里面怎样用ant来编译jsp文件
- hql语句中文乱码问题如何解决?
- 怎么实现 帮个忙啊
- struts2中怎样隐藏错误信息的提示?
- js文件,在firefox上报illegal character
- 想不明白Axis2的一个问题
- 关于spring事物的作用
- jboss的 http://localhost:8080/web-console/ 左边框部分出不来,是何原因?
- Error 500--Internal Server Error是什么错误?我快抓狂了
- 我的JDBC连接SQLServer2000的问题!100分!
- 在新窗口打开的问题
- 怎样使用Struts1.x中的令牌(Token)避免注册时重复刷新插入值的问题?
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) {
}
});
}
}
}}
http://www.java2s.com/Code/Java/Swing-Components/ToolTipComboBoxExample.htm
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);
}
}
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);
}
}