两个组合框之间的共享数据 [英] Shared data between two comboboxes

查看:49
本文介绍了两个组合框之间的共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在两个(或更多个)组合框之间共享数据,但我想独立选择元素.例如,如果我在第一个comboBox中选择Object1,则我的第二个ComboBox也选择Object1,因为它们具有相同的模型(DefaultComboBoxModel,并且此模型还管理选定的对象).但是我不想要这种行为.我想在我的comboBoxes中独立选择对象.当我在第一个comboBox中选择对象时,我的第二个comboBox不应更改.

I need to share data between two(or maybe more) comboboxes, but I want to choose elements independently. For example, if I choose Object1 in first comboBox, my second ComboBox also chooses Object1, because they have same model(DefaultComboBoxModel and this model also manages chosen objects). But I don't want this behavior. I want to choose objects in my comboBoxes independently. When I choose object in first comboBox my second comboBox shouldn't change.

此刻,我正在考虑两个模型的超级模型.超模型会将事件发送到子模型,并且它们将更新组合框数据,但不会更新状态.但是我认为这不是最好的方法.

At this moment I'am thinking about supermodel for two models. Supermodel will send events to submodels and they will update comboboxes data, but not the state. But I think this is not the best way.

还有更多有趣且简单的方法吗?

Are there more intresting and simple aproaches?

这是理解我的意思的简短代码:

Here is the short code to understand what I mean:

package hello;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
public class Comboboxes extends JFrame
{
private JPanel contentPane;
public static void main(String[] args)
{
    EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            try
            {
                Comboboxes frame = new Comboboxes();
                frame.setVisible(true);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    });
}

public Comboboxes()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JComboBox one = new JComboBox();
    one.setBounds(10, 11, 414, 26);
    contentPane.add(one);

    JComboBox two = new JComboBox();
    two.setBounds(10, 52, 414, 26);
    contentPane.add(two);

    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
    model.addElement("Item 1");
    model.addElement("Item 2");
    model.addElement("Item 3");

    one.setModel(model);
    two.setModel(model);
}
}

推荐答案

ComboBoxModel写一个装饰器.装饰器应该管理selectedItem属性,而其他所有内容都由委托管理.

Write a decorator for your ComboBoxModel. The decorator should manage the selectedItem property, while everything else is managed by the delegate.

您将拥有1个原始模型,并在组合框上放置不同的装饰器:

You would then have 1 original model, and place different decorators on the comboboxes:

DefaultComboBoxModel original = ...;

DecoratedModel firstModel = new DecoratedModel( original );
JComboBox firstCombo = new JComboBox( firstModel );

DecoratedModel secondModel = new DecoratedModel( original );
JComboBox secondCombo = new JComboBox( secondModel );

然后可以在original模型上执行数据更改,该模型将同时调整所有组合框中的数据

Changes to the data can then be performed on the original model, which will adjust the data in all comboboxes simultaneously

注意:确保连接到装饰器的侦听器以装饰的模型作为源而不是委托模型接收事件.这是编写装饰器时的常见错误

Note: make sure listeners attached to the decorator receive events with the decorated model as source, and not with the delegate model. This is a common mistake when writing a decorator

修改

另一种选择是拥有不是ComboBoxModel的基本数据结构,并创建使用该数据结构的ComboBoxModel的实现.然后,您可以创建所有共享相同数据结构的不同组合框模型实例.

An alternative is to have a base data structure which is not a ComboBoxModel and create an implementation of ComboBoxModel which uses that data structure. You can then create different combobox model instances which all share the same data structure.

这篇关于两个组合框之间的共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆