现在的位置: 首页 > 综合 > 正文

JComboBox 组合框条目渲染(JComboBox添加对象选项)

2018年01月30日 ⁄ 综合 ⁄ 共 2618字 ⁄ 字号 评论关闭

如何在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{

	@Override
	public 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中添加对象的功能。

抱歉!评论已关闭.