0

I have two JComboBox components, ward and bedno. I need the bedno ComboBox to display the bed number range G001 - G050 when General Ward is selected in the ward ComboBox.

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);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Add an `ActionListener` to the first, when triggered, look up the ward and update the second. I'd personally have a `Map` with the key from the first containing a `List` of values for the second – MadProgrammer Mar 09 '16 at 05:12
  • Build a separate model for each item you can select. Something like this example: http://stackoverflow.com/questions/4982260/binding-comboboxes-in-swing/4982576#4982576 – camickr Mar 09 '16 at 05:42

1 Answers1

1

Add a listener to the ward JcomboBox. Get the selection and set the appropriate values inside the bedno JComboBox

You can have separate models for each possible selection from the ward combobox like camickr has suggested. Simply set the appropriate model inside the if statement.

Something like this:

 //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    
            }
        }
    });
Priyath Gregory
  • 927
  • 1
  • 11
  • 37