Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

swing - Create JTable from ArrayList of Objects - Java

How do I display my "Click" objects in a JTable?

ArrayList<Click> myClicks = new ArrayList<Click>();

Click click = new Click(620, 1028);
Click click2 = new Click(480, 230);

myClicks.add(click);
myClicks.add(click2);

It should look something like this:

|---Mouse X---|---Mouse Y---|
|     620     |     1028    |
|     480     |      230    |

Which is really...

       |              |
       v              v
  click.getX()  click.getY()
  click2.getX() click2.getY()

I don't want to use a 2D Object[][] because it makes more sense to keep them as Click objects as long as possible.

I know I probably have to extend the TableModel interface but I'm not sure how.

I'd just like a quick and simple example please (the java docs are a little confusing).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here

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;
        }            
    }        
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...