JComboBox 组合框条目渲染(JComboBox添加对象选项)
来源:互联网 发布:硬件软件 编辑:程序博客网 时间:2024/05/21 10:31
如何在JComboBox中添加对象,如果直接将对象加入进去,则只会看到该对象的toString()方法输出的内容。如:
JPanel panel = new JPanel();panel.add(new JLable("选项"));JComboBox comboBox = new JComboBox(new JPanel(){panel});
则会在组合条目中会显示为如下图所示:
要如何才能将对象加入到JComboBox中呢?
先来看一个eclipse中的对字体设置颜色的选择组合选项示意图:
可以看出该下拉列表中每个选项是由前面的颜色显示面板和后面的文字组成,整个选项对象实际上是一个JPanel对象。
通过查看JComboBox 的 API看到 有这个一个方法: void
setRenderer(ListCellRenderer aRenderer)
解释其功能为:
设置渲染器,该渲染器用于绘制列表项和从 JComboBox 字段的列表中选择的项。该渲染器在 JComboBox 不可编辑时使用。如果其可编辑,则使用编辑器呈现和编辑所选项。
默认渲染器显示字符串或图标。其他渲染器可处理图形图像和复合选项。
要显示所选项,请调用 aRenderer.getListCellRendererComponent
,其中传递列表对象和索引 -1。
可以看到默认的渲染器为字符串和图标,所以我们直接加入对象时只能显示出对象的toString()方法返回的内容。
要实现正确的添加对象,就必须调用该方法,设置渲染器。
继承JListCellRenderer 实现自己的CellRenderer类:
class PanelComboBoxCellRenderer implements ListCellRenderer{@Overridepublic Component getListCellRendererComponent(JList list, Object value, int index , boolean isSelected , boolean cellHasFocus ) {if(value instanceof JPanel){if(isSelected){//设置选中时的背景色((JPanel)value).setBackground(Color.LIGHT_GRAY);}else{((JPanel)value).setBackground(Color.white);}return (JPanel)value;}return null;}}
自定义的下拉列表选项对象:
class ColorCell extends JPanel{private Color cellColor; private JPanel colorLabel; private JLabel colorNameLabel; private String colorName;public ColorCell(Color color,String name){this.cellColor = color;this.colorName = name;colorLabel= new JPanel();colorLabel.setBackground(color);colorNameLabel = new JLabel(name);colorNameLabel.setForeground(color);colorLabel.setPreferredSize(new Dimension(20,15));//约束组件大小setLayout(new FlowLayout(FlowLayout.LEFT,2,0));add(colorLabel);add(colorNameLabel);}public Color getCellColor() {return cellColor;}public void setCellColor(Color cellColor) {this.cellColor = cellColor;}public String getColorName() {return colorName;}public void setColorName(String colorName) {this.colorName = colorName;}}
测试类程序:
import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.FlowLayout;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JPanel;import javax.swing.ListCellRenderer;/** * * @author 梦科 * */public class ComboBoxTest {public static void main(String[] args){JFrame frame = new JFrame();JComboBox comboBox = new JComboBox(new JPanel[]{ new ColorCell(Color.red,"红色"), new ColorCell(Color.black ,"黑色"), new ColorCell(Color.blue,"蓝色"), new ColorCell(Color.green,"绿色") });comboBox.setPreferredSize(new Dimension(100,20));ListCellRenderer renderer = new PanelComboBoxCellRenderer();comboBox.setRenderer(renderer);JPanel centPanel = new JPanel();centPanel.add(comboBox);frame.add(centPanel);frame.setSize(new Dimension(200,200));frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}}
效果图如下:
这样就实现了在JComboBox中添加对象的功能。
- JComboBox 组合框条目渲染(JComboBox添加对象选项)
- JComboBox 组合框条目渲染(JComboBox添加对象选项)
- JComboBox 渲染(2012.09.29)
- JCombobox组合框效果实现
- JComboBox
- JcomboBox
- JCombobox
- Jcombobox渲染实例
- 实现JComboBox中添加自定义对象
- [Swing]JCombobox组合框效果实现
- 下拉列表框(JComboBox)
- jcomboBox添加keyPressed事件
- JavaSwing_2.9: JComboBox(下拉列表框)
- 列表框JList和组合列表框JComboBox
- swing 动态添加Jtable(动态添加Jcombobox机制一样)
- 在JTable中实现添加下拉框组件JComboBox
- 动态获取JComboBox组件中的选项 年月日
- JComboBox的下拉框向上
- 带下拉箭头的按钮(JSplitButton)
- ANT标签详解
- NUnit2.0详细使用方法
- 直接按键设置组件的快捷方式
- JTable的自动换行处理
- JComboBox 组合框条目渲染(JComboBox添加对象选项)
- 在.NET环境中使用单元测试工具NUnit
- Dialog里监听ESC按键事件
- SQL Server 2000 和 SQL Server 2005 自动备份攻略
- android ui的几个概念:px,dip(dp),sp,dpi,分辨率等
- java 对象序列化实现复杂UI对象状态的保存。
- 开发X64位系统,不但包含差异,如果使用注册表,注册表也是不同的。
- JScrollPane实现自动滚动到底部
- 不对窗体进行边框装饰后,实现对鼠标事件的监控