Tomasz Kostrzewski
Tomasz Kostrzewski

Reputation: 63

How to get content from JComboBox from another class

i'm working on some app. on main page (register page) there is a combobox with few options to choose. Depend on what will be choose, specific content will apear in next window. Question is how to getContent from this combobox which is in other class.

I assume that some if control instruction need to be added, but everytime i add something it returns an error.

Can someone please help how code should look like please ? For example if option "1 " will be choosen , set background to Black, if "2" set background to Pink etc.

Register window:

public Main() throws Exception {

        registerWindowFrame = new JFrame("xxx");                                                    
        //registerWindowFrame.setSize(1440,2960);                                                                        
        registerWindowFrame.setSize(500, 750);
        registerWindowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                                              
        registerWindowFrame.setLayout(null);
        registerWindowFrame.getContentPane().setBackground(Color.RED);

BloodList bloodList = new BloodList();
        bloodList.setSize(bloodList.getPreferredSize());
        bloodList.setLocation(10, 365);
        registerWindowFrame.add(bloodList);

comboxbox class:

public class BloodList extends JComboBox <String> {

    int i;

    public String[] bloodList =
            {
                    "1",
                    "2",
                    "3",

            };

    public BloodList() {
        for (i=0; i < bloodList.length; i++)
        {
            this.addItem(bloodList[i]);
        };
    }

}

Window after regration window:

public mainWindowBplus() {
        super();
        bloodBplusFrame = new JFrame("SETTINGS");
        bloodBplusFrame.setSize(500, 750);
        bloodBplusFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        bloodBplusFrame.setLayout(null);
        bloodBplusFrame.getContentPane().setBackground(Color.BLUE);
    bloodBplusFrame.setVisible(true);

Upvotes: 1

Views: 136

Answers (1)

Daniel_Kamel
Daniel_Kamel

Reputation: 619

You should add an action listener to you jcombobox which gets the value of the chosen String and then use that value in the other class, try this:

public class BloodList extends JComboBox <String> {
    private String s="";

    private String[] bloodList =
            {
                    "1",
                    "2",
                    "3",

            };

    public BloodList() {
        for (int i=0; i < bloodList.length; i++)
        {
            this.addItem(bloodList[i]);
        };
    }

    ActionListener cbActionListener = new ActionListener() {//add actionlistner to listen for change
            @Override
            public void actionPerformed(ActionEvent e) {

                s = (String) BloodList.this.getSelectedItem();//get the selected string
            }
    };

    this.addActionListener(cbActionListener);

    public String getS(){return s;}

}

Now you can use that String in another class by using the getS() method.

Upvotes: 1

Related Questions