【问题标题】:how to use a JComboBox on a conditional statement to show different list of elements?如何在条件语句上使用 JComboBox 来显示不同的元素列表?
【发布时间】:2016-06-23 07:51:39
【问题描述】:

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

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);

【问题讨论】:

标签: java swing jcombobox


【解决方案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    
            }
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 2022-01-05
    • 2022-08-21
    • 2021-10-08
    • 2022-04-06
    • 2017-03-11
    • 1970-01-01
    相关资源
    最近更新 更多