簡體   English   中英

填充JComboBox的正確方法?

[英]The correct way to populate a JComboBox?

我目前正在Eclipse上使用Java在Java上構建應用程序,作為編程基礎知識和基本Java編程的自助指南,這純粹是出於教育目的,其唯一目的是能夠輕松引用主題並在我通過編程學習它們的過程中進行編程進入本教程應用程序。

隨着時間的流逝以及我學習更多的編程組件,應用程序的內容將不斷擴展。

所以我的第一個問題歸結為正確的形式。

我正在使用一個下拉框( JComboBox ),以便從GUI中選擇特定主題。 我想填充列表並保持程序整潔。 所以我的問題是,如何填充JComboBox以限制混亂的代碼。 也許是一個文本文件,我可以從該文本文件中單獨添加主題並更有效地進行編輯? 我追求正確的編程過程,而不是采用所有可能的方法。 我知道我可以使用ArrayList ,但是我渴望了解使用大量內容而不是少量內容時所采取的選擇。

謝謝,

西蒙

我認為最干凈的方法是定義自定義ComboBoxModel

這樣,您可以為組合框定義數據模型,從而將創建組合框的部分與數據管理本身分開。

可能使用文本文件是一件好事,因為在插入新條目時不必修改代碼。 您可以在ComboBoxModel構造函數中定義讀取文件過程。 這樣,每次您運行程序時,您都會找到更新的組合框的內容。

如果內容不能由應用程序本身更新,則ArrayList不是一個不錯的選擇。 如果要對數組列表的內容進行硬編碼,則每次需要添加新條目時都將被迫修改代碼。

一個小例子:

class YourModel implements ComboBoxModel{

//implements all interface methods required...
@override
public YourModel(String filename)
{
    comboBoxItemList = new ArrayList<String>();
    // open your file
    // add every entry to the the list
}
@override
public Object getElementAt(int index)
{
    return comboBoxItemList.get(index);
}
List<String> comboBoxItemList;
}

編寫所需內容后,您將不再需要修改代碼。 您也可以將相同的模型用於幾個不同的JComboBox。

YourModel model = new YourModel("path_to_a_file");
JComboBox box1 = new JComboBox();
box1.setModel(model);
JComboBox box2 = new JComboBox();
box2.setModel(model);

填充組合框最簡單的方法是(如Java文檔所述):

String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
//Create the combo box, select item at index 4.
//Indices start at 0, so 4 specifies the pig.
JComboBox petList = new JComboBox(petStrings);

但是,這不是您可以尋求的最佳選擇。 用字符串數組填充組合框不是提供靈活性和模型/ UI解耦的最佳選擇。 這就是MVC模型起作用的地方。 MVC模型基本上告訴您使用模型(在您的情況下為ComboBoxModel )來備份數據。 擁有模型為您提供了從所需的任何位置(文件,套接字,Web服務...)獲取數據的可能性和靈活性。

使用自定義ComboBoxModel的另一種方法是使用JGoodies Binding將視圖綁定到視圖模型。 這樣做,您的視圖模型不包含任何特定於視圖的代碼,而是使用標准的Java Bean機制(例如,屬性更改支持)按需更新視圖,並且它通過Bean屬性自動接收所有視圖更新。 顯示的數據實際來自何處(在示例中,數據直接來自Java枚舉)與視圖實現無關。 這是一個例子:

class View {
    private JComboBox chatPresenceCombo = new JComboBox();

    public bind(ViewModel viewModel) {
        BeanAdapter<ViewModel> beanAdapter = new BeanAdapter<ViewModel>(viewModel, true);
        Bindings.bind(chatPresenceCombo, new SelectionInList<ChatPresence>(viewModel.getChatPresenceValues(),
                beanAdapter.getValueModel(ViewModel.PROPERTY_CHAT_PRESENCE)));
    }
}

class ViewModel
{
    private ChatPresence chatPresence;

    private final PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);

    public static final String PROPERTY_CHAT_PRESENCE = "chatPresence";

    public ChatPresence getChatPresence() {
        return chatPresence;
    }

    public void setChatPresence(ChatPresence chatPresence) {
        ChatPresence old = this.chatPresence;
        this.chatPresence = chatPresence;
        changeSupport.firePropertyChange(PROPERTY_CHAT_PRESENCE, old, chatPresence);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }

    public ChatPresence[] getChatPresenceValues() {
         return ChatPresence.values();
    }
}

public enum ChatPresence {
    //....
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM