简体   繁体   中英

How to set default combobox value to the value set in Properties file?

I am writing script to display properties file using swing. On UI, there is a combobox:

private JComboBox Types= new JComboBox();

I have set 3 values to it as below:

private String[] Options = { "Web", "IOS", "Android" };
for (int i = 0; i < 3; i++)
    Types.addItem(Options[count++]);

The default value set is Web. I need to read config.prop file and set the default value of the combobox to the value set in config.prop file. For eg. if value set in config.prop is Type=IOS, combobox should display IOS.

I have tried below method, but the combobox shows blank:

Types.setSelectedItem(configProp.getProperty("Type"));

As the JcomboBox is list of String, It should be already working fine. Can you check the value of

configProp.getProperty("Type")

I think I know your problem. you are using count in your loop. use 'i'

private String[] Options = { "Web", "IOS", "Android" };
for (int i = 0; i < 3; i++)
    Types.addItem(Options[i]);

int index = Arrays.asList( Options ).indexOf( configProp.getProperty("Type") );
if ( index != -1 )
{
   Types.setSelectedIndex(index);
}
else if( Options.length() > 0 )
{
   Types.setSelectedIndex(0);
}

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