java 从 ArrayList 创建 JTable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15956187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:26:23  来源:igfitidea点击:

Create JTable from ArrayList

javaswingarraylistjtableabstracttablemodel

提问by Alexandre Huard

Now that I managed to put the objects from a file into a ArrayList, I have to display them into a JTable.

现在我设法将文件中的对象ArrayList放入JTable.

These are the 3 objects contained in my ArrayList

这些是包含在我的 3 个对象中 ArrayList

Lieu<Double, String>(45.573715, -73.900295, "p1");
Lieu<Double, String>(45.573882, -73.899748, "p2");
Lieu<Double, String>(45.574438, -73.900099, "p3");

In the Lieuclass I have the methods getX()and getY()

Lieu课堂上我有方法getX()getY()

But I can't figure out how to diplay them in a JTable.

但我不知道如何在JTable.

Longitude           Latitude
45.573715           -73.900295
45.573882           -73.899748
45.574438           -73.900099

Here's what I have for a start:

这是我的开始:

public class MonModel extends AbstractTableModel{

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

    @Override
    public int getRowCount() {
        return l.size();//l is the arraylist that contains the 3 elements
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if(columnIndex==0){
            return l.get(rowIndex).getX();
        }
        else if(columnIndex==1){
            return l.get(rowIndex).getY();
        }
        return null;
    }

回答by trashgod

Use your TableModelto create a JTableand add it to a JFrame. Also consider overriding getColumnName(), as shown here. See also How to Use Tables.

使用您TableModel创建一个JTable并将其添加到JFrame. 也可以考虑覆盖getColumnName(),如图所示这里。另请参阅如何使用表格

MonModel model = new MonModel();
JTable table = new JTable(model);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(table), BorderLayout.CENTER);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);

回答by Amarnath

Use a TableModelfor showing data in the JTable. For Example:

使用 aTableModel显示JTable. 例如:

In UI class, set the table model to the table.

在 UI 类中,将表模型设置为表。

JTable table = new JTable(new MonModel());

Table Model class

表模型类

class MonModel extends AbstractTableModel {

    private List<LatNLon> l;
    private String[] columnNames = {"Longitude", "Latitude"};

    public MonModel() {
        l = new ArrayList<LatNLon>();

        l.add(new LatNLon("45.573715", "-73.900295"));
        l.add(new LatNLon("45.573715", "-73.900295"));
        l.add(new LatNLon("45.573715", "-73.900295"));
    }

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

    public int getColumnCount() {
        return 2;
    }

    public int getRowCount() {
        return l.size();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        if(columnIndex==0){
            return l.get(rowIndex).getX();
        }
        else if(columnIndex==1){
            return l.get(rowIndex).getY();
        }
        return null;
    }
}

Latitude and Longitude class.

纬度和经度类。

class LatNLon {
    private String x;
    private String y;

    public LatNLon(String x, String y) {
        this.x = x;
        this.y = y;
    }
// Code: For Getters and Setters.
}

Also read How to use Tables.

另请阅读如何使用表格