0

该程序应在JTable.

例如:我从 vollist.java 类中得到这个输出。

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

控制台输出:

vol1
vol2
vol3
...

如何在我的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 中的输出(向上计数)

4

2 回答 2

1

基本上,您需要相互调整两段代码......

DefaultTableModel model = new DefaultTableModel(new String[] {
    "Nr:", "Volume Name", "TotalSize [MB]", "Used [MB]", "Status"}, 0);

while (volumeIter.hasNext()) {
    volume = volumeIter.next();
    model.addRow(new Object[] {
        {null, vollist.volname(), null, null, null});
}

table = new JTable(model);

看看帽子如何使用表格了解更多详情

更新

一个更好的主意是允许TableModel实际“建模”提供的数据本身,例如......

public class FileSystemTabelModel extends AbstractTableModel {

    private static final String[] COLUMN_NAMES = new String[]{"Nr:", "Volume Name", "TotalSize [MB]", "Used [MB]", "Status"};
    private File[] roots;

    public FileSystemTabelModel() {
        roots = File.listRoots();
    }

    @Override
    public int getRowCount() {
        return roots.length;
    }

    @Override
    public int getColumnCount() {
        return COLUMN_NAMES.length;
    }

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

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        File root = roots[rowIndex];
        Object result = null;
        switch (columnIndex) {
            case 0:
                result = rowIndex;
                break;
            case 1:
                result = root.getName();
                break;
            case 3:
                result = root.getTotalSpace();
                break;
            case 4:
                result = root.getTotalSpace() - root.getFreeSpace();
                break;
            case 5:
                result = "All Good";
                break;
        }
        return result;
    }
}

那么你只需要...

table = new JTable(new FileSystemTabelModel());

这更多应该如何使用表模型 - 恕我直言

于 2013-09-05T21:15:45.817 回答
1

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

这更广泛,但是是 OOP 强类型。

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

//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的一些来源

于 2013-09-05T21:30:59.180 回答