java - 将对象列表连接到 JTable

标签 java swing list

我在 Java 中有一个 SI (SingleInstruction) 类型的对象列表。 这是 SI 类及其构造函数。

    public class SI {
        private String recipe;
        private Integer makingOrder;
        private String instruction;
        private double estimatedTimeInMinutesSeconds;
        private Double timerInMinutesSeconds;
        private String timerEndingText;
        private boolean containsOtherInstructions = false;
        private List<SI> embeddedInstructionsList;

        public SI (String s1, Integer i1, String s2, double d1, Double d2, String s3){
            recipe = s1;
            makingOrder = i1;
            instruction = s2;
            estimatedTimeInMinutesSeconds = d1;
            timerInMinutesSeconds = d2;
            timerEndingText = s3;
        }
setters and getters methods ommited....

我需要在我的 GUI 中有一个表格来显示这个列表的数据。 当我开始这个程序时,我创建了一个

public List<SI> finalList = new ArrayList();

它是空的,所以我需要显示一个空表。 该表应该有列

(String Recipe, Integer Order, String Instruction, double Est.time, Double Timer, String TimerText, boolean containOtherInstructions)

之后 finalList 填充了数据,我需要将这些数据显示在表格中。 为了给你举个例子,我创建了一个按钮,它创建了一些 SI 并将它们添加到 finalList。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    SI si1 = new SI("Greek Salad", 1, "cut veggies", 4, null, null);
    SI si2 = new SI("Greek Salad", 2, "put spices", 1, null, null);
    SI si3 = new SI("Greek Salad", 3, "feta and finish", 1, null, null);
    SI si4 = new SI("Break", null, "Your free 10 minutes", 10, null, null);
    SI si5 = new SI("Meat", 1, "preheat oven", 0.5, 10.0, "oven is ready");
    SI si6 = new SI("Meat", 2, "spices on meat", 2, null, null);
    SI si7 = new SI("Meat", 3, "cook meat", 1, 10.0, "meat is ready");
    SI si8 = new SI("Meat", 4, "serve", 1, null, null);
    SI si9 = new SI("Break", null, "Your free 10 minutes", 10, null, null);
    SI si10 = new SI("Jelly", 1, "Boil water", 0.5, 4.0, "water is ready");
    SI si11 = new SI("Jelly", 2, "add mix and stir", 4, null, null);
    SI si12 = new SI("Jelly", 3, "share in bowls, wait 3 hours", 2, null, null);
    finalList.add(si1);
    finalList.add(si2);
    finalList.add(si3);
    finalList.add(si4);
    finalList.add(si5);
    finalList.add(si6);
    finalList.add(si7);
    finalList.add(si8);
    finalList.add(si9);
    finalList.add(si10);
    finalList.add(si11);
    finalList.add(si12);
}

现在我需要在按下按钮时更新表格并显示数据。 我已经尝试了很多来找出如何去做,但它太难了。我不确定是应该扩展 AbstractTableModel 还是只使用 defaultTableModel。

为了帮助您给我建议,此表的数据是最终数据,用户无法修改任何单元格。应该允许用户做的是向上或向下移动行以更改 finalList 中的顺序。

PS:如果您对 GUI 构建器有任何快速建议,我正在使用 netbeans。

最佳答案

在模型- View - Controller 架构中,模型始终负责提供数据。如果您为 JTable 使用模型,则该模型会提供数据。

为了创建一个模型,创建一个单独的类来扩展 AbstractTableModel 并具有一个构造函数来创建您需要的所有 SI 对象并将它们存储到 ArrayList。

 public class FinalTableModel extends AbstractTableModel {

    private List<SI> li = new ArrayList();
    private String[] columnNames = { "Recipe", "Order", "Instruction",
                "Est.time", "Timer", "Timer Label", "Has Subinstructions"};

    public FinalTableModel(List<SI> list){
         this.li = list;
    }

    @Override
    public String getColumnName(int columnIndex){
         return columnNames[columnIndex];
    }

    @Override     
    public int getRowCount() {
        return li.size();
    }

    @Override        
    public int getColumnCount() {
        return 7; 
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        SI si = list.get(rowIndex);
        switch (columnIndex) {
            case 0: 
                return si.getRecipe();
            case 1:
                return si.getMakingOrder();
            case 2:
                return si.getInstruction();
            case 3:
                return si.getEstimatedTimeInMinutesSeconds();
            case 4:
                return si.getTimerInMinutesSeconds();
            case 5:
                return si.getTimerEndingText();
            case 6:
                return si.isContainsOtherInstructions(); 
           }
           return null;
   }

   @Override
   public Class<?> getColumnClass(int columnIndex){
          switch (columnIndex){
             case 0:
               return String.class;
             case 1:
               return Integer.class;
             case 2:
               return String.class;
             case 3:
               return Double.class;
             case 4:
               return Double.class;
             case 5:
               return String.class;
             case 6:
               return Boolean.class;
             }
             return null;
      }
 }

然后像这样创建 JFrame 和 JTable:

    jScrollPane1 = new javax.swing.JScrollPane();
    jTable1 = new javax.swing.JTable();

    jTable1.setModel(new FinalTableModel(finalList));
    jScrollPane1.setViewportView(jTable1);

想法是 JTable 将通过调用 getValueAt(row, col) 自动从模型请求数据。

Netbeans 使将模型分配给 JTable 变得非常容易。该模型是 JTable 的一个属性。单击“...”按钮并选择“自定义代码”。在下面创建一个模型实例。

enter image description here

关于java - 将对象列表连接到 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7378013/

相关文章:

java - 当 JLabel 包含 HTML 时,BoxLayout 不遵守胶水

python - 在列表中查找平均值,并在 Python 中返回结果分数超过平均值

python - 在 Python 中将短划线分隔的字符串数据转换为嵌套字典/列表

java - 接口(interface)如何在其签名或返回类型中包含引用接口(interface)具体实现类型的方法?

java - 如何通过二分搜索搜索 ArrayList 中的任何值

java - 如何指示表中的列是可排序的(UI 设计问题)?

java - 在另一个 JLabel 前面显示一个 JLabel

list - 可以将宏应用于参数列表吗?

java - 在java中生成 vector 图形并在windows中使用/打开/打印的良好格式?

java - 如何从 ASP .NET 网站检测安装在客户端上的 Java 运行时?