Question

need some help with ComboBoxes in Java. Looked through similar questions, found one slightly related, but not what im dealing with.

I need to load certain arrays into combo boxes depending on the items selected in the precious combo box:

think getting some procedure done at a medical center: Choose a procedure->get a list of doctors who do it, choose a doctor->get a list of available hours etc.

A single choice is working fine(whether it's "procedure->list of doctors", or "list of doctors->their working hours"), but doing more than a single one of those changes doesn't work.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;

public class GUIbandymas extends JFrame {


String[] start={"Choose","Choice1", "Choice2"}; 
String[] Option1={"Choose","A1"};
    String[] Option2={"Choose","A2","A3"};
    String[] Option3={"Choose","a","b","c","d"};
    String[] Option4={"Choose","1","2","3","4"};
    String[] Option5={"Choose","I","II","III","IV"};



String[] pradinis={"Pasirinkite Laika"};
String[] p1={"Pasirinkite Gydytoja"};
 static double kainaR;
  static double kainaK;
   JComboBox<String> G=new JComboBox<String>(p1);
   JComboBox<String> proc;
  JComboBox<String> laikas=new JComboBox<String>(pradinis);
  JComboBox<String> minutes;
  JButton button = new JButton ("Registuotis");
 JLabel label = new JLabel("Moketi uz vizita");
 JLabel suma = new JLabel();



public GUIbandymas() throws Exception {


        setValueProc(start);
        frame();


}


public void frame()
{
    JFrame frame = new JFrame();
    frame.setVisible(true);
    frame.setSize(500,300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    panel.add(proc);
    panel.add(G);
    panel.add(laikas);
    panel.add(button);
    button.setEnabled(false);


    //panel.add(minutes);
    frame.add(panel);

    panel.add(label);
    panel.add(suma);


      proc.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e) {
        if(proc.getSelectedItem().toString().equals("Choice1"))
        {
            setGyd(Option1);
        }
        if(proc.getSelectedItem().toString().equals("Choice2"))
        {
            setGyd(Option2);
        }
    }   
});


G.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent a) {

            if(G.getSelectedItem().toString().equals("A1"))
            {
                setLaikas(Option3);
            }
            if(G.getSelectedItem().toString().equals("A2"))
            {
                setLaikas(Option4);
            }
            if(G.getSelectedItem().toString().equals("A3"))
            {
                setLaikas(Option5);
            }

    }

});//JComboBox



}

public void setGyd(String[] s)
{
    G.removeAllItems();
    for(int i=0; i<s.length; i++)
    {
        G.addItem(s[i]);
    }



}



public void setValueProc(String[] sarasas)
{
    proc=new JComboBox<String>(sarasas);

}

public void setLaikas(String[] sarasas)
{

    laikas.removeAllItems();
    for(int i=0; i<sarasas.length; i++)
    {
        laikas.addItem(sarasas[i]);
    }       
}


}

Im in a dire need of any suggestions and possible fixes, im inclined to think that it has something to do with action listeners, since methods do work, but im at a loss since i cant determine what is it.

EDITED: the actual code should work, seems like there is no unneeded things from other files left. NOTE: this is work with GUI, just launch it in you main() :)

Was it helpful?

Solution

While I don't really like the if-else approach you're using, it should work just fine. I agree with rrirower's suggestion that you should look at using a data model instead. Especially if you get a lot of choices, since the code turns messy quite fast.

The problem with your code is that you run into NullPointerException when rebuilding the combobox items. The reason for this is that G.actionPerformed() is called when you remove/add items. After you have removed all items (before you start adding new ones), G.getSelectedItem() will return null.

If you code a little bit more defensively, then it works as expected:

    proc.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Object selectedItem = proc.getSelectedItem();
            if ("Choice1".equals(selectedItem)) {
                setGyd(Option1);
            }
            if ("Choice2".equals(selectedItem)) {
                setGyd(Option2);
            }
        }
    });
    G.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent a) {
            Object selectedItem = G.getSelectedItem();
            if ("A1".equals(selectedItem)) {
                setLaikas(Option3);
            }
            if ("A2".equals(selectedItem)) {
                setLaikas(Option4);
            }
            if ("A3".equals(selectedItem)) {
                setLaikas(Option5);
            }
        }
    });//JComboBox

Instead of checking for null's, I just flipped the equals and skipped the unnecessary toString() (they are already strings, that's what you put in there).

Another thing, a pet peeve of mine, please follow the normal java code convention for all your class, field and method names. You're almost there, but Option1 etc. should start with lowercase. G should probably have a more descriptive name, as well as start with lowercase.

Finally, I didn't understand why you both create a JFrame in the constructor and extend JFrame in your class. You should choose one or the other.

OTHER TIPS

You don't appear to be using a data model for the combo box. The data model controls the internal list of items. Have a look at this for more info.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top