如何使用arraylist将数据存储在JTable的预定义默认表模型中 [英] How I can use arraylist to store my data in predefined default table model for JTable

查看:87
本文介绍了如何使用arraylist将数据存储在JTable的预定义默认表模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时将数据添加到我自己的JTable模型中.我的界面上有表格和添加按钮.据我了解,我无法使用二维数组来做到这一点,并且想在定制表模型中使用Arraylist,但是如果使用此类数据,我不知道如何制作构造函数.

I want to add data to my own JTable model in runtime. I have the table and add button on my interface. As I understand I cant do it with two dimensional array and want to use Arraylist in my custom made table model but I dont know how to make the constructor if I use such type of data.

static String[] columnNames = {"A", "B", "C"};
static Object data[][] = new Object[15][3];

public MyTableModel() {
   super(data, columnNames);
}

这是作为二维数组的数据的构造函数,但我想使用:

this is the constructor for data as two dimansional array but I want to use:

ArrayList<Object[]> data = new ArrayList<Object[]>();

如何制作

推荐答案

一种方法是使用AbstractTableModel.这是一个示例:

One way to do it, is to use AbstractTableModel. Here is an example:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;

public class TestTable {

    public static class MyModel extends AbstractTableModel {

        private List<Object[]> data;
        private List<String> columnNames;

        public MyModel(List<String> columnNames, List<Object[]> data) {
            super();
            this.columnNames = columnNames;
            this.data = data;
        }

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

        @Override
        public int getColumnCount() {
            return columnNames.size();
        }

        @Override
        public String getColumnName(int column) {
            return columnNames.get(column);
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return data.get(rowIndex)[columnIndex];
        }

    }

    protected void initUI() {
        JFrame frame = new JFrame(TestTable.class.getSimpleName());
        List<String> columns = Arrays.asList("Name", "Age");
        List<Object[]> data = new ArrayList<Object[]>();
        for (int i = 0; i < 50; i++) {
            Object[] value = new Object[2];
            value[0] = "Name-" + i;
            value[1] = 12 + i;
            data.add(value);
        }
        JTable table = new JTable(new MyModel(columns, data));
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestTable().initUI();
            }
        });
    }
}

如果您需要修改data List<Object[]>,请不要忘记触发适当的表事件. AbstractTableModel已经包含所有这些内容.

If you need to to modify your data List<Object[]>, don't forget to fire appropriate Table Events. AbstractTableModel contains them all already.

这篇关于如何使用arraylist将数据存储在JTable的预定义默认表模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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