Jcombobox update from another Jcombobox

Asked

Viewed 1,558 times

1

My idea was to make a Jcombobox whose values were updated according to the previous Combobox options.

I’m using several Arraylists.

For example, a Combobox with January and February option... If you choose January in the first Combobox, the second will only have January events.

My canvas:

inserir a descrição da imagem aqui

  • 2

    What languages are you using? You can put the code you already have here?

  • Java (using Eclipse).

  • 1

    Check out the guide [Ask]. You are free to [Edit] the question and add more details, add tags, etc.

  • The code I didn’t put in because I don’t know how to proceed to do what I have in mind. I’ve been told it would have to be with. toArray() but I don’t know how. The graphic part is: https://dl.dropboxusercontent.com/u/38051425/Captura%20de%20tela%202014-06-17%2010.49.22.png and the idea was to make this application with the theme Animal Shop. I have several arraylists. When choosing the category 'dog' instead of cat, for example, in specimens, only the species of dog would appear... and so on.

  • Where will you get the list of events for the chosen month? Is everything already loaded in variables or will you access the comic book? How you reference events depending on the month you choose?

  • I just gave an example applied to months > events. My project is a Pet Shop. With categories (dog, cat, rodent, etc.), species of these categories (Iamese, etc.) and names of animals available with the chosen category and species.

  • @user had not seen his answer, I advise using the @ + "user name" so that the user can receive a notification that has been answered. Is your doubt still there? I think I understand your problem now. If you still want an answer let me know (with :P this time)

Show 2 more comments

3 answers

2

Without your code to help makes it a little difficult to help.

Your combobox choice is fixed, this is know what is the element of each item?

However, I’m going to post some sort of pseudocode to see if it helps. (I’m thinking yours combobox of choice is fixed)

switch (comboBox1.getSelectItem) {
            case cao:
                    comboBox2.removeAllItens();
                    for(Raça raça: Raças){
                        comboBox2.addItem("raça");
                    }                
                break;
            case ave:
                comboBox2.removeAllItens();
                    for(TipoPassaraos passaro: Passaros){
                        comboBox2.addItem("passaro");
                    }
                break;

}

1

You can use a listener in the first Combobox to change the content of the second. Something like this:

private void configuraComboBox(){
    List<String> meses = Arrays.asList("Janeiro","Fevereiro");
    jCombobox.setModel(new DefaultComboBoxModel(meses.toArray()));

    jCombobox.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {

               if(jCombobox.getSelectedItem() == null){
                  jCombobox2.setModel(new DefaultComboBoxModel<>());

               }else{
                 switch ((String)  jComboBox.getSelectedItem()) {
                    case "Janeiro":
                        jCombobox2.setModel(new DefaultComboBoxModel<String>(getEventosJaneiro()));
                        break;
                    case "Fevereiro":
                        jCombobox2.setModel(new DefaultComboBoxModel<String>(getEventosFevereiro()));
                        break;
                    default:
                       jCombobox2.setModel(new DefaultComboBoxModel<>());
                }
              }
            }
        });
}

private String[] getEventosJaneiro(){
    return new String[]{"Férias", "Atividade"};
}

private String[] getEventosFevereiro(){
    return  new String[]{"Carnaval", "Volta as aulas"};
}

0

You need to use the entry of items at runtime, for this use the addItem method. To be clearer:

if(comboBox1.getSelectItem.toString.equals("Frutas"){
comboBox2.addItem("Maçã");
comboBox2.addItem("Pera");
}

To remove:

comboBox1.removeAllItens();

For further clarification: http://docs.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html

  • .addItem ? But the items are from my Arraylist, so it cannot be like this.

  • Well, if you could pass your code, I could try to help you. Because depending on the structure of your Arraylist would just filter out what you want to add from the arrayList in the combobox

  • The problem is that there are several arraylist and classes and I don’t even know what code to put here. Because I have a class that stores 3 arraylists. Suppliers, customers, animals. I have a class for each animal category (dog, cat, bird, fish, rodent) and each one is subclass of the animal class that has the name, category, date of birth, color (use here the Enum) and price. In the "sales" class where I need this code for the combobox I’ve only done the graphic part. So it’s complicated to have code here clearly... That’s the problem. But this is the structure basically.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.