Search code examples
javaeclipseswingcomboboxeclipse-mars

My program has a Country and State combobox, and from the state combobox it only gets the first value no matter what you choose


So i created a program for an assignment i have, which creates a sequential file with data about students. I made a country and state combobox, depending on the country comes the corresponding states, but in the sequential file (which is a .dat file) no matter what state i choose, in the sequential file, the state is always the first one.For example if choose Australia and Tasmania state , the state in the sequential file will be New South Wales. Here is my code thanks in advance

public assignment1st() 
{
    super("create student file");

    try{
        output=new DataOutputStream(new FileOutputStream("studentRec.dat"));

    }
      catch ( IOException e )  {
           System.err.println( "File won't open properly/n" +
             e.toString( ) );
           System.exit( 1 );
    }

    initialize();

    //*******HERE STARTS THE COUNTRY/STATE COMBOBOX BUILD**************************************
    String[] countries = {"-CHOOSE","Australia","Belgium","Brazil","Canada","Georgia","Greece",
        "India","Lithuania","Macedonia"};
    comboBox_1 = new JComboBox<Object>(countries);
    comboBox_1.addActionListener(this);
    comboBox_1.setBounds(278, 142, 92, 20);
    frame.getContentPane().add(comboBox_1);


    //  Create sub combo box with multiple models
    //State Combobox

    comboBox_2 = new JComboBox<String>();
    comboBox_2.addItem("-CHOOSE-");
    comboBox_2.setBounds(452, 142, 109, 20);
    frame.getContentPane().add(comboBox_2);
    comboBox_2.setPrototypeDisplayValue("XXXXXXXXXX");

    String[] Australia = { "New South Wales", "Tasmania", "Queensland" ,"Victoria"};
    states.put(countries[1], Australia);

    String[] Belgium = { "Louxembourg", "Hainaut", "Flemish" };
    states.put(countries[2], Belgium);

    String[] Brazil = { "Amazonas", "Mato Grosso" };
    states.put(countries[3], Brazil);

    String[] Canada = { "Vancouver", "Quebec" };
    states.put(countries[4], Canada);

    String[] Georgia = {"Tbilisi", "S.Ossetia" };
    states.put(countries[5], Georgia);

    String[] Greece = { "Pelloponisos", "Chalchidikis", "Thesprotias" };
    states.put(countries[6], Greece);

    String[] India = {  "Jalpur", "Kolkata", "New Delhi" };
    states.put(countries[7], India);

    String[] Lithuania = { "Akmene", "Kretinga", "Varena" };
    states.put(countries[8],Lithuania);

    String[] Macedonia = {  "Bitola", "Struga", "Veles" };
    states.put(countries[9], Macedonia);


}

code which imports data

   if ( studentID > 0 )  {



                //PLACE FOR COMBOBOXEZ


                String sex=(String) comboBox.getSelectedItem();
                output.writeUTF(sex);

                String country=(String) comboBox_1.getSelectedItem();
                output.writeUTF(country);


                String state=(String) comboBox_2.getSelectedItem();
                output.writeUTF(state);



                String month=(String) comboBox_3.getSelectedItem();
                output.writeUTF(month);

                String day=(String) comboBox_4.getSelectedItem();
                output.writeUTF(day);

                String year=(String) comboBox_5.getSelectedItem();
                output.writeUTF(year);


                output.writeInt(maths);
                output.writeInt(buisness);
                output.writeInt(programming);
                output.writeInt(accounting);
                output.writeInt(art);
                output.writeInt(music);

and finally the actionperformed for the country state comboboxes

public void actionPerformed( ActionEvent e )  { 

    //*************FOR STATE AND COUNTRY COMBOBOXEZ*********************
    String country = (String)comboBox_1.getSelectedItem();
        Object o = states.get( country );

        if (o == null)
        {
            comboBox_2.setModel( new DefaultComboBoxModel<String>() );
        }
        else
        {
            comboBox_2.setModel( new DefaultComboBoxModel<String>( (String[])o ) );
        }
       //**********DONE WITH THE STATE AND COUNTRY COMBOBOXEZ**********

Solution

  • This is because your this part of code :

    if (o == null)
    {
        comboBoxstate.setModel( new DefaultComboBoxModel<String>() );
    }
    else
    {
        comboBoxstate.setModel( new DefaultComboBoxModel<String>( (String[])o ) );
    }
    

    Is before this:

    if (e.getSource()==btnEnter){
        addRecord( ) ;
    }
    

    As such, your combo box gets rearranged first, and then the record is added.

    If you put the second part before the first one, it works perfectly. That's it.

    But, I would suggest you to check which component caused the action, and then do what you want to do, else actions on some components can cause operations which you expected some other component to perform. If you want that, you can experiment with e.getActionCommand()