从ArrayList中创建的JTable [英] Create JTable from ArrayList

查看:175
本文介绍了从ArrayList中创建的JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我总算把对象从文件到的ArrayList ,我必须将它们显示成的JTable

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

 寮<双,字符串>(45.573715,-73.900295,P1);
代替<双,字符串>(45.573882,-73.899748,P2);
代替<双,字符串>(45.574438,-73.900099,P3);

类我有方法的getX()的getY()

但我无法弄清楚如何diplay他们在一个的JTable

 经度纬度
45.573715 -73.900295
45.573882 -73.899748
45.574438 -73.900099

下面就是我有一个启动:

 公共类MonModel扩展AbstractTableModel上{    @覆盖
    公众诠释getColumnCount(){
        返回2;
    }    @覆盖
    公众诠释getRowCount(){
        返回l.size(); // l为包含3个元素的数组列表
    }    @覆盖
    公共对象getValueAt(INT的rowIndex,INT参数:columnIndex){
        如果(参数:columnIndex == 0){
            返回l.get(rowIndex位置).getX();
        }
        否则如果(参数:columnIndex == 1){
            返回l.get(rowIndex位置).getY();
        }
        返回null;
    }


解决方案

使用的TableModel 用于显示数据的的JTable 。例如:

在UI类,表格模型设置为表。

  JTable的表=新的JTable(新MonModel());

表Model类

 类MonModel扩展AbstractTableModel上{    私人列表< LatNLon>升;
    私有String [] = COLUMNNAMES {经度,纬度};    公共MonModel(){
        L =新的ArrayList< LatNLon>();        l.add(新LatNLon(45.573715,-73.900295));
        l.add(新LatNLon(45.573715,-73.900295));
        l.add(新LatNLon(45.573715,-73.900295));
    }    @覆盖
    公共字符串getColumnName(INT列){
        返回COLUMNNAMES [专栏];
    }    公众诠释getColumnCount(){
        返回2;
    }    公众诠释getRowCount(){
        返回l.size();
    }    公共对象getValueAt(INT的rowIndex,INT参数:columnIndex){
        如果(参数:columnIndex == 0){
            返回l.get(rowIndex位置).getX();
        }
        否则如果(参数:columnIndex == 1){
            返回l.get(rowIndex位置).getY();
        }
        返回null;
    }
}

纬度和经度类。

 类LatNLon {
    私人字符串x;
    私人y字符串;    公共LatNLon(字符串x,y字符串){
        this.x = X;
        this.y = Y;
    }
// code:对于getter和setter。
}

另请阅读如何使用表的。

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

These are the 3 objects contained in my 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 Lieu class I have the methods getX() and getY()

But I can't figure out how to diplay them in a 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;
    }

解决方案

Use a TableModel for showing data in the JTable. For Example:

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

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.

这篇关于从ArrayList中创建的JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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