第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

鏈接數據和 ComboBox 項目的最佳方式

鏈接數據和 ComboBox 項目的最佳方式

婷婷同學_ 2023-09-13 17:33:06
我在制作 JComboBox 時遇到困難,該 JComboBox 將用于過濾和選擇從內部數據結構中提取的特定自定義數據對象,并且 JComboBox 中顯示的值只是該自定義數據對象中一個字段的值,甚至是封閉的自定義數據對象的字段的字段(即自定義對象本身)。例如,我有設備型號注冊表。https://drive.google.com/open?id=1q-_ii_V7SWDBFvUJGw0cd2BEWP3BnM0H模型具有定義它的名稱、規(guī)格、設備類型和制造商。這是我使用的實際模型類: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 添加和刪除項目,并選擇 JComboBox 項目代表的實際數據,以使用這些對象創(chuàng)建新模型。執(zhí)行此操作的標準方法是什么?我創(chuàng)建了一個組合框渲染器: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 項目選擇中獲取實際的數據類?我可能以錯誤的方式做了這一切,并且使用了很多不好的做法。如果您看到這一點,請通知我糾正,如果您能向我展示如何正確實施這一點,我將不勝感激。先感謝您!
查看完整描述

1 回答

?
阿波羅的戰(zhàn)車

TA貢獻1862條經驗 獲得超6個贊

我想我通過使用 ComboBox 模型找到了更好、更優(yōu)雅的整體解決方案。


首先,數據對象存儲在帶有字符串鍵的 HashMap 中,這些鍵是唯一的,代表每個數據對象的實際名稱。


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

第二個 ComboBox 模型是通過采用 HashMap 的鍵集從這些鍵創(chuàng)建的。


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 類中,JComboBox 不知道來自 Model 的實際數據對象,并且設計簡單。


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

它的模型是通過控制器通過方法設置的。


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);

}

我希望這可以幫助別人。


查看完整回答
反對 回復 2023-09-13
  • 1 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號