简体   繁体   English

如何在Java的JList中显示数组列表?

[英]how to show an array list in a JList in Java?

I have an arraylist and want to show it on Jlist using gui, I am using drag and drop to make the form, so I need to fill the list dynamically. 我有一个arraylist,想使用gui在Jlist上显示它,我正在使用拖放操作来制作表单,因此我需要动态填充列表。 I tried to get into the properties of the Jlist and make the model is a custom code but the problem which happened that the array list takes its elements from a text file, that means when I want to fill the array list I should call a method one of its parameters is a name of file. 我试图进入Jlist的属性并使模型成为自定义代码,但是发生的问题是数组列表从文本文件中获取其元素,这意味着当我要填充数组列表时,我应该调用一个方法它的参数之一是文件名。 When using this method try and catch shohld be used but when I set the model of the Jlist it minds use it! 当使用这种方法时,尝试使用catch捕获,但是当我设置Jlist的模型时,会想到使用它! What should I do to make my array list shown on the form as a list? 如何使数组列表以列表形式显示在表单上?

    public static ArrayList<Beverage> readBeverageTextFile(String fileName) throws FileNotFoundException, IOException {
    ArrayList<Beverage> bevList = new ArrayList<>();
    Reader reader = new FileReader(fileName);
    BufferedReader buf = new BufferedReader(reader);
    String line;
    while (true) {

        line = buf.readLine();
        if (line == null) {
            break;
        }

        try {
            StringTokenizer tokenizer = new StringTokenizer(line, "|");

            String name = tokenizer.nextToken();
            //System.out.println("The name is " + name);
            String desc = tokenizer.nextToken();
            //System.out.println("The dest " + desc);
            double price = Double.parseDouble(tokenizer.nextToken());
            //System.out.println("The Price is " + price);
            double quantity = Double.parseDouble(tokenizer.nextToken());
            //System.out.println("The Quantity is " + quantity);

            double ex = Double.parseDouble(tokenizer.nextToken());
            double pac = Double.parseDouble(tokenizer.nextToken());
            double capacity = Double.parseDouble(tokenizer.nextToken());
            //System.out.println("The Capacity is  " + capacity);
            bevList.add(new Beverage(capacity, name, desc, quantity, price, ex, pac));

        } catch (NumberFormatException ex) {
            Logger.getLogger(Beverage.class.getName()).log(Level.WARNING, "error");
        }
    }
    return bevList;
}

If you are only using the ArrayList to read data into and then display it in a JList, then don't use an ArrayList. 如果仅使用ArrayList读取数据,然后将其显示在JList中,则不要使用ArrayList。 Instead use a DefaultListModel , fill it with your file's data by calling addElement(...) on it, and then set the JList's model via setModel(...) with your data-filled model. 而是使用DefaultListModel ,通过在其上调用addElement(...)将其数据填充到文件中,然后通过setModel(...)将JList的模型设置为数据填充的模型。

If on the other hand you absolutely must use an ArrayList to hold your data, then I see you having two possible options here: 另一方面,如果您绝对必须使用ArrayList来保存数据,那么我在这里看到两个可能的选择:

  • Use your ArrayList, but also create a DefaultListModel, and fill the list model with the ArrayList data using a for loop. 使用ArrayList,还要创建一个DefaultListModel,并使用for循环用ArrayList数据填充列表模型。 The down-side, your data will be held in two collections, and there's no guarantee that the data will remain fully synchronized. 不利的一面是,您的数据将保存在两个集合中,并且不能保证数据会保持完全同步。
  • Create an AbstractListModel using your ArrayList as a data nucleus. 使用ArrayList作为数据核来创建AbstractListModel。 This would be the cleaner way to do this, but will require more effort on your part, including making sure that you call the proper fireXXX(...) notification methods for instance. 这将是更干净的方法,但是您需要付出更多的努力,包括确保例如调用正确的fireXXX(...)通知方法。

For more details, please see the How to use Lists tutorial. 有关更多详细信息,请参见“ 如何使用列表”教程。


Edit Regarding your code, @again, in place of an ArrayList, use a DefaultListModel. 编辑关于您的代码,再次使用@DefaultListModel代替ArrayList。 Key also will be how your objects display in the JList, and to get this right, you'll need to either give the Beverage class a decent toString() method, or else (and better) you should give the JList a decent ListCellRenderer. 关键还在于您的对象在JList中的显示方式,要正确实现这一点,您需要为Beverage类提供一个体面的toString()方法,或者(更好的是)为JList提供一个体面的ListCellRenderer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM