gpt4 book ai didi

Java:使 jcombobox 的一项不可选择(如子标题)并编辑该项目的字体

转载 作者:搜寻专家 更新时间:2023-11-01 01:21:11 24 4
gpt4 key购买 nike

如何使组合框中的一个项目不可选择,因为我需要用子主题分隔组合框中的项目

是否可以单独修改该特定项目的字体?

        jComboBox_btech_course.setFont(new java.awt.Font("Tahoma", 0, 14));
jComboBox_btech_course.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select Course" }));
jComboBox_btech_course.setName("");

private class theHandler implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
//BTech courses

if(jComboBox_mtech_dept.getSelectedItem().equals("Civil Engineering"))
{
jComboBox_btech_course.removeAllItems();
jComboBox_btech_course.addItem("Building Construction");
jComboBox_btech_course.addItem("Principle And Practice");
jComboBox_btech_course.addItem("Surveying");
jComboBox_btech_course.addItem("Engineering Geology");
jComboBox_btech_course.addItem("Structural Analysis");
jComboBox_btech_course.addItem("Hydraulic Engineering");
jComboBox_btech_course.addItem("Environmental Engineering");
jComboBox_btech_course.addItem("Structural Design");
jComboBox_btech_course.addItem("Geotechnical Engineering");
/*This item has to be unselectable*/
jComboBox_btech_course.addItem("***Sub-topic***");
jComboBox_btech_course.addItem("Transportation Engineering");
jComboBox_btech_course.addItem("Foundation Engineering");
jComboBox_btech_course.addItem("Estimation & Valuation");
jComboBox_btech_course.addItem("Hydrology & Flood Control");
jComboBox_btech_course.addItem("System Analysis, Project Planning And Construction Management");
jComboBox_btech_course.addItem("Irrigation Engineering");
jComboBox_btech_course.addItem("Computer Application in Civil Engineering");
jComboBox_btech_course.addItem("Planning, Design & Detailing");
}
}
}

最佳答案

前言:在建议的解决方案中,我假设您要禁用以"**" 开头的项目。您可以将此逻辑更改为您想要的任何内容。在一个改进的版本中,MyComboModel 类(见下文)甚至可以存储哪些项目被禁用,允许任意项目被标记为禁用。

您的问题的解决方案涉及两件事:

1。禁止选择您想要禁用的项目

为此,您可以使用自定义 ComboBoxModel ,并覆盖它的 setSelectedItem()如果要选择的项目是禁用的,则不执行任何操作的方法:

class MyComboModel extends DefaultComboBoxModel<String> {
public MyComboModel() {}
public MyComboModel(Vector<String> items) {
super(items);
}
@Override
public void setSelectedItem(Object item) {
if (item.toString().startsWith("**"))
return;
super.setSelectedItem(item);
};
}

您可以通过将它的实例传递给 JComboBox 构造函数来设置这个新模型:

JComboBox<String> cb = new JComboBox<>(new MyComboModel());

2。用不同的字体显示禁用的项目

为此你必须使用自定义 ListCellRenderergetListCellRendererComponent()您可以为禁用和启用项目配置不同视觉外观的方法:

Font f1 = cb.getFont();
Font f2 = new Font("Tahoma", 0, 14);

cb.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if (value instanceof JComponent)
return (JComponent) value;

boolean itemEnabled = !value.toString().startsWith("**");

super.getListCellRendererComponent(list, value, index,
isSelected && itemEnabled, cellHasFocus);

// Render item as disabled and with different font:
setEnabled(itemEnabled);
setFont(itemEnabled ? f1 : f2);

return this;
}
});

关于Java:使 jcombobox 的一项不可选择(如子标题)并编辑该项目的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28344126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com