2016-12-04 70 views
0

這段代碼是草率的,我也歡迎一些反饋意見。基於另一個JComboBox的內容的動態JComboBox內容

我試圖根據另一個JComboBox的值更改JComboBox的值。還有一個額外的複雜因素,我使用一個額外的類來確定返回的字符串數組(請參閱我的上一個問題)。

從理論上講,我的代碼應該工作:

String[] siteSelectStrings = {"Site", "London", "Long Island"}; 
    JComboBox regSiteSelectBox = new JComboBox(siteSelectStrings); 
    regSiteSelectBox.addItemListener(new ItemListener() { 
     public void itemStateChanged(ItemEvent arg0) { 
      getBuildingList gbl = new getBuildingList(); 
      regBuildingSelectBox.addItem(gbl.buildingSelectList((String)(regSiteSelectBox.getSelectedItem()))); 
      } 
     }); 
    regSiteSelectBox.setBounds(24, 336, 282, 20); 
    contentPane.add(regSiteSelectBox); 


    regBuildingSelectBox = new JComboBox(); 
    regBuildingSelectBox.setBounds(24, 367, 282, 20); 
    contentPane.add(regBuildingSelectBox); 

以及用於返回建築陣列的方法:

public class getBuildingList { 

public String[] buildingSelectList(String site) 
{ 
    switch (site) 
    { 
    case "London": 
     return new String[] {"Building", "Harvell", "LYNX Complex", "Caroline", "Salters"}; 
    case "Long Island": 
     return new String[] {"Building", "Phillips", "Pascal"}; 
    } 
    return new String[] {"Failed to populate buildings"}; 
    } 
} 

但是,而不是返回一個清晰的字符串,它返回以下:

[Ljava.lang.String;@917081d

我不知道如何解碼,雖然它似乎是一個內存參考。我哪裏錯了?

+0

爲了讓我們來幫助你,你有,因爲它似乎這是哪裏的東西得到弄糟的部分爲我們提供了'getBuildingList.buildingSelectList(字符串ARG)'方法...你應該真的習慣用大寫字母開始類名,否則它會讓別人讀取你的代碼變得非常混亂 – Raven

+0

@Raven我傾向於用[LC] [UC +]的形式命名類,無論它們在哪裏用作子對象而不是主類 - 有一種支持這種方法的方法,儘管它不是基於Java的。我在技術上是一個C#程序員... – Wolfish

+0

好吧然後...但我們仍然需要這種方法;) – Raven

回答

1

好吧,據我可以看到它的問題是,您將添加完整的字符串數組作爲一個項目。然後JCombobox通過調用toString() mehtod將其轉換爲單個字符串,從而使其顯示[Ljava.lang.String;@917081d
爲了讓您的陣列的內容被顯示爲JComboBox單個條目,你必須清除它,然後seperately添加每個項目:

regBuildingSelectBox.removeAllItems(); 

for(String currentEntry : gbl.buildingSelectList((String)(regSiteSelectBox.getSelectedItem())) { 
     regBuildingSelectBox.addItem(currentEntry); 
} 

而當你要求你的代碼的其他反饋.. 。我建議使用枚舉而不是硬編碼的字符串數組。這只是一個潛在的錯誤源

2

如果您的方法正在返回要顯示在組合框中的字符串數組,則需要創建一個新的ComboBoxModel以添加到組合框中。

例如:

import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import javax.swing.*; 
import javax.swing.plaf.basic.*; 

public class ComboBoxTwo extends JPanel implements ActionListener 
{ 
    private JComboBox<String> mainComboBox; 
    private JComboBox<String> subComboBox; 
    private Hashtable<String, String[]> subItems = new Hashtable<String, String[]>(); 

    public ComboBoxTwo() 
    { 
     String[] items = { "Select Item", "Color", "Shape", "Fruit" }; 
     mainComboBox = new JComboBox<String>(items); 
     mainComboBox.addActionListener(this); 

     // prevent action events from being fired when the up/down arrow keys are used 
     mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE); 
     add(mainComboBox); 

     // Create sub combo box with multiple models 

     subComboBox = new JComboBox<String>(); 
     subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4 
     add(subComboBox); 

     JButton arrow = SwingUtils.getDescendantOfType(JButton.class, subComboBox, "Text", ""); 
     Dimension d = arrow.getPreferredSize(); 
     System.out.println(arrow.getClass()); 
     System.out.println(d); 
     d.width = 100; 
     arrow.setPreferredSize(d); 

     String[] subItems1 = { "Select Color", "Red", "Blue", "Green" }; 
     subItems.put(items[1], subItems1); 

     String[] subItems2 = { "Select Shape", "Circle", "Square", "Triangle" }; 
     subItems.put(items[2], subItems2); 

     String[] subItems3 = { "Select Fruit", "Apple", "Orange", "Banana" }; 
     subItems.put(items[3], subItems3); 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     String item = (String)mainComboBox.getSelectedItem(); 
     Object o = subItems.get(item); 

     if (o == null) 
     { 
      subComboBox.setModel(new DefaultComboBoxModel()); 
     } 
     else 
     { 
      subComboBox.setModel(new DefaultComboBoxModel((String[])o)); 
     } 
    } 

    private static void createAndShowUI() 
    { 
     try 
     { 
//   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } 
     catch (Exception e) { } 
     JFrame frame = new JFrame("SSCCE"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new ComboBoxTwo()); 
     frame.setLocationByPlatform(true); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowUI(); 
      } 
     }); 
    } 
} 
+0

正在重建完全必要的模型嗎?我試着迭代數組中的項目,除了它通過數組遍歷兩次之外,這個工作除外。這感覺非常複雜。爲什麼有必要? – Wolfish

+0

@Wolfish,重新創建模型讓解決方案非常簡單。每組數據都有自己的模型。數據可能來自任何地方。它可以被硬編碼。它可能來自數據庫。如果可能來自平面文件。該解決方案非常可重用。 – camickr