简体   繁体   中英

Jframe. Get value from ComboBox, in another class, using getSelectedItem()

I have main jframe code in class:

@SuppressWarnings("serial")
public class CreateBuildAndPr extends JFrame  {

....some code...

    private JComboBox comboBoxClients = new JComboBox();
    private JComboBox comboBoxBranch = new JComboBox();

    ....some code...

        public String getClient(){
            String getClient = comboBoxClients.getSelectedItem().toString();
            //System.out.printf("\nClient: \n" + getClient);
            return getClient;
        }

        /**
         * Create the frame.
         */
        public CreateBuildAndPr() {
            lblCreateBuildAnd.setFont(new Font("Tahoma", Font.BOLD, 11));
            comboBoxBranch.setModel(new DefaultComboBoxModel(new String[] {"1a", "2a", "3a", "4a"}));
            comboBoxClients.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3"}));
            textFieldInfo.setColumns(10);
            btnCreateBuild.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {

                    CreateNewBuildAndPr callSe = new CreateNewBuildAndPr();
                    callSe.newBuild();
                }
            });

            initGUI();
        }

So when I call this method getClient(), in CreateBuildAndPr class, the selected value from ComboBox is correct. Lets say: "2". But when I call from other class, always the return result is "1". Here is the other class:

public class CreateNewBuildAndPr extends ConnectionsUrlAndDb {

    @Test
    public void newBuild() {


    CreateBuildAndPr createBuildAndPr = new CreateBuildAndPr();



        System.out.printf("\n\nSelenium: " +createBuildAndPr.getClient());
        String info = createBuildAndPr.getInfo();
        System.out.printf("\n\nSelenium: " +info);
        String branch = createBuildAndPr.getBranch();
        System.out.printf("\n\nSelenium: " +branch);

... more code .... }

How I can correct getSelectedItem in other class?

This line

CreateBuildAndPr createBuildAndPr = new CreateBuildAndPr();

creates a new object of that class. What you want is to reference the existing one with the JComboBox and its selected value. The solution would be to pass the combobox (or even a reference to the frame that contains it) to the CreateNewBuildAndPr object as a parameter.

For example modify your CreateNewBuildAndPr class to contain a variable

private JComboBox clientCombo;

and define a new constructor in that class as

public CreateNewBuildAndPr (JComboBox clientCombo)
{
    this.clientCombo = clientCombo
}

and in your JFrame ActionListener pass the combobox as a variable

CreateNewBuildAndPr callSe = new CreateNewBuildAndPr(comboBoxClients);

which will allow you to reference this combobox in CreateNewBuildAndPr class.

clientCombo.getSelectedItem()...

I just used the single JComboBox as an example here. If you passed the entire GUI object you could access it and use its methods, such as getInfo() that you seem to have there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM