java - 链接数据和 ComboBox 项目的最佳方式

标签 java swing data-structures jcombobox

我在制作 JComboBox 时遇到困难,该 JComboBox 将用于过滤和选择从内部数据结构中提取的特定自定义数据对象,并且 JComboBox 中显示的值只是该自定义中一个字段的值数据对象,甚至是封闭的自定义数据对象的字段中的字段(即自定义对象本身)。例如,我有设备型号注册表。

https://drive.google.com/open?id=1q-_ii_V7SWDBFvUJGw0cd2BEWP3BnM0H

模型具有定义它的名称、规范、设备类型和制造商。这是我使用的实际模型类:

public class Models
{
    private DeviceTypes deviceType;
    private Manufacturers manufacturer;
    private String name;

    //getters and setters
}

这是 HashMap 的进一步部分,其中包含所有模型及其 ID。

public Map<Integer,Models> modelsTable = new HashMap<Integer, Models>();

我希望能够从 JComboBox 添加和删除项目,并选择 JComboBox 项目代表的实际数据,以使用这些对象创建新模型。 执行此操作的标准方法是什么?我创建了一个 ComboBox 渲染器:

public class DeviceTypeRenderer extends BasicComboBoxRenderer
{
    private static final long serialVersionUID = 3442865560696889757L;

    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus)
    {
        if (value instanceof DeviceTypes)
        {
            value = ((DeviceTypes)value).getName();
        }

        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

        return this;
    }
}

public class ManufacturersRenderer extends BasicComboBoxRenderer
{
    private static final long serialVersionUID = 3723328665061573656L;

    public Component getListCellRendererComponent(JList<?> list, Object value,int index, boolean isSelected, boolean cellHasFocus)
    {
        if (value instanceof Manufacturers)
        {
            value = ((Manufacturers)value).getName();
        }

        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

        return this;
    }
}

然后我只需将它们作为数据对象添加或删除。

DeviceTypes deviceType = new DeviceTypes(name,description);

comboBoxDeviceType.addItem(deviceType);

并且 JComboBox 显示 deviceType.getName();

最好的逆向方法是什么?要从 JComboBox 项目选择中获取实际的数据类?我可能以错误的方式做了这一切,并且使用了很多不好的做法。如果您看到这一点,请通知我纠正,如果您能向我展示如何正确实现这一点,我将不胜感激。预先感谢您!

最佳答案

我认为通过使用 ComboBox 模型,我找到了更好、更优雅的整体解决方案。

首先,数据对象存储在带有字符串键的 HashMap 中,这些键是唯一的,代表每个数据对象的实际名称。

private HashMap<String, DataElement> uniqueStringMap = new HashMap<String, DataElement>();

第二个 ComboBox 模型是通过采用 HashMap 的键集从这些键创建的。

public static DefaultComboBoxModel<String> getModel(DataType dataType)
{
    String[] items = (String[]) DataManager.getDataTable(dataType)
                                           .getUniqueStringMap()
                                           .keySet().toArray();

    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(items);
    return model;
}

然后在 GUI Controller 中设置 ComboBox 模型。

deviceGUI.getDeviceRegistrationPanel().setDeviceTypeCmbModel(CmbModelFactory.getModel(DataType.DEVICE_TYPE));

在实际的 Swing 类中,JComboBox 不知道来自 Model 的实际数据对象,并且设计很简单。

private JComboBox<String> cmbDeviceType = new JComboBox<String>();

它的模型是通过 Controller 通过方法设置的。

public void setDeviceTypeCmbModel(ComboBoxModel<String> model)
{
    cmbDeviceType.setModel(model);
}

数据检索是通过获取组合框中选定的项目来完成的。

public String getDeviceType()
{
    return (String) cmbDeviceType.getSelectedItem();
}

然后该字符串值用于获取实际的数据对象。

public static DeviceType getDeviceType(String name)
{
    return (DeviceType) DataManager.getByUniqueString(DataType.DEVICE_TYPE, name);
}

我希望这对某人有帮助。

关于java - 链接数据和 ComboBox 项目的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58184314/

相关文章:

java - 如何在类(class)中单独更新外观和感觉干净?

c++ - 将唯一 ID 映射到对象的数据结构

java - 一旦工作表受到保护,启用 xls 中的下拉菜单

java - Javadoc 在 Eclipse 中做了什么?

java - 如何读取 Hibernate 映射

java - 无法在 Java6 中使用 SevenZFile 解压 7z 文件

java - 图形和缩放功能

java - 棋盘坐标

c++ - 在 C++ 中预热一些变量

java - 在集合中排序