2016-12-04 66 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