简体   繁体   English

从一个类到另一个Java Swing使用相同的JComboBox

[英]Use the same JComboBox from one class to another Java Swing

I have 2 classes. 我有2节课。 I'd like to have 2 identical JComboBox in each class. 我想在每个课程中有2个相同的JComboBox。 I did it successfully in the first class, because the values in my JComboBox are the same as the values in a JList, filled with text from a text field. 我在第一堂课中成功地做到了,因为JComboBox中的值与JList中的值相同,并填充了来自文本字段的文本。 But I could not manage to get the same result in the other class ! 但是我无法在另一堂课上得到同样的结果!

Extract of my first class : 我第一堂课的摘录:

    DefaultComboBoxModel Ajout = new DefaultComboBoxModel();
    btnValidate.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            String newCategorie = textCreerCategorie.getText();
            Ajout.addElement(newCategorie);
            list.setModel(Ajout);
            myComboB.setModel(Ajout);

So that's it ! 就是这样了 ! How can I have the same JComboBox in my other class and use my JList in it ? 我如何在另一个类中拥有相同的JComboBox并在其中使用我的JList?

What error did you get ? 您遇到什么错误?

Why couldn't you instantiate a new JComboBox in your second class ? 您为什么不能在第二堂课中实例化一个新的JComboBox? Provide some more details, so we can help you ! 提供更多详细信息,以便我们为您提供帮助!

First of all. 首先。 Are you importing the right libraries in your second class, so you can use JComboBox ? 您要在第二个类中导入正确的库,以便可以使用JComboBox吗? (ie javax.swing.*;) (即javax.swing。*;)

Try creating a new instance of a JComboBox in your second class and use that. 尝试在第二个类中创建JComboBox的新实例,然后使用它。 If you want to use the Ajout JComboBox that you instantiated in your first class, try and making it Global first and use it in your second class. 如果要使用在第一堂课中实例化的Ajout JComboBox,请尝试使其成为Global,然后在第二堂课中使用它。

Once again my answer is based on assumptions. 我的答案再次基于假设。 It could really help if we could see more of your code ! 如果我们能看到更多您的代码,那将真的有帮助! :) :)

You can do it using own class of ComboBoxModel : 您可以使用自己的ComboBoxModel类来实现:

class MyComboBoxModel extends DefaultComboBoxModel<Job> {
    public MyComboBoxModel(Job[] items) {
        super(items);
    }

    @Override
    public Job getSelectedItem() {
        Job selectedJob = (Job) super.getSelectedItem();

        // do something with this job before returning...

        return selectedJob;
    }
}

And later when you create JComboBox in any places using this model. 稍后,当您使用此模型在任何地方创建JComboBox时。

MyComboBoxModel myModel = new MyComboBoxModel(jobs);
JComboBox<Job> jobList = new JComboBox<Job>(myModel);

Thanks everyone for your answers. 谢谢大家的回答。 To be more specific, it is a shopping list where I have two class, one is for the main window where you can choose a category and product and display in a list. 更具体地说,这是我有两个班级的购物清单,一个是主窗口,您可以在其中选择类别和产品并在列表中显示。

The otherone is for a windows that manage your categories and products so when a categorie is created (from a jtext field and add to my jlist) I want it to add it in a ComboBox that is in the main window. 另一个是用于管理您的类别和产品的窗口,因此在创建类别时(从jtext字段添加到我的jlist),我希望它将其添加到主窗口中的ComboBox中。 I hope you guys will understand me (french girl over here). 我希望你们能理解我(这里的法国姑娘)。

In the modification class : 在修改类中:

    DefaultComboBoxModel Ajout = new DefaultComboBoxModel();
    btnValiderCategorie.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            String newCategorie = textCreerCategorie.getText();
            Ajout.addElement(newCategorie);
            list.setModel(Ajout);
            ajouterACategorie.setModel(Ajout);

This allows me to add at the same time a new categorie in my jlist and in a comboBox that is in the modification windows. 这使我可以同时在jlist和修改窗口中的comboBox中添加新类别。 Then I'd like to have the same combobox in the main window so the user can choose a categorie that he created in the other class. 然后,我想在主窗口中具有相同的组合框,以便用户可以选择他在另一个类中创建的类别。 The main class does a lot of other things but my preocupation in to have the same effect in this combobox 主类做很多其他事情,但我的专心致志在此组合框中具有相同的作用

    JComboBox<String> categories_content = new JComboBox<String>();
    categories_content.setBounds(140, 63, 157, 30);

than the other comboBox in the modification class. 而不是修改类中的另一个comboBox。 Sorry guys if it's too confuse i'd understand 抱歉,如果我太困惑了

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

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