简体   繁体   English

Jframe。 使用getSelectedItem()从ComboBox的另一个类中获取价值

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

I have main jframe code in class: 我在课堂上有主要的jframe代码:

@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. 因此,当我在CreateBuildAndPr类中调用此方法getClient()时,从ComboBox中选择的值是正确的。 Lets say: "2". 可以说:“ 2”。 But when I call from other class, always the return result is "1". 但是,当我从其他班级打电话时,返回的结果始终是“ 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? 如何在其他课程中更正getSelectedItem?

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. 您想要的是使用JComboBox及其选定的值引用现有的一个。 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. 解决方案是将组合框(甚至是包含它的框架的引用)作为参数传递给CreateNewBuildAndPr对象。

For example modify your CreateNewBuildAndPr class to contain a variable 例如,修改您的CreateNewBuildAndPr类以包含一个变量

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 并在您的JFrame ActionListener中将组合框作为变量传递

CreateNewBuildAndPr callSe = new CreateNewBuildAndPr(comboBoxClients);

which will allow you to reference this combobox in CreateNewBuildAndPr class. 这将允许您在CreateNewBuildAndPr类中引用此组合框。

clientCombo.getSelectedItem()...

I just used the single JComboBox as an example here. 我在这里仅以单个JComboBox为例。 If you passed the entire GUI object you could access it and use its methods, such as getInfo() that you seem to have there. 如果传递了整个GUI对象,则可以访问它并使用它的方法,例如您似乎在那里的getInfo()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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