1

I have a combo box with options Small, Medium, and Large. These are the properties and want to apply...

The string variable Pattern have values Small, Medium, Large and it's add to the combo box Barsize

While running this java file, if I choose Medium option it got selected and also property had applied but if I run once again it goes to Small option in index of the combo box.

How do I save that....??? I need an option what I have selected in index till Next changes will make...

Coding is........

propertiesPanel.add(new JLabel("Barsize"))
BarField = new JComboBox(pattern)
propertiesPanel.add(BarField)

Here pattern have the values Small, Medium, and Large......

4

1 回答 1

0

While running this java file, if I choose Medium option it got selected and also property had applied but if I run once again it goes to Small option in index of the combo box.

You need some global place (I'm thinking in a Singleton pattern) to keep this selected size so when you show this window again you can set this value as selected in your combo box.

Something like this:

public class SizeProperties {

    private static SizeProperties _instance;
    private Sizes _selectedSize;

    public enum Sizes{SMALL, MEDIUM, LARGE}

    private SizeProperties(){
        _selectedSize = Sizes.SMALL;
    }

    public static SizeProperties getInstance(){
        if(_instance == null){
            _instance = new SizeProperties();
        }
        return _instance;
    }

    public Sizes getSelectedSize(){
        return _selectedSize;
    }

    public void setSelectedSize(Sizes size){
        _selectedSize = size;
    }
}

In your dialog you can fill your combobox as follows:

JComboBox barField = new JComboBox();
for(SizeProperties.Sizes s : SizeProperties.Sizes.values()){
    barField.addItem(s);
}
barField.setSelectedItem(SizeProperties.getInstance().getSelectedSize());

Finally to save selected size in your singleton class:

SizeProperties.Sizes selectedSize = (SizeProperties.Sizes) barField.getSelectedItem();
SizeProperties.getInstance().setSelectedSize(selectedSize);
于 2013-09-25T12:24:24.480 回答