Java:JTable中的控制台输出 [英] Java: Console output in JTable

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

问题描述

程序应该在 JTable 中列出卷。

例如: .java类。

For Example: I get this output form the vollist.java class.

while (volumeIter.hasNext()) {
    volume = volumeIter.next();
    System.out.println(volume.getName());
}

控制台输出:

vol1
vol2
vol3
...

如何在我的 JTable 中获得此控制台输出。

How can I get this console output in my JTable.

table = new JTable();
table.setModel(new DefaultTableModel(
    new Object[][] {
        {null, vollist.volname(null), null, null, null},
        {null, vollist.volname(null), null, null, null},
        {null, vollist.volname(null), null, null, null},
    },
    new String[] {
        "Nr:", "Volume Name", "TotalSize [MB]", "Used [MB]", "Status"
    }
));

只显示row1 - > vol1 row2 - > vol1 ......如何控制台中的输出row1 - > vol1 row2 - > vol2(count up)

That only displays row1 -> vol1 row2 -> vol1 ...... How can i get an output like in the console row1 -> vol1 row2 -> vol2 (count up)

推荐答案

定义和实现您的TableModel这种情况下扩展AbstractTableModel)

Define and implement your TableModel (in this case extending AbstractTableModel)

这是更广泛的,但是是强大的类型。

class VolumeTableModel extends AbstractTableModel {
    private String[] columnNames = {"Nr:", "Volume Name", "TotalSize [MB]", "Used [MB]", "Status"};
    private ArrayList<Volume> volumes;

    public VolumeTableModel(ArrayList<Volume> volumes) {
        this.volumes = volumes;
    }

    public VolumeTableModel() {
        volumes = new ArrayList<Volume>();
    }

    public void addVolume(Volume volume) {
        volumes.add(volume);
        fireTableRowsInserted(volumes.size()-1, volumes.size()-1);
    }

    public int getColumnCount() {
        return columnNames.length;
    }

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

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        Volume volume = volumes.get(row);
        switch (col) {
            case 0: return volume.number;
            case 1: return volume.name;
            case 2: return volume.totalSize;
            case 3: return volume.usedSize;
            case 4: return volume.status;
            default: return null;
        }
    }

    public Class getColumnClass(int col) {
        return String.class;
        //or just as example
        switch (col) {
            case 0: return Integer.class;
            case 1: return String.class;
            case 2: return Integer.class;
            case 3: return Integer.class;
            case 4: return String.class;
            default: return String.class;
        }
    }
}

并指定为TableModel for your table

and specify that as the TableModel for your table

//if you have the Volume ArrayList
VolumeTableModel myTableModel = new VolumeTableModel(volumesArrayList);
//if you dont have the Volume ArrayList
VolumeTableModel myTableModel = new VolumeTableModel();
myTableModel.addVolume(volume);
JTable table = new JTable(myTableModel);

来自 http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

这篇关于Java:JTable中的控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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