-2

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**********
greg-449
  • 109,219
  • 232
  • 102
  • 145
paulaxa1
  • 1
  • 1
  • 8
  • I think that we can't make a definite answer based on your posted code without guessing, but you appear to be selecting your state before the user has had a chance to interact with its JComboBox. You may wish to create and post a [mcve] so we can tell you with more assurance what you're doing wrong and how to fix it. – Hovercraft Full Of Eels Jan 24 '16 at 16:17
  • 1
    As a side note, you appear to be using null layouts and setBounds, and I strongly encourage you **not** to do this. While null layouts and `setBounds()` might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one. – Hovercraft Full Of Eels Jan 24 '16 at 16:17
  • 1
    Also, I see that you're using your GUI as its own ActionListener since you've got `something.addActionListener(this);`, and I will recommend that you don't do this. While this is fine for tiny demo toy programs, you shouldn't do this for any other types of programs as this gives the class too much responsibility and makes it too easy to make mistakes. Create unique listeners for each specific component or set of components that need one. – Hovercraft Full Of Eels Jan 24 '16 at 16:35
  • .............. 9 hours and no improvement to the question. Voting to close. – Hovercraft Full Of Eels Jan 25 '16 at 01:46
  • thanks for the tips man, here is a small program that only has the problem i am asking, i will post it as an answer, it's the "minimal"/ – paulaxa1 Jan 25 '16 at 08:26
  • i'm so getting banned for this.. i can't post my code please if you would like download the textfile with the minimal code here.. https://www.sendspace.com/file/80pjmw sorry again but i can't post it here it won't let me, and i am stupid and can't figure out why – paulaxa1 Jan 25 '16 at 10:12

1 Answers1

0

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

dryairship
  • 6,022
  • 4
  • 28
  • 54
  • thank you so much man i knew it was a stupid mistake but i couldn't figure it out. Also please comment here what i do wrong and i get hate from people in stackoverflow, what should i improve about my questions? – paulaxa1 Jan 25 '16 at 16:41
  • That's **not hate**. They want you to improve the quality of your questions. We want SO to be a great Q&A site. Questions with poor grammar, bad punctuation, irrelevant code etc. are not considered good questions. You may go through some articles in the help section. Also, if my answer solved your problem, then please consider accepting it and/or upvoting it. – dryairship Jan 26 '16 at 11:38
  • didn't know the accepting thing :) also i can't vote yet i need 15 reputation for that, thanks again man – paulaxa1 Jan 26 '16 at 22:36