Search code examples
javajcombobox

Rearranging a JComboBox in reverse


These questions might have really simple answers but in this code

public void setupYears()
{
        ArrayList<String> years_tmp = new ArrayList<String>();

        for(int years = Calendar.getInstance().get(Calendar.YEAR)-90 ; years<=Calendar.getInstance().get(Calendar.YEAR);years++)
        {
          years_tmp.add("Year"+years+"");
        }


        Y = new JComboBox(years_tmp.toArray());

        Y.setLocation(404,310);
        Y.setSize(250,25);
        Y.setEditable(false );
        firstPanel.add(Y);
}

How do I firstly reverse the years so the first year would be the current year and the last year would be 90 years ago instead of vice versa?

Also how would I put the "Year" as the first object in the JComboBox instead of "Yearxxxx"?

"xxxx" being whatever year is displayed in the JComboBox


Solution

  • To solve the ordering problem:

    for(int years = Calendar.getInstance().get(Calendar.YEAR) ; years>=Calendar.getInstance().get(Calendar.YEAR)-90;years--)
    

    And to put "Year" in the first box, simply add the line

    years_tmp.add("Year");
    

    before your for loop.

    Hope this helps.