使用lwuit实现依赖的组合框

时间:2013-05-17 09:11:36

标签: combobox lwuit

private void buildUI() throws Exception {
        try {
            Display.init(this);
    countryLabel = new Label(resources.getString("Label10"));
            countryLabel.setSelectedStyle(DefaultLayout.labelStyle());
            countryLabel.setPressedStyle(DefaultLayout.labelStyle());
            countryLabel.setUnselectedStyle(DefaultLayout.labelStyle());
country =  new ComboBox(countryChoiceDesc);             
            country.setSelectedStyle(DefaultLayout.comboBoxPressedStyle());
            country.setUnselectedStyle(DefaultLayout.comboBoxNormalStyle());
            country.setPressedStyle(DefaultLayout.comboBoxPressedStyle());          
            CountryListener countryListener = new CountryListener();
            country.addSelectionListener(countryListener);      
    townLabel = new Label(resources.getString("Label12"));
            townLabel.setSelectedStyle(DefaultLayout.labelStyle());
            townLabel.setPressedStyle(DefaultLayout.labelStyle());
            townLabel.setUnselectedStyle(DefaultLayout.labelStyle());
//          townList = new List(townChoiceDesc);
//          townModel = townList.getModel();
//          
//          town = new ComboBox(townModel);
            town = new ComboBox(townChoiceDesc);
            town.setSelectedStyle(DefaultLayout.comboBoxPressedStyle());
            town.setUnselectedStyle(DefaultLayout.comboBoxNormalStyle());
            town.setPressedStyle(DefaultLayout.comboBoxPressedStyle());
            townListener townListener = new townListener();
            town.addSelectionListener(townListener);

            districtLabel = new Label(resources.getString("Label13"));
            districtLabel.setSelectedStyle(DefaultLayout.labelStyle());
            districtLabel.setPressedStyle(DefaultLayout.labelStyle());
            districtLabel.setUnselectedStyle(DefaultLayout.labelStyle());
//          districtList = new List(districtChoiceDesc);
//          districtModel = districtList.getModel();            
//          district = new ComboBox(districtModel);

            district = new ComboBox(districtChoiceDesc);
            district.setSelectedStyle(DefaultLayout.comboBoxPressedStyle());
            district.setUnselectedStyle(DefaultLayout.comboBoxNormalStyle());
            district.setPressedStyle(DefaultLayout.comboBoxPressedStyle());     

            cityLabel = new Label(resources.getString("Label11"));
            cityLabel.setSelectedStyle(DefaultLayout.labelStyle());
            cityLabel.setPressedStyle(DefaultLayout.labelStyle());
            cityLabel.setUnselectedStyle(DefaultLayout.labelStyle());
}catch (Exception e) {
            System.out.println("build ui"+e.getMessage());
            dialog = DefaultLayout.validation(AppConstants.exceptionMsg);
            dialog.show();
        }
    }   
class CountryListener implements SelectionListener {
        public void selectionChanged(int arg0, int arg1) {
            try {
//              Dialog.show("Beno","country action", "Ok", null);
                countryValue = countryChoice[country.getSelectedIndex()];
                getCity(countryValue);
                System.out.println("cityChoiceDesc.length :"+cityChoiceDesc.length);
                if (cityChoiceDesc.length > 0) {
//                  System.out.println("city set");
                     cityList = new List(cityChoiceDesc);       

                     ListModel cityModel = cityList.getModel();
                     city.setModel(cityModel);

                }
                    else {
                    String citylist[] = {};
                    List list = new List(citylist);
                    ListModel citylistModel = list.getModel();
                    city.setModel(citylistModel);
                    String townlist[] = {};
                    List list1 = new List(townlist);
                    ListModel townlistModel = list1.getModel();
                    town.setModel(townlistModel);
                    String districtlist[] = {};
                    List list2 = new List(districtlist);
                    ListModel districtlistModel = list2.getModel();
                    district.setModel(districtlistModel);
                }

            } catch (Exception e) {
                try {
                    dialog = DefaultLayout.validation(AppConstants.exceptionMsg);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                dialog.show();
            }
        }
    }

    class CityListener implements SelectionListener {
        public void selectionChanged(int arg0, int arg1) {
            try {

                cityValue = cityChoice[city.getSelectedIndex()];
//              city.setSelectedIndex(city.getSelectedIndex());
                getTown(cityValue);
                if (townChoiceDesc.length > 0) {
                    List list = new List(townChoiceDesc);
                    ListModel listModel = list.getModel();
                    town.setModel(listModel);
                } else {
                    String townlist[] = {};
                    List list1 = new List(townlist);
                    ListModel townlistModel = list1.getModel();
                    town.setModel(townlistModel);
                    String districtlist[] = {};
                    List list2 = new List(districtlist);
                    ListModel districtlistModel = list2.getModel();
                    district.setModel(districtlistModel);
                }
            } catch (Exception e) {
                try {
                    dialog = DefaultLayout.validation(AppConstants.exceptionMsg);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                dialog.show();
            }
        }
class townListener implements SelectionListener {
        public void selectionChanged(int arg0, int arg1) {
            System.out.println("town list");
            townValue = townChoice[town.getSelectedIndex()];
            try {
                getDistrict(townValue);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (districtChoiceDesc.length > 0) {
                List list = new List(districtChoiceDesc);
                ListModel listModel = list.getModel();
                district.setModel(listModel);
            } else {
                String districtlist[] = {};
                List list2 = new List(districtlist);
                ListModel districtlistModel = list2.getModel();
                district.setModel(districtlistModel);
            }

        }
    }

在选择国家时,它会调用城市监听器,但城市监听器未调用,但相应的值会在城市中更新。选择城市组合时,它并没有打电话给城镇听众,为什么有人帮助我。即使我选择城市组合框中的值,它也不会显示所选项目

1 个答案:

答案 0 :(得分:1)

您应该将动作侦听器与组合框一起使用而不是选择侦听器。不要创建新列表,只需在现有列表中使用setModel()。