Java:JTable中的控制台输出

程序应在中列出卷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的输出(计数)

回答:

定义并实现您的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的某些来源

以上是 Java:JTable中的控制台输出 的全部内容, 来源链接: utcz.com/qa/413408.html

回到顶部