Java从另一个类获取选定的组合框

Java从另一个类获取选定的组合框,java,swing,combobox,Java,Swing,Combobox,我是新手。首先,如果这篇文章不遵守stackoverflow的规则,我很抱歉。我想问同样的问题(我认为它有错误的答案)从3年前从这个来源: 如何从一个类中获取ComboBox的选定项,并在新类中使用该选定项的值 比如说,源类和其他类。我想从其他类的源类打印第3项(组合框中的第三项) 我已经使用了上述来源的答案。然而,它只返回第一项。因为我认为每次从源类调用构造函数时,它都会将所选项重新启动到第一个项 当我使用javax.swing.JFrame(我使用Netbeans)时,如何做到这一点 另一

我是新手。首先,如果这篇文章不遵守stackoverflow的规则,我很抱歉。我想问同样的问题(我认为它有错误的答案)从3年前从这个来源:

如何从一个类中获取ComboBox的选定项,并在新类中使用该选定项的值

比如说,源类和其他类。我想从其他类的源类打印第3项(组合框中的第三项)

我已经使用了上述来源的答案。然而,它只返回第一项。因为我认为每次从源类调用构造函数时,它都会将所选项重新启动到第一个项

当我使用javax.swing.JFrame(我使用Netbeans)时,如何做到这一点

另一类:

public class Other extends javax.swing.JFrame{

public Other(){

Source sc = new Source();
String var = sc.getSelectedItem();

System.out.println(var);
}
}
假设我在源类中选择了第3项。那么它会在其他课程中获得第3项吗?还是我做错了?很抱歉给您带来不便

我想问同样的问题(它有错误的答案)从3年前

不,这个答案完全正确

如何从一个类中获取ComboBox的选定项,并在新类中使用该选定项的值

同样,Reimeus会告诉你如何正确操作

比如说,源类和其他类。我想从其他类的源类打印第3项(组合框中的第三项)

不确定您所说的
“项目3”
是什么意思,但如果它是另一个选定的项目,您所指的答案同样是正确的

我已经使用了上述来源的答案。然而,它只返回第一项。因为我认为每次从源类调用构造函数时,它都会将所选项重新启动到第一个项

这正是你的问题——你不应该重新调用构造函数,因为那样会给你错误的引用。它将为您提供一个全新的GUI引用,一个指向未显示的GUI的引用。您需要当前查看的兴趣类的引用。你如何做到这一点将取决于你的程序结构——请展示更多

例如:请注意,以下代码有两个JButton,一个在ActionListener中正确执行操作(实际上是一个AbstractAction,但构造类似),另一个执行错误操作:

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class GetCombo extends JFrame {
    // the displayed ClassWithCombo object
    private ClassWithCombo classWithCombo = new ClassWithCombo(this);;
    private int columns = 10;
    private JTextField textField1 = new JTextField(columns);
    private JTextField textField2 = new JTextField(columns);

    public GetCombo() {
        classWithCombo.pack();
        classWithCombo.setLocationByPlatform(true);
        classWithCombo.setVisible(true);

        setLayout(new FlowLayout());

        add(textField1);
        add(new JButton(new AbstractAction("Doing It Right") {

            @Override
            public void actionPerformed(ActionEvent e) {
                // use the ClassWithCombo reference that is already displayed
                String selectedString = classWithCombo.getSelectedItem();
                textField1.setText(selectedString);
            }
        }));
        add(textField2);
        add(new JButton(new AbstractAction("Doing It Wrong") {

            @Override
            public void actionPerformed(ActionEvent e) {
                // create a new non-displayed ClassWithCombo reference.
                ClassWithCombo classWithCombo = new ClassWithCombo(GetCombo.this);
                String selectedString = classWithCombo.getSelectedItem();
                textField2.setText(selectedString);
            }
        }));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            GetCombo getCombo = new GetCombo();
            getCombo.setTitle("Get Combo Example");
            getCombo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            getCombo.pack();
            getCombo.setLocationRelativeTo(null);
            getCombo.setVisible(true);
        });
    }
}

@SuppressWarnings("serial")
class ClassWithCombo extends JDialog {
    private static final String[] DATA = { "Mon", "Tues", "Wed", "Thurs", "Fri" };
    private JComboBox<String> combo = new JComboBox<>(DATA);

    public ClassWithCombo(JFrame frame) {
        super(frame, "Holds Combo Dialog", ModalityType.MODELESS);
        setLayout(new FlowLayout());
        setPreferredSize(new Dimension(300, 250));
        add(combo);
    }

    public String getSelectedItem() {
        return (String) combo.getSelectedItem();
    }

}

HTML?摆动AWT?JavaFX?SWT?是javax.swing.JFrame@madprogrammer谢谢。对不起,我的帖子不好(还在学习)。我已经编辑过了。怎么样?@ArbintoroMas:我无法编译和运行你发布的代码,所以我无法告诉你如何修复它。关键在于理解引用是什么——必须在引用包含JComboBox的可视化GUI的引用上调用
getSelectedItem()
方法(根据Reimeus的回答)。一个很好的测试是,
newsource()
应该只在整个组合程序中出现一次。若您不止一次看到它,那个么您很可能做错了。@ArbintoroMas:请参阅答案中添加的代码示例。不要使用两个JFrame,因为GUI应该只有一个JFrame。如果你有多个窗口,一个应该是JFrame,另一个是JDialogs。所以我不能使用两个JFrame,先生?我已经在下面发布了可编译代码。我使用两个jframe,因为我使用的是Netbeans。很抱歉给您带来不便*我正在检查你的代码示例(谢谢)@ArbintoroMas:请注意编辑以回答。还请注意,您的“答案”不应作为答案发布,因为这不是答案。相反,你应该编辑你的主要问题,并在那里提供这些信息。另外,请避免要求任何人更正您的代码。我们是志愿者,希望帮助您更正代码,但我们不是来为您编写代码的。希望我的回答能帮助你做到这一点。
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class GetCombo extends JFrame {
    // the displayed ClassWithCombo object
    private ClassWithCombo classWithCombo = new ClassWithCombo(this);;
    private int columns = 10;
    private JTextField textField1 = new JTextField(columns);
    private JTextField textField2 = new JTextField(columns);

    public GetCombo() {
        classWithCombo.pack();
        classWithCombo.setLocationByPlatform(true);
        classWithCombo.setVisible(true);

        setLayout(new FlowLayout());

        add(textField1);
        add(new JButton(new AbstractAction("Doing It Right") {

            @Override
            public void actionPerformed(ActionEvent e) {
                // use the ClassWithCombo reference that is already displayed
                String selectedString = classWithCombo.getSelectedItem();
                textField1.setText(selectedString);
            }
        }));
        add(textField2);
        add(new JButton(new AbstractAction("Doing It Wrong") {

            @Override
            public void actionPerformed(ActionEvent e) {
                // create a new non-displayed ClassWithCombo reference.
                ClassWithCombo classWithCombo = new ClassWithCombo(GetCombo.this);
                String selectedString = classWithCombo.getSelectedItem();
                textField2.setText(selectedString);
            }
        }));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            GetCombo getCombo = new GetCombo();
            getCombo.setTitle("Get Combo Example");
            getCombo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            getCombo.pack();
            getCombo.setLocationRelativeTo(null);
            getCombo.setVisible(true);
        });
    }
}

@SuppressWarnings("serial")
class ClassWithCombo extends JDialog {
    private static final String[] DATA = { "Mon", "Tues", "Wed", "Thurs", "Fri" };
    private JComboBox<String> combo = new JComboBox<>(DATA);

    public ClassWithCombo(JFrame frame) {
        super(frame, "Holds Combo Dialog", ModalityType.MODELESS);
        setLayout(new FlowLayout());
        setPreferredSize(new Dimension(300, 250));
        add(combo);
    }

    public String getSelectedItem() {
        return (String) combo.getSelectedItem();
    }

}
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class GetCombo2 extends JFrame {
    // the displayed ClassWithCombo object
    private ClassWithCombo classWithCombo = new ClassWithCombo(this);;
    private int columns = 10;
    private JTextField textField1 = new JTextField(columns);

    public GetCombo2() {
        classWithCombo.pack();
        classWithCombo.setLocationByPlatform(true);

        // !! don't do this here
        // classWithCombo.setVisible(true);

        setLayout(new FlowLayout());

        textField1.setFocusable(false);
        add(textField1);
        add(new JButton(new AbstractAction("Open Combo as a Dialog") {

            @Override
            public void actionPerformed(ActionEvent e) {
                // open combo dialog as a **modal** dialog:
                classWithCombo.setVisible(true);

                // this won't run until the dialog has been closed
                String selectedString = classWithCombo.getSelectedItem();
                textField1.setText(selectedString);
            }
        }));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            GetCombo2 getCombo = new GetCombo2();
            getCombo.setTitle("Get Combo Example");
            getCombo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            getCombo.pack();
            getCombo.setLocationRelativeTo(null);
            getCombo.setVisible(true);
        });
    }
}

@SuppressWarnings("serial")
class ClassWithCombo extends JDialog {
    private static final String[] DATA = { "Mon", "Tues", "Wed", "Thurs", "Fri" };
    private JComboBox<String> combo = new JComboBox<>(DATA);

    public ClassWithCombo(JFrame frame) {
        // !! don't make it MODELESS
        // !! super(frame, "Holds Combo Dialog", ModalityType.MODELESS);
        // !! note the change. Made it APPLICATION_MODAL
        super(frame, "Holds Combo Dialog", ModalityType.APPLICATION_MODAL);

        JButton submitButton = new JButton(new AbstractAction("Submit") {
            // !! add an ActionListener to close window when the submit button
            // has been pressed.

            @Override
            public void actionPerformed(ActionEvent e) {
                ClassWithCombo.this.setVisible(false);
            }
        });

        setLayout(new FlowLayout());
        setPreferredSize(new Dimension(300, 250));
        add(combo);
        add(submitButton);
    }

    public String getSelectedItem() {
        return (String) combo.getSelectedItem();
    }

}