简体   繁体   English

更快的方法来写下拉框项目列表

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

I'm learning Java and working with Swing at the moment. 我正在学习Java,目前正在使用Swing。 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? 但是说,例如,我希望我的下拉框列出1900-2013年,是否有更好,更快的方法列出这些数字,而不是将它们全部输入一个大数组? Like could I just say I want the array to hold all values between 2 parameters? 就像我可以说我希望数组保留2个参数之间的所有值吗?

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 但是说,例如,我希望我的下拉框列出1900-2013年

  • use JSpinner with SpinnerDateModel instead of JComboBox JSpinnerSpinnerDateModel而不是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 否则,您将需要AutoCompete for JComboBox实现一些AutoCompete for JComboBox ,因为带有1k Items的范围根本不是用户友好的,也不是在使用该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 如果要使用用户选择的Date instance ,则将Date放到DefaultComboBoxModel而不是intInteger (代表年份)中

Something like this? 像这样吗 Counts backwards from this year to 1900. 从今年倒数到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。

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. 值“方法”需要较少的编码,并且在某些简单情况下更为明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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