Netbeans - Entering items in a jComboBox

孤街醉人 提交于 2019-12-23 09:26:24

问题


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

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

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

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));

回答1:


There are 2 approaches I am aware of:

  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));
    }
    
  2. 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);
    }
    

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

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

    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.

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().




回答2:


Using Netbeans NEON and other netbeans version

1. Go to the properties of the combobox

2. Then go to model




回答3:


you can inject your code by using "custom code" feature in the GUI editor for the "model" of combobox




回答4:


Completing blurec answer (I cannot comment yet), in the GUI editor select the comboxbox, go properties, then model, then hit the three dots. Then select Custome Code and add your code, for instance:

new DefaultComboBoxModel<>(functionThatReturnsAnStringArray())



回答5:


public NewJFrame() {
        initComponents();
        reformatComboBox();
  }

private void reformatComboBox() {
        JComboBoxName.removeAllItems();
        JComboBoxName.addItem("item1");
        JComboBoxName.addItem("item2");
}



回答6:


For the posterity:

Right click the ComboBox and select Customize Code. Here at the comboBox.setModel, in the left select custom property. After new String, add your values in the following form:

Value 1: Integer.toString(myInt1) Value 2: Integer.toString(myInt2)

If your variables are int of course. If not just put the String variable and you are done.

Hope it helps.



来源:https://stackoverflow.com/questions/1542081/netbeans-entering-items-in-a-jcombobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!