如何在条件语句上使用JComboBox来显示不同的元素列表?

时间:2016-03-09 04:49:00

标签: java swing jcombobox

我有两个JComboBox个组件,wardbedno。在bedno ComboBox中选择G001 - G050时,我需要General Ward ComboBox显示床号范围ward

ward = new JComboBox();
    ward.setEnabled(false);
    ward.setModel(new DefaultComboBoxModel(new String[] {"Select Ward", "General Ward", "Private Ward", "Casualty Ward", "Cardiac Ward", "Pre-operative Ward", "Post-operative Ward", "Surgical Ward", "Pulmonary Ward", "Orthopaedic Ward", "Gynae & obstractles Ward", "Paediatric Ward", "Gastroenterology Ward", "ENT Ward", "Neurology Medicine Ward"}));
    ward.setBackground(Color.WHITE);
    ward.setBounds(124, 558, 249, 20);
    frm3.getContentPane().add(ward);

    bedno = new JComboBox();
    bedno.setEnabled(false);
    bedno.setModel(new DefaultComboBoxModel(new String[] {"Select Bed No.", "G001", "G002", "G003", "G004", "G005", "G006", "G007", "G008", "G009", "G010", "G011", "G012", "G013", "G014", "G015", "G016", "G017", "G018", "G019", "G020", "G021", "G022", "G023", "G024", "G025", "G026", "G027", "G028", "G029", "G030", "G031", "G032", "G033", "G034", "G035", "G036", "G037", "G038", "G039", "G040", "G041", "G042", "G043", "G044", "G045", "G046", "G047", "G048", "G049", "G050", "P051", "P052", "P053", "P054", "P055", "P056", "P057", "P058", "P059", "P060", "C061", "C062", "C063", "C064", "C065", "C066", "C067", "C068", "C069", "C070", "C071", "C072", "C073", "C074", "C075", "CD076", "CD077", "CD078", "CD079", "CD080", "CD081", "CD082", "CD083", "CD084", "CD085", "CD086", "CD087", "CD088", "CD089", "CD090", "PO091", "PO092", "PO093", "PO094", "PO095", "PO096", "PO097", "PO098", "PO099", "PO100", "POP101", "POP102", "POP103", "POP104", "POP105", "POP106", "POP107", "POP108", "POP109", "POP110", "S111", "S112", "S113", "S114", "S115", "S116", "S117", "S118", "S119", "S120", "S121", "S122", "PL123", "PL124", "PL125", "PL126", "PL127", "PL128", "PL129", "PL130", "PL131", "PL132", "OP133", "OP134", "OP135", "OP136", "OP137", "OP138", "OP139", "OP140", "OP141", "OP142", "G143", "G144", "G145", "G146", "G147", "G148", "G149", "G150", "G151", "G152", "G153", "G154", "G155", "G156", "G157", "G158", "G159", "G160", "G161", "G162", "PD163", "PD164", "PD165", "PD166", "PD167", "PD168", "PD169", "PD170", "PD171", "PD172", "GE173", "GE174", "GE175", "GE176", "GE177", "GE178", "GE179", "GE180", "GE181", "GE182", "ENT183", "ENT184", "ENT185", "ENT186", "ENT187", "ENT188", "ENT189", "ENT190", "NM191", "NM192", "NM193", "NM194", "NM195", "NM196", "NM197", "NM198", "NM199", "NM200", "", "", ""}));
    bedno.setBackground(Color.WHITE);
    bedno.setBounds(541, 558, 249, 20);
    frm3.getContentPane().add(bedno);

1 个答案:

答案 0 :(得分:1)

向病房JcomboBox添加一个监听器。获取选择并在bedno JComboBox中设置适当的值

你可以从ward组合框中为每个可能的选择设置单独的模型,就像camickr建议的那样。只需在if语句中设置适当的模型。

这样的事情:

 //define models here for each selection
    final DefaultComboBoxModel GeneralWardModel = new DefaultComboBoxModel(new String[]{"G001", "G002", "G003"..."G050"});

    ward.addActionListener (new ActionListener () {
        public void actionPerformed(ActionEvent e) {
            if ("General Ward".equals(ward.getSelectedItem())){
                //set the appropriate model here
                bedno.setModel(GeneralWardModel);
            } else {
                //more conditions if you need them    
            }
        }
    });