简体   繁体   English

设置另一个班级的组合框

[英]Setting combobox from another class

Is it possible to set the selected item of a combobox from a different class? 是否可以设置其他类别的组合框的选定项目? Say I have Class A and Class B, in class AI setup the combobox and add it to the Jframe. 假设我有A类和B类,在AI类中设置组合框并将其添加到Jframe中。 Could I do something like ClassA.comboBox.setSelectedItem("Option1"); 我可以做类似ClassA.comboBox.setSelectedItem(“ Option1”);的事情吗? from class B? 从B班? That doesn't work but I hope it illustrates what I am trying to do. 那没有用,但我希望它能说明我正在尝试做的事情。 Do you know how/if I can achieve this result with Java? 您知道/如何使用Java可以达到这个结果吗? Many thanks. 非常感谢。

This a common enough problem which can solved in several ways 这是一个常见的问题,可以通过多种方式解决

You could... 你可以...

Pass a reference of A to B , so B can call what ever methods A has available to change it's state. A的引用传递给B ,以便B可以调用方法A可以更改其状态的任何方法。

You should avoid allowing direct access to your classes fields, because it would allow B to do things you might not want it to do. 您应避免允许直接访问您的类字段,因为这将允许B做您可能不希望做的事情。

Equally, you should prefer interface s over passing actually implementations, further restricting what B can do to A . 同样,与实际传递的实现相比,您应该更喜欢interface ,这进一步限制了B可以对A做什么。

This, however, does tighten the coupling of your code. 但是,这确实加强了代码的耦合。 What should A have to have a reference to B to do anything (or visa-versa) A做某事必须参考B内容(反之亦然)

You could... 你可以...

Use an Observer Pattern , allowing B to generate events that interested parties (like A ) so that they can update themselves accordingly, based on there own choices, the event and/or the state of B 使用观察者模式 ,允许B生成感兴趣方(例如A )的事件,以便他们可以根据自己的选择,事件和/或B的状态进行相应的更新。

This de-couples your code ( B no longer needs an instance of A to do anything) and makes your code more re-usable, other classes can register interest with instances B and B doesn't care 这使您的代码解耦( B不再需要A的实例来做任何事情),并使您的代码更可重用,其他类可以向实例B注册兴趣,而B不在乎

You shouldn't... 你不应该

Use static . 使用static static is a BAD, BAD idea when it comes to cross communication between objects. 当涉及对象之间的交叉通信时, static是一个糟糕的想法。 The moment you have more then one instance of any class, the whole thing just blows up into a horrible mess. 一旦您拥有一个以上任何一个类的实例,整个事情就会变得一团糟。 You also lose a lot of control over who is doing what to those instances (nothing stopping any one from creating a new instance of a class and assigning it to the static field), all of which just makes your life a misery (or in the case of the developers who have to clean it up, your name becomes a dirty word) 您还失去了对谁对这些实例做什么的控制权(没有阻止任何人创建类的新实例并将其分配给static字段),所有这些都使您的生活陷入困境(或在痛苦中)。对于必须清理的开发人员,您的名字变成了一个肮脏的词)

If you have an instance of A in B the better option is to have getters/setters for the elements you need in A : 如果您在B中有一个A实例,那么更好的选择是让A中的元素具有getter / setter方法:

Class A {
    private JComboBox comboBox = new JComboBox();

    public A(){}
    public JCombobox getComboBox(){ return comboBox;}
}

And in B : 在B中:

Class B {
    public B(){
    A aInstance = new A();
    aInstance.getComboBox().setSelectedItem("Option1");
}

You can first create the class B and then pass the reference A to the class B , then in the class B you can do what you want with class A . 您可以首先创建类B ,然后将引用A传递给类B ,然后在类B可以对类A做您想做的事情。 A simple example: 一个简单的例子:

class A extends JFrame {

    private ComboBox comboBox;

    public A() {
      comboBox = new ComboBox();
      B b = new B(this);
    }

    public void addItem(Obejct item) {
        // add item to comboBox
    }

}

class B {
    private A a;

    public B(A a) {
         this.a = a;
         String item = "new Item";
         a.addItem(item);
    }

}

Hope this help! 希望对您有所帮助!

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

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