Java JComboBox是否可以将editable true设置为仅一个项目? [英] Java JComboBox is it possible to set editable true into one item only?

查看:95
本文介绍了Java JComboBox是否可以将editable true设置为仅一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 JComboBox ,上面有三个项目..我想将editable只设置为 JComboBox的最后一项例如:

i want to create a JComboBox that have a three items on it.. I want to set editable only to the last item of the JComboBox for example:

JComboBox cb = new JComboBox();

cb.addItem("Dog");
cb.addItem("Cat");
cb.addItem("Other");

当我下拉 JComboBox 并选择项目其他.. JComboBox 变得可编辑..我该怎么做?

when i drop down the JComboBox and choose the item Others..JComboBox became editable.. how can I do that?

推荐答案

您可以使用 ItemListener 知道选择了哪个项目,然后根据需要设置 JComboBox 可编辑。试试这个例子:

You can use an ItemListener to know which item is selected and then you can set the JComboBox editable as needed. Try this example:

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Demo {

      private void initGUI(){        
        Object[] items = new Object[]{"Dog","Cat","Other"};
        DefaultComboBoxModel dcbm = new DefaultComboBoxModel(items);        

        final JComboBox comboBox = new JComboBox(dcbm);
        comboBox.setPreferredSize(new Dimension(200, 20));        
        comboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                Object selectedItem = comboBox.getSelectedItem();
                boolean editable = selectedItem instanceof String && ((String)selectedItem).equals("Other");
                comboBox.setEditable(editable);
            }
        });

        /*
         * Here you can add a new item to your JComboBox when it becomes editable
         */
        comboBox.getEditor().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object newItem = comboBox.getEditor().getItem();                  
                DefaultComboBoxModel dcbm = (DefaultComboBoxModel) comboBox.getModel();
                dcbm.addElement(newItem);
                dcbm.setSelectedItem(newItem);
            }
        });

        JPanel content = new JPanel(new FlowLayout());
        content.add(new JLabel("Test:"));
        content.add(comboBox);

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(content);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

    public static void main(String[] args) {        
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().initGUI();
            }
        });      
    }
}

正如您所见,当其他项目时被选中 JComboBox 变得可编辑:

As you'll see, when "Other" item is selected JComboBox becomes editable:

这篇关于Java JComboBox是否可以将editable true设置为仅一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆