JComboBox 禁用某个项目 并 置灰

本文介绍了一种在Java Swing中使用JComboBox时禁用并灰显某些选项的方法。通过继承JComboBox并自定义渲染器,可以实现指定项的禁用及视觉效果。示例代码展示了如何设置和管理这些禁用项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近因为项目需要,针对JComboBox,在特定情况下,某一项变灰 且 不可用。

中文资料特别少,最后在国外网站上找到了解决方案:继承JComboBox 并设置了渲染器。

程序代码

import java.awt.Component;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

public class DisabledItemsComboboxDemo extends JFrame {

	public static void main(String[] args) {
		DisabledItemsComboboxDemo frame = new DisabledItemsComboboxDemo(
				"Combobox with disabled items");
		frame.setVisible(true);
	}

	public DisabledItemsComboboxDemo(String title) {
		super(title);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.init();
	}

	private void init() {
		this.setSize(500, 50);
		DisabledItemsComboBox box = new DisabledItemsComboBox();
		box.addItem("This item is disabled but selected at first appearance",
				true);
		box.addItem("This item is disabled and you cannot select it", true);
		box.addItem("This is the first selectable item");
		box.addItem("This is the second selectable item");
		box.addItem("This is the third selectable item");
		add(box);
	}

	private class DisabledItemsComboBox extends JComboBox {

		public DisabledItemsComboBox() {
			super();
			super.setRenderer(new DisabledItemsRenderer());
		}

		private Set<Object> disabled_items = new HashSet<Object>();

		public void addItem(Object anObject, boolean disabled) {
			super.addItem(anObject);
			if (disabled) {
				disabled_items.add(getItemCount() - 1);
			}
		}

		@Override
		public void removeAllItems() {
			super.removeAllItems();
			disabled_items = new HashSet<Object>();
		}

		@Override
		public void removeItemAt(final int anIndex) {
			super.removeItemAt(anIndex);
			disabled_items.remove(anIndex);
		}

		@Override
		public void removeItem(final Object anObject) {
			for (int i = 0; i < getItemCount(); i++) {
				if (getItemAt(i) == anObject) {
					disabled_items.remove(i);
				}
			}
			super.removeItem(anObject);
		}

		@Override
		public void setSelectedIndex(int index) {
			if (!disabled_items.contains(index)) {
				super.setSelectedIndex(index);
			}
		}

		private class DisabledItemsRenderer extends BasicComboBoxRenderer {

			@Override
			public Component getListCellRendererComponent(JList list,
					Object value, int index, boolean isSelected,
					boolean cellHasFocus) {

				if (isSelected) {
					setBackground(list.getSelectionBackground());
					setForeground(list.getSelectionForeground());
				} else {
					setBackground(list.getBackground());
					setForeground(list.getForeground());
				}
				if (disabled_items.contains(index)) {
					setBackground(list.getBackground());
					setForeground(UIManager
							.getColor("Label.disabledForeground"));
				}
				setFont(list.getFont());
				setText((value == null) ? "" : value.toString());
				return this;
			}
		}
	}
}


(开始 感叹 GFW )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值