Netbeans - Entering items in a jComboBox
Asked Answered
B

6

7

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));
Bandy answered 9/10, 2009 at 6:21 Comment(0)
P
7

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

Petitionary answered 28/10, 2009 at 16:36 Comment(0)
S
4

Using Netbeans NEON and other netbeans version

1. Go to the properties of the combobox

enter image description here

2. Then go to model

enter image description here

Sirenasirenic answered 25/9, 2016 at 16:53 Comment(0)
J
2

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

Jaquesdalcroze answered 9/10, 2009 at 14:1 Comment(0)
C
1
public NewJFrame() {
        initComponents();
        reformatComboBox();
  }

private void reformatComboBox() {
        JComboBoxName.removeAllItems();
        JComboBoxName.addItem("item1");
        JComboBoxName.addItem("item2");
}
Canso answered 15/12, 2016 at 10:43 Comment(1)
Please include commentary about why this solution works and answers the questionStanleigh
M
0

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())
Macdonald answered 13/6, 2014 at 18:12 Comment(0)
P
0

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.

Photoactinic answered 27/5, 2018 at 20:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.