【问题标题】:Populating JComboBox using an MultiDimensional Array List使用多维数组列表填充 JComboBox
【发布时间】:2023-03-17 09:16:01
【问题描述】:

我一直在尝试使用构造函数使用数组列表填充 Eclipse GUI Java JComboBox,但没有任何运气。这是我迄今为止尝试过的。

import item.Item;  
import javax.swing.JComboBox;
import java.util.ArrayList;  


public class SelectionScreen{
    private JFrame frame;
    static ArrayList< Item> list;
    private String items;

     public static void main (String[] args){  

        initialize();
     }
     public void initialize(){
        list = new ArrayList< Item >();  
        list.add(new Item("Strawberry,200,.25,.75);
        list.add(new Item("Banana,200,.25,1.00);  
        list.add(new Item("Oranges,200,.25,2.00);


       JcomboBox comboBox = newJcomboBox();
       ComboBox.setBounds(63,29,86,22)
       frame.getContentPane().add(comboBox); 

     // here is where I tried to fill the combobox

     //comboBox.setModel(new DefaultComboBoxModel(Item.getName()))); //Wrong
     //comboBox.setModel(Item.getName); //Wrong

  //the following only loads the last item in the list which is Oranges
     for(Item i: list{

      comboBox.setModel(new DefaultComboBoxModel(New String[] { 
      i.getName()})); 
      }

   // tried making a different list to collect my fruits.
     for(Item i: list){
     list2[ i.getName()];
     Item.length;
     } //which was a complete fail.

我完全迷路了,对 Java 的经验也不是很丰富。我可以很好地使用 comboBox.setModel(new DefaultComboBoxModel(new String[]{ "Strawberry","Banana","Oranges"}));
但是当我从文本文件中导入它们时,我不会知道列表中有哪些水果。

任何帮助将不胜感激。

【问题讨论】:

    标签: java swing arraylist jcombobox


    【解决方案1】:
    /*The following only loads the last item in the list which is Oranges.*/
    
    for(Item i: list)
    {
          comboBox.setModel(new DefaultComboBoxModel(new String[] { 
          i.getName()})); 
    }
    

    不要在循环内不断创建新的 ComboBoxModel。如果您继续创建新模型,则不能向模型添加多个项目。因此,您只会看到添加了单个项目的最后一个模型。如果您想使用这种方法,那么您将在循环外创建模型,然后在循环内添加模型项。

    实际上您不需要创建组合框模型。您可以直接将项目添加到组合框:

    类似:

    for(Item i: list
    {
          comboBox.addItem( i.getName() );
    }
    

    另一种选择是将 Item 对象直接添加到组合框中。然后,您可以使用自定义渲染器来控制 Item 对象的哪个属性显示在组合框中。查看Combo Box With Custom Renderer 了解有关此方法的更多信息。

    【讨论】:

    • 完美工作非常感谢。
    【解决方案2】:

    如果您希望在组合框中显示Item 对象,则应声明JComboBox 以存储Item 对象。这样您就可以轻松地添加项目,而无需对模型进行任何处理:

    JComboBox<Item> itemsCombo = new JComboBox<>();
    list.forEach(itemsCombo::add);
    

    组合框中显示的值将是Item.toString 返回的值。如果这不是您想要的(因为您的toString 返回了对对象的更完整描述——通常被认为是更好的做法),那么编写自己的Custom Renderer 相当容易。

    JComboBox API 的唯一缺点是您必须强制转换所选项目:

    Item selectedItem = (Item)itemsCombo.getSelectedItem();
    

    这很难看,我希望 API 不需要它,但为了避免必须定义自己的模型,这是一个很小的代价。

    实际上,您可以通过以下方式避免演员:

    Item selectedItem = itemsCombo.getItemAt(itemsCombo.getSelectedIndex());
    

    但这也很丑。

    顺便说一句,这是标准 Java 教程和示例已经过时的几个领域之一,因此完全不应该责怪不知道使用泛型。

    【讨论】:

    • The value displayed in the combobox will be whatever is returned by Item.toString. - 你不应该依赖 toString() 实现。通常 toString() 将为对象的所有属性显示一个字符串,而 OP 只想显示名称。
    • it is fairly easy to write your own Custom Renderer. - 仅供参考,您仍然需要小心使用自定义渲染器,因为这会破坏组合框的一些默认功能。查看我的问题答案中提供的链接(和潜在的解决方案)。
    猜你喜欢
    • 1970-01-01
    • 2014-11-20
    • 2023-02-11
    • 2012-11-20
    • 2016-02-18
    • 2011-01-11
    • 1970-01-01
    相关资源
    最近更新 更多