Can't believe it has gotten to this. I cannot for the life of me figure out what is messing up my desired outcome. Here is the story:

I have a small GUI program for a sports club. I have 3 list boxes, 1 for each U10's,U12's and U14 children. I have a textfield to enter name. 3 Combo boxes for the dob, and a radio button for gender.

My problem is, When a child is in the U10 age group, it adds that child to all 3 list boxes, but it works fine on both U12 and U14's. It is coded exactly the same way as the other 2. I tried switching the order of execution to test if it happened to the others, but it just continued to happen to the U10's. This is bugging me. I will post the code I think will be helpful.

Classify Button - adds info to the list boxes

private void m_btnClassifyActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        String wrongAge = "candidate must be between 8 and 15 years old!";
        if(doValidate())
        {
            if(checkAge()==10)
            {
                fillList(10);
                clearFields();
            }
            else if(checkAge()==12)
            {
                fillList(12);
                clearFields();
            }
              else if(checkAge()==14)
            {
               fillList(14);
               clearFields();
            }
             else{
                 JOptionPane.showMessageDialog(this, wrongAge,"Invalid Entry", JOptionPane.ERROR_MESSAGE);


             }

        }
    }

validation method

private boolean doValidate()
    {
        String notEnoughMsg = "You must enter a day,month and year!";
        String noName = "You must enter a name";
        if(m_cbDay.getSelectedIndex()==0 || m_cbMonth.getSelectedIndex()==0 || m_cbYear.getSelectedIndex()==0)
        {
              JOptionPane.showMessageDialog(this, notEnoughMsg,"Invalid Entry", JOptionPane.ERROR_MESSAGE);
              return false;

        }
 else if(m_tfName.getText().equals(""))
        {
       JOptionPane.showMessageDialog(this, noName,"Invalid Entry", JOptionPane.ERROR_MESSAGE);
       return false;
        }


        return true;

    }

age calculation

private int checkAge()
    {
        Date today = Calendar.getInstance().getTime();
        long todayMs = today.getTime();
        int year = Integer.parseInt((String)m_cbYear.getSelectedItem());
        int month = Integer.parseInt((String)m_cbMonth.getSelectedItem());
        int day = Integer.parseInt((String)m_cbDay.getSelectedItem());
        GregorianCalendar calAge = new GregorianCalendar(year,month-1,day);
        Date newAge = calAge.getTime();
        long ageMs = newAge.getTime();
        
        long elapsedTime = todayMs - ageMs;
        int age = (int)(elapsedTime / 1000 / 24 / 60 / 60 / 365);

        if(age < 7 || age > 15)
            return -1;
        else if(age > 7 && age <= 10)
            return 10;
        else if(age > 10 && age <=12)
            return 12;
        else if(age > 12 && age <=15)
            return 14;
        else
            return 0;
   
    }

fillList method- adds contents to the list

private void fillList(int age)
    {
         String gender = "";
            String name = m_tfName.getText();
            if(m_rbMale.isSelected())
                gender = m_rbMale.getText();
            else
                gender = m_rbFemale.getText();
        if(age==10)
        {
            m_lbU10.setModel(defaultListModel1);
            defaultListModel1.addElement("Name: "+name);
            defaultListModel1.addElement("Gender: "+gender);
            
        }
 else if(age==12)
        {
      m_lbU12.setModel(defaultListModel2);
           defaultListModel2.addElement("Name: "+name);
            defaultListModel2.addElement("Gender: "+gender);
     }
 else{
                 m_lbU14.setModel(defaultListModel3);
             defaultListModel3.addElement("Name: "+name);
            defaultListModel3.addElement("Gender: "+gender);

 }
        
        
        
    }

Any help is appreciated.

Dunno if this is relevant, but why do you call checkAge() 3 times in btnClassifyActionPerformed - is it possible that it returns different values after the whatever else happens between the first and second calls (eg clearFields())?

Dunno if this is relevant, but why do you call checkAge() 3 times in btnClassifyActionPerformed - is it possible that it returns different values after the whatever else happens between the first and second calls (eg clearFields())?

There wasn't really any reason for it, I must have just been testing it and then just copied it 3 times because of laziness. I tried calling it once anyway, with the same result. clearFields() just clears the whole form.

Edit: I have just noticed something. Even though it adds the value to all 3 listboxes visually, when I create a member in a different age group(other than U10), it removes the U10 value that shouldnt be there. It is as if it is just a graphics bug or something. The U10 user is not actually an element of the other 2 list boxes, it is just there, which is quite odd.

I can't see anything else in the code you posted. Maybe the problem lies elsewhere? eg Is there any kind of change listener on the U10 listbox...
Did you tracing the execution for the U10 case to see if it executes (only) the code you expected?

I have tried tracing the execution. It enters the correct if statements in both checkAge() and fillList(). And it also only enters the fillList(10) method from the classify button method, which is all correct. The only other thing connected with these are the list Models. But all 3 models are made the same way, e.g defaultListModel1 = new javax.swing.DefaultListModel(), defaultListModel2 = new javax.swing.DefaultListModel()

If you didn't see my above edit, here it is again:

I have just noticed something. Even though it adds the value to all 3 listboxes visually, when I create a member in a different age group(other than U10), it removes the U10 value that shouldnt be there. It is as if it is just a graphics bug or something. The U10 user is not actually an element of the other 2 list boxes, it is just there, which is quite odd.

It seems to only happen when the other 2 list boxes are empty!

Oh my god. All 3 lists were defaulted to the defaultListModel1. Thats why it seemed to not actually be there, because it was changing the model as soon as the age group was used. It is always so damn simple.

why did you validate Date by using InputMask http://download.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html#format

add actionListener directly to the JButton, all methods pact to the separated void as f.e. doValidate()

you have to set Focus to JComponents from void doValidate()

int checkAge()

1/ value for Calendar and Date prepare outSide this methods, sure you can prepare fix value for one year as Long Value
long 15Y =
long 14y =
long 13y =
.
.
.
2/ please there is something interest http://www.java2s.com/Code/Java/Data-Type/CatalogData-Type.htm for conversion between Calendar and Date (f.e.)

3/ replace 3 JComboBox with some of Free JCalendar, compare its funcionalities with www.toedter.com/en/jcalendar/index.html

4/ maybe JTable should be better JComponent for that

why did you validate Date by using InputMask http://download.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html#format

add actionListener directly to the JButton, all methods pact to the separated void as f.e. doValidate()

you have to set Focus to JComponents from void doValidate()

int checkAge()

1/ value for Calendar and Date prepare outSide this methods, sure you can prepare fix value for one year as Long Value
long 15Y =
long 14y =
long 13y =
.
.
.
2/ please there is something interest http://www.java2s.com/Code/Java/Data-Type/CatalogData-Type.htm for conversion between Calendar and Date (f.e.)

3/ replace 3 JComboBox with some of Free JCalendar, compare its funcionalities with www.toedter.com/en/jcalendar/index.html

4/ maybe JTable should be better JComponent for that

Even though I appreciate you gave your time to speak in my thread. I unfortunately can not understand most of what you are trying to say. But also, There is no need to give me 1,2,3,4...There was a specification for my assignment, that I did not share here. Thanks anyway.

sorry I wrote this reply in my car (trafic) ...

replace your 3. JComboBox with:

1/ for Date you have:
by using JFormattedTextField with basic input mask see link

or

search for free JCalendar (I voting for Kai Toedter, cos is very simply and confortly customized by your own ...)

then you no longer care about that

2/ if you want to compare difference between some period, then just calculate long value for some period f.e. one year or month and compare with age in years

long oneYear (or month) = 10000000 (this value isn't true)
//you can prepare this value calculate diff between two java.util.Date periods
long oneYear = (maxDate.getTime() - minDate.getTime());
// one day isn't integer value, but missing 0.001 per one day, you have to roundUp, f.e. by using BigDecimal ...

3/ search for "Conversion between Calendar to Date" sure vice versa

4/ your JList implemantations should be JTable, then you need only one DefaultTableModel, as synchronize (most expensive woodoo)two or more DefalutListModel

5/

myButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myButtonActionPerformed(evt);
            }

            private void myButtonActionPerformed(ActionEvent evt) {
                doValidate();
            }
        });

I hope that now you can understood that

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.