link 数据和 ComboBox 项目的最佳方式
Best way to link data and ComboBox items
我在制作一个 JComboBox 时遇到困难,该 JComboBox 将用于过滤 select 从内部数据结构中提取的特定自定义数据对象,并且 JComboBox 中显示的值只是一个值该自定义数据对象中的一个字段,甚至是封闭的自定义数据对象的一个字段(即自定义对象本身)。比如我有设备型号登记表。
https://drive.google.com/open?id=1q-_ii_V7SWDBFvUJGw0cd2BEWP3BnM0H
模型有名称、规格、设备类型和定义它的制造商。这是我使用的实际模型 class:
public class Models
{
private DeviceTypes deviceType;
private Manufacturers manufacturer;
private String name;
//getters and setters
}
这是包含所有模型及其 ID 的 HashMap 的进一步部分。
public Map<Integer,Models> modelsTable = new HashMap<Integer, Models>();
我希望能够在 JComboBox 中添加和删除项目,并希望能够 select 实际数据与 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 项目 selection 中获取实际数据 class?我可能以错误的方式做了这一切,并且使用了很多不好的做法。如果您看到,请通知我改正,如果您能告诉我如何正确实施,我将不胜感激。提前致谢!
我想我通过使用 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 控制器中设置 ComboBox 模型。
deviceGUI.getDeviceRegistrationPanel().setDeviceTypeCmbModel(CmbModelFactory.getModel(DataType.DEVICE_TYPE));
而在实际的 Swing 中 class JComboBox 不知道模型中的实际数据对象,它的设计很简单。
private JComboBox<String> cmbDeviceType = new JComboBox<String>();
它的模型是通过一种方法从控制器中设置的。
public void setDeviceTypeCmbModel(ComboBoxModel<String> model)
{
cmbDeviceType.setModel(model);
}
通过获取 ComboBox 中的选定项目来完成数据检索。
public String getDeviceType()
{
return (String) cmbDeviceType.getSelectedItem();
}
然后该字符串值用于获取实际数据对象。
public static DeviceType getDeviceType(String name)
{
return (DeviceType) DataManager.getByUniqueString(DataType.DEVICE_TYPE, name);
}
我希望这对某人有所帮助。
我在制作一个 JComboBox 时遇到困难,该 JComboBox 将用于过滤 select 从内部数据结构中提取的特定自定义数据对象,并且 JComboBox 中显示的值只是一个值该自定义数据对象中的一个字段,甚至是封闭的自定义数据对象的一个字段(即自定义对象本身)。比如我有设备型号登记表。
https://drive.google.com/open?id=1q-_ii_V7SWDBFvUJGw0cd2BEWP3BnM0H
模型有名称、规格、设备类型和定义它的制造商。这是我使用的实际模型 class:
public class Models
{
private DeviceTypes deviceType;
private Manufacturers manufacturer;
private String name;
//getters and setters
}
这是包含所有模型及其 ID 的 HashMap 的进一步部分。
public Map<Integer,Models> modelsTable = new HashMap<Integer, Models>();
我希望能够在 JComboBox 中添加和删除项目,并希望能够 select 实际数据与 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 项目 selection 中获取实际数据 class?我可能以错误的方式做了这一切,并且使用了很多不好的做法。如果您看到,请通知我改正,如果您能告诉我如何正确实施,我将不胜感激。提前致谢!
我想我通过使用 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 控制器中设置 ComboBox 模型。
deviceGUI.getDeviceRegistrationPanel().setDeviceTypeCmbModel(CmbModelFactory.getModel(DataType.DEVICE_TYPE));
而在实际的 Swing 中 class JComboBox 不知道模型中的实际数据对象,它的设计很简单。
private JComboBox<String> cmbDeviceType = new JComboBox<String>();
它的模型是通过一种方法从控制器中设置的。
public void setDeviceTypeCmbModel(ComboBoxModel<String> model)
{
cmbDeviceType.setModel(model);
}
通过获取 ComboBox 中的选定项目来完成数据检索。
public String getDeviceType()
{
return (String) cmbDeviceType.getSelectedItem();
}
然后该字符串值用于获取实际数据对象。
public static DeviceType getDeviceType(String name)
{
return (DeviceType) DataManager.getByUniqueString(DataType.DEVICE_TYPE, name);
}
我希望这对某人有所帮助。