Create JTable from ArrayList of Objects - Java

自古美人都是妖i 提交于 2019-11-29 12:19:23

You could use DefaultTableModel, but in your case, that would mean you need to convert your data to confirm to it's needs, better to define a model that supports you data model...that's kinda the point

The following examples uses an AbstractTableModel, because it gives you control over the backing data, but takes care of most of the house keeping (registering and firing events).

The example is not mutable. That is, you can't add or delete new clicks or change existing clicks. It's not difficult to do and you should read through How to use tables for more details

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;

public class ClickTable {

    public static void main(String[] args) {
        new ClickTable();
    }

    public ClickTable() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                List<Click> clicks = new ArrayList<>(25);
                clicks.add(new Click(620, 1028));
                clicks.add(new Click(480, 230));
                ClickTableModel model = new ClickTableModel(clicks);
                JTable table = new JTable(model);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Click {

        private int x;
        private int y;

        public Click(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

    }

    public class ClickTableModel extends AbstractTableModel {

        private List<Click> clicks;

        public ClickTableModel(List<Click> clicks) {
            this.clicks = new ArrayList<>(clicks);
        }

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

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

        @Override
        public String getColumnName(int column) {
            String name = "??";
            switch (column) {
                case 0:
                    name = "Mouse X";
                    break;
                case 1:
                    name = "Mouse Y";
                    break;
            }
            return name;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            Class type = String.class;
            switch (columnIndex) {
                case 0:
                case 1:
                    type = Integer.class;
                    break;
            }
            return type;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Click click = clicks.get(rowIndex);
            Object value = null;
            switch (columnIndex) {
                case 0:
                    value = click.getX();
                    break;
                case 1:
                    value = click.getY();
                    break;
            }
            return value;
        }            
    }        
}

it makes more sense to keep them as Click objects as long as possible.

And there will probably be other cases when you want to display other custom Objects in a table as well. The Row Table Model provides generic support for an ArrayList of Objects. It provides support for dynamic functions like add and delete.

The JButtonTableModel.java gives and example of the code necessary to implement the full model for an Object.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!