简体   繁体   中英

Quicker Way To Write List of Items For Drop-Down Boxes

I'm learning Java and working with Swing at the moment. When I create a drop-down box, I create an array containing what I want to appear as options. But say, for example, I want my drop-down box to list the years 1900-2013 is there a better and quicker way to list these numbers rather then type them all out in one massive array? Like could I just say I want the array to hold all values between 2 parameters?

When I create a drop-down box, I create an array containing what I want to appear as options. But say, for example, I want my drop-down box to list the years 1900-2013

  • use JSpinner with SpinnerDateModel instead of JComboBox

  • otherwise you would need to implements some of AutoCompete for JComboBox , because range with 1k Items isn't user friendly at all, nor working, bothering with selections in this Object

  • put Date to DefaultComboBoxModel instead of int or Integer (representing years) in the case that you want to working with Date instance from users selection

Something like this? Counts backwards from this year to 1900.

List<String> years = new ArrayList<String>();
int thisyear =2013; //you could parse this out of a new Date() object instead
for (int y = thisYear; y >= 1900; y--) {
    years.add(Integer.toString(y));
}

Something along these lines should generate an ArrayList of the required years.

ArrayList<String> myArray = new ArrayList<>();
for(int i = 1900; i < 2014; i++){
    myArray.add(Integer.toString(i));
}

for(String s : myArray){
    System.out.println(s);
}

I believe generating a list like this is known as creating it dynamically, or 'at run time'.

You can use different approaches, not just looping through years.
For example you can create a custom model for combobox:

ComboBoxModel model = new DefaultComboBoxModel ()
{
    @Override
    public Object getElementAt ( int index )
    {
        return 1900 + index;
    }

    @Override
    public int getSize ()
    {
        return 114;
    }
};
JComboBox comboBox = new JComboBox ( model );

That should be more than enough to generate the desired list.

Of course you can also use a simple array as data:

Integer[] years = new Integer[ 114 ];
for ( int i = 0; i <= 113; i++ )
{
    years[ i ] = 1900 + i;
}
ComboBoxModel model = new DefaultComboBoxModel ( years );    
JComboBox comboBox = new JComboBox ( model );

Each approach has its pros and cons.

Model "approach" will consume much less resources at start of your application (in case your list is really really big), though it will generate values each time combobox renderer calls for the value or you are trying to retrieve it (atleast in my simple implementation - you can add values caching, that will fix that issue).

Values "approach" takes less coding and more obvious in some simple cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM