0

i am making a medical drug software program and i want the user to be able to select a sickness category (respiratory disease, cardiovascular disease etc) from a jcombo box that is populated by the category array.

Once this category is selected i want another jcombobox to be populated only with drugs relevant to that category.

The problem i am having is that i cant seem to get the values of whats in the jcombobox and use those in an If statement to then populate or make visible the second box with the relevant drugs.

 public CatergoryBar(ArrayList<Catergory> category)
{
    new GridBagLayout();
    Dimension dim = getPreferredSize();
    dim.width = 250;
    setPreferredSize(dim);
    setBorder(BorderFactory.createEtchedBorder());

    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 1;
    c.weighty = 1;
    c.fill = GridBagConstraints.NONE;

    categoryBox = new JComboBox(category.toArray()); 
    add(categoryBox, c);
    c.gridy++;



    categoryBox.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
           String item = String.valueOf(categoryBox.getSelectedItem());

           if(item.equals("Respiratory Disease"))
           {
               respBox = new JComboBox(PFormulary.respDiseases.toArray()); 
               add(respBox, c);
               c.gridy++;
           }
        }
    });{

My respiratory combobox gets displayed if i do it normally outside of the item listener but as soon as i put it inside of the item listener nothing pops up when i click on "Respiratory Disease".

Thanks in advance!

awyeanah2
  • 128
  • 9
  • 1
    Put the 2nd combo box in one card of a card layout. Put a blank panel in the other card. When the 2nd is populated, flip from the blank panel to the combo. By using this approach, the layouts can account for the potential future appearance of the 2nd combo. General tips: 1) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). 2) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jul 20 '19 at 00:46
  • Thank you andrew, after abit of tinkering i have got it working. But lets say for example i wanted to add a third JCombobox underneath the second depending on what i clicked in the second, that would still be doable? – awyeanah2 Jul 20 '19 at 03:21
  • The approach outlined in my comment is good for as many combos as can be squeezed into a GUI. Just be sure to call [`setPrototypeDisplayValue(Object)`](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/JComboBox.html#setPrototypeDisplayValue(E)) which *"Sets the prototype display value used to **calculate the size** of the display for the UI portion"*. I'd give it the largest string expected to be displayed in that combo. – Andrew Thompson Jul 20 '19 at 03:46

1 Answers1

0

Possible duplicate: java swing dynamically adding components

In summary, call revalidate() after adding the new component. This is necessary because, within the listener, you are adding components dynamically, so you need to instruct the layout manager to revalidate and repaint dirty regions.

Alexander Guyer
  • 2,063
  • 1
  • 14
  • 20
  • Less optimal than using a `CardLayout` as described above. – Andrew Thompson Jul 20 '19 at 00:46
  • Switching between blank and populated components only works if each and every component is known when the layout is initialized. If the number of components changes after initialization, you have to revalidate and repaint, as far as I am aware. As such, while a `CardLayout` may work at this stage of the application, it's very possible it will evolve to have more dynamic requirements, and adding and removing components after initialization will be a necessity – Alexander Guyer Jul 20 '19 at 23:57
  • *"Switching between blank and populated components only works if each and every component is known when the layout is initialized."* In this case it is 2. Two combo boxes. – Andrew Thompson Jul 21 '19 at 00:09