Java: Console output in JTable

2019-03-05 07:05发布

The program should list Volumes in a JTable.

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

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

Console Output:

vol1
vol2
vol3
...

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

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

2条回答
别忘想泡老子
2楼-- · 2019-03-05 07:23

Basically, you need to adapt the two pieces of code with each other...

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

Take a look hat How to use tables for more details

Updated

A better idea would be to allow the TableModel to actually "model" the supplied data iteself, for example...

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

Then all you would need to is...

table = new JTable(new FileSystemTabelModel());

This more how a table model should be used - IMHO

查看更多
Animai°情兽
3楼-- · 2019-03-05 07:35

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

This is more extensive but is OOP strong typed.

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

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

Some source from http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

查看更多
登录 后发表回答