将对象列表连接到JTable [英] Connect a list of objects to a JTable

查看:77
本文介绍了将对象列表连接到JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java类型为SI(SingleInstruction)的对象的列表. 这是带有其构造函数的SI类.

I have a list of objects of type SI (SingleInstruction) in Java. Here is the SI class with its constructor.

    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中有一个表来显示此列表的数据. 当我开始程序时,我创建一个

I need to have a table in my GUI that displays the data of this list. When I begin the program I create a

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

这是空的,所以我需要显示一个空表. 该表应具有

which is empty so I need an empty table to be displayed. The table should have columns of

(字符串配方,整数顺序,字符串指令,两倍的估计时间, Double Timer,字符串TimerText,布尔包含其他指令)

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

之后,finalList中填充了数据,我需要将这些数据显示在表中. 为了给您提供示例,我创建了一个按钮,该按钮创建了一些SI并将其添加到finalList.

Afterwards the finalList is filled with data and I need these data to be displayed in the table. In order to give you an example, I created a button that creates some SI and adds them to the 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.

Now I need that when I press the button, the table is updated and displays the data. I've tried a lot to find out how to do it but it is too hard. I'm not sure if I should extend AbstractTableModel or just use the defaultTableModel.

为帮助您提供建议,此表的数据为最终数据,用户无法修改任何单元格.但是,应该允许用户执行的操作是向上或向下移动行以更改finalList中的顺序.

To help you give me suggestions, the data of this table is final and user can't modify any cell. what should be allowed to the user to do though is to move rows up or down to change the order in the finalList.

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

PS: I am using netbeans, if you have any quick suggestions on the GUI builder.

推荐答案

在模型-视图-控制器"体系结构中,模型始终负责提供数据.如果您为JTable使用模型,则模型将提供数据.

In Model-View-Controller Architecture the model is always responsible for the provision of data. If you use a model for your JTable the model provides the data.

为了创建模型,请创建一个单独的类,该类扩展AbstractTableModel并具有构造函数,该构造函数创建所需的所有SI对象并将其存储到ArrayList中.

In order to create a model make a separate class that extends AbstractTableModel and has a constructor that creates all the SI objects you need and stores them to an 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,如下所示:

Then make your JFrame and and create your JTable like this:

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

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

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

The idea is that JTable will autamatically request the data from the model, through call to getValueAt(row, col).

通过Netbeans,可以非常轻松地将模型分配给JTable.该模型是JTable的属性.点击"..."按钮,然后选择自定义代码".在下面创建一个模型实例.

Netbeans makes it very easy to assign the model to your JTable. The model is a property of the JTable. Click the '...' button and choose "Custom code". Underneath create a model instance.

这篇关于将对象列表连接到JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆