Netbeans-在jComboBox中输入项目 [英] Netbeans - Entering items in a jComboBox

查看:42
本文介绍了Netbeans-在jComboBox中输入项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从netbeans生成了一个GUI,在其中也放置了一个组合框.

I have generated a GUI from netbeans in which I have placed a combobox too.

默认情况下,组合框中的项目是item1,item2,item3,item4.

By default, the items in combobox are item1, item2, item3, item4.

但是我想要我自己的东西. Netbeans不允许编辑生成的代码,因此如何根据我的要求编辑comnbobox.

But I want my own items. Netbeans doesn't allow editing generated code so how can i edit the comnbobox according to me.

注意:我知道一种方法,可以通过编辑jComboBox的"model"属性来实现,但是我不想那样做,因为我想要jComboBox中的各种项(位于数组中),所以我想传递该jComboBox中的数组如下:

Note: I know one method by editing the "model" property of that jComboBox but I don't want to do it like that because I want various items (which are in an array) in that jComboBox so I want to pass that array in that jComboBox like as follows:

jComboBox2 = new javax.swing.JComboBox();

String [] date = new String[31];
for(int i = 0; i < 31; i++) {
    date[i] = i + 1;
}

jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(date));

推荐答案

我知道2种方法:

  1. 简单方法-在构造函数中调用initComponents()之后,添加代码以构建模型并调用jComboBox2.setModel(myModel)进行设置.因此,构造函数将类似于:

  1. Simple approach - After the call to initComponents() in the constructor add you code to build your model and call jComboBox2.setModel(myModel) to set it. So the constructor would look something like:

public SomeClass() {
    initComponents();
    String [] date = new String[31];
    for(int i = 0; i < 31; i++) {
        date[i] = i + 1;
    }
    jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(date));
}

  • 复杂方法-添加保存所需模型的可读属性.例如:

  • Complex approach - add a readable property that holds the desired model. For example:

    private ComboBoxModel getComboBoxModel()
    {
        String[] items = {"Item A", "Item B", "Item C"};
        return new DefaultComboBoxModel(items);
    }
    

    然后,在jComboBox2属性表中,单击按钮以编辑模型.

    Then, in the jComboBox2 property sheet, click the button to edit the model.

    在编辑器面板中,将下拉菜单从Combo Box Model Editor更改为Value from existing component.

    In the editor panel change the dropdown from Combo Box Model Editor to Value from existing component.

    选择Property.选择comboBoxModel属性.点击确定

    Select Property. Choose the comboBoxModel property. Click OK

    我曾经尝试过第二种方法.再也没有真正使用过它.太多的工作,没有太大的收获.另外,它在设计器中显示一个空的组合框,这只会使布局更难.

    I tried the second way once. Never really used it again. Too much work, no real much gain. Plus it displays an empty combo box in the designer which just makes layout harder.

    我使用第一种方法,并使用NetBean的模型编辑器为模型提供一些代表性的值.这使我在设计器中有了合理的尺寸行为,但以initComments()中的一条不必要的行为代价.

    I use the first approach, plus use NetBean's model editor to supply some representative values for the model. That gives me the sensible size behavior in the designer at the cost of one unnecessary line in initComments().

    这篇关于Netbeans-在jComboBox中输入项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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