1

For the last few weeks I have been spending some time on a date of birth picker for a small Java project. I have got everything working but there seems to be one error. When I choose some dates and then switch the JComboBoxes desynchronize and won't automatically change the selected values in the other JComboBoxes untill the button is pressed for some reason. I think it is to do with the fact that my JComboBoxes have different sized indexes.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*; 
import java.text.SimpleDateFormat;
import javax.swing.event.*;

public class dateTest extends JFrame implements ActionListener,ItemListener{

    JFrame myMainWindow = new JFrame("This is my title");
    JTabbedPane myTabs = new JTabbedPane();
    JPanel  firstPanel = new JPanel(); //a panel for first tab

    //first panel components
    String Day2_tmp[]={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"};
    JComboBox Day2 = new JComboBox(Day2_tmp);
    String Day3_tmp[]={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
    JComboBox Day3 = new JComboBox(Day3_tmp);
    String Month_tmp[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
    JComboBox Month = new JComboBox(Month_tmp);
    String Day4_tmp[]={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29"};
    JComboBox Day4 = new JComboBox(Day4_tmp);
    Calendar cal = Calendar.getInstance();
    JComboBox Y;
    JComboBox X;
    JButton btnBornOn = new JButton();
    Object dd;
    Object mm;
    Object yyyy;
    //end first panel

    JLabel tst = new JLabel();

    public void runGUI(){
        myMainWindow.setBounds(500, 10, 430, 230); //set position, then dimensions
        myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        myMainWindow.setLayout(new GridLayout(1,1));

        createFirstPanel(); //call method to create each panel
        setupYears();   

        myTabs.addTab("First Panel", firstPanel); //add panel //title, image?, panel, tooltip?      
        myMainWindow.getContentPane().add(firstPanel); //adds the tabbedpane to mainWindow      
        myMainWindow.setVisible(true); //make the GUI appear
    }

    public void createFirstPanel(){
        firstPanel.setLayout(null);
        firstPanel.setBackground( new Color(-1) );

        Day2.setLocation(20,50);
        Day2.setSize(100,25);
        Day2.setVisible(false);
        Day2.setEditable(false );
        Day2.addItemListener(this);
        Day2.addActionListener(this);
        Day2.setBackground( new Color(-1) );
        firstPanel.add(Day2);

        Day3.setLocation(20,50);
        Day3.setSize(100,25);
        Day3.setVisible(true);
        Day3.setEditable(false );
        Day3.addItemListener(this);
        Day3.addActionListener(this);
        Day3.setBackground( new Color(-1) );
        firstPanel.add(Day3);

        Day4.setLocation(20,50);
        Day4.setSize(100,25);
        Day4.setVisible(false);
        Day4.setEditable(false );
        Day4.addItemListener(this);
        Day4.addActionListener(this);
        Day4.setBackground( new Color(-1) );
        firstPanel.add(Day4);

        Month.setLocation(160,50);
        Month.setSize(100,25);
        Month.setEditable(false );
        Month.addItemListener(this);
        Month.setVisible(true);
        Month.setBackground( new Color(-1) );
        firstPanel.add(Month);

        btnBornOn.setLocation(250,100);
        btnBornOn.setSize(150,50);
        btnBornOn.setText("DOB DD/MM/YYYY");
        btnBornOn.addActionListener(this);
        btnBornOn.setOpaque(true);
        btnBornOn.setContentAreaFilled(true);
        btnBornOn.setBorderPainted(false);
        btnBornOn.setVisible(true);
        firstPanel.add(btnBornOn);
    }

    public void setupYears(){
        ArrayList<String> years_tmp = new ArrayList<String>();
        for(int years = Calendar.getInstance().get(Calendar.YEAR) ; years>=Calendar.getInstance().get(Calendar.YEAR)-90;years--){
            if(years % 4 == 0){
                years_tmp.add(years+"");
            }
        }

        Y = new JComboBox(years_tmp.toArray());
        Y.setLocation(300,50);
        Y.setSize(100,25);
        Y.setEditable(false );
        Y.setVisible(false);
        Y.addItemListener(this);
        Y.setBackground( new Color(-1) );
        firstPanel.add(Y);

        ArrayList<String> years2_tmp = new ArrayList<String>();

        for(int years2 = Calendar.getInstance().get(Calendar.YEAR) ; years2>=Calendar.getInstance().get(Calendar.YEAR)-90;years2--){
            years2_tmp.add(years2+"");
        }

        X = new JComboBox(years2_tmp.toArray());

        X.setLocation(300,50);
        X.setSize(100,25);
        X.setEditable(false );
        X.addItemListener(this);
        X.setBackground( new Color(-1) );
        firstPanel.add(X);
    }

    public void setDateInBoxes(){
        if(Day2.isVisible()== true){
            Day3.setSelectedItem(Day2.getSelectedItem());
            Day4.setSelectedItem(Day2.getSelectedItem());
        }

        if(Day3.isVisible()== true){
            Day2.setSelectedItem(Day3.getSelectedItem());
            Day4.setSelectedItem(Day3.getSelectedItem());
        }

        if(Day4.isVisible()== true){
            Day3.setSelectedItem(Day4.getSelectedItem());
            Day2.setSelectedItem(Day4.getSelectedItem());
        }

        if(X.isVisible()== true){
            Y.setSelectedItem(X.getSelectedItem());
        }

        if(Y.isVisible()== true){
            X.setSelectedItem(Y.getSelectedItem());
        }
    }

    public void actionPerformed(ActionEvent e){ 
        setDateInBoxes();

        if(e.getSource() == btnBornOn){
            dd =Day3.getSelectedItem();
            mm =Month.getSelectedItem();
            yyyy =X.getSelectedItem();
            System.out.println("This person was born on: "+dd+"/"+mm+"/"+yyyy);
        }
    }

    public void itemStateChanged(ItemEvent e){
        if(Month.getSelectedItem().equals("February")){
            Day2.setVisible(false);
            Day3.setVisible(false);
            Day4.setVisible(true);
        }

        if(Month.getSelectedItem().equals("April")){
            Day2.setVisible(true);
            Day3.setVisible(false);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("June")){
            Day2.setVisible(true);
            Day3.setVisible(false);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("September")){
            Day2.setVisible(true);
            Day3.setVisible(false);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("November")){
            Day2.setVisible(true);
            Day3.setVisible(false);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("January")){
            Day2.setVisible(false);
            Day3.setVisible(true);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("March")){
            Day2.setVisible(false);
            Day3.setVisible(true);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("May")){
            Day2.setVisible(false);
            Day3.setVisible(true);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("July")){
            Day2.setVisible(false);
            Day3.setVisible(true);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("August")){
            Day2.setVisible(false);
            Day3.setVisible(true);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("October")){
            Day2.setVisible(false);
            Day3.setVisible(true);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("December")){
            Day2.setVisible(false);
            Day3.setVisible(true);
            Day4.setVisible(false);
        }

        if(Day4.getSelectedItem().equals("29")){
            Y.setVisible(true);
            X.setVisible(false);
        }else {
            Y.setVisible(false);
            X.setVisible(true);
        }

        if(Day2.isVisible()== true){
            Y.setVisible(false);
            X.setVisible(true);
        }

        if(Day3.isVisible()== true){
            Y.setVisible(false);
            X.setVisible(true);
        }
    }

    public static void main(String[] args){
        dateTest gt = new dateTest();
        gt.runGUI();
    }
}

I was hoping someone could tell me what the problem is and how to fix it. Also any suggestions for improvement would be welcomed as well as criticisms. Also I am sorry for the amount of code I put in but I don't know where the problem is in it so I didn't know what part to put down.

  • Seems to me you show and hide controls pending on the number of days of month. That is the wrong way. Read about JComboBox and [ComboBoxModel](https://docs.oracle.com/javase/7/docs/api/javax/swing/ComboBoxModel.html). – PeterMmm Dec 10 '14 at 15:17
  • 1
    `if(years % 4 == 0)` that is not the only leap year condition. [GregorianCalendar](http://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html) has a method for this. – PeterMmm Dec 10 '14 at 15:20
  • And there are some good date picker for swing: http://sourceforge.net/projects/jdatepicker/ – PeterMmm Dec 10 '14 at 15:22
  • Can you please rephrase the problem description, breaking the process of creating the error into a list of steps? (And define what "won't work" means.) – HeavyE Dec 10 '14 at 15:53
  • Won't work as in the ComboBoxes won't automatically change the selected values in the other JComboBoxes untill the dob button is pressed –  Dec 10 '14 at 17:22

0 Answers0