使用ArrayList在java中填写一个JTable [英] Use arraylist to fill a JTable in java
问题描述
我一直在阅读教程,我现在还不能确定,所以我会问这里。是否有可能有两列JTable中,其中一个ArrayList的第1列显示的内容和列2显示数字高达ArrayList的大小?我已经看了看教程,但他们并不真正提的ArrayList,所以任何样品codeS或方向将是有益的。谢谢
I have been reading tutorials and I am still not sure so I will ask here. Is it possible to have a JTable with 2 columns where column 1 displays contents of an arraylist and column 2 displays numbers up to the size of the arraylist?. I have looked at tutorials but they dont really mention arraylists, so any sample codes or direction would be helpful. Thanks
推荐答案
是的,当然这是可能的。通过延长 AsbtractTableModel
实施一个表模型,并使用该列表来实现它的方法。
Yes, of course it's possible. Implement a table model by extending AsbtractTableModel
, and use the list to implement its methods.
阅读 JTable的教程。
表模型的主要方法是这样的:
The main methods of the table model would look like this:
public Object getValueAt(int row, int column) {
if (column == 0) {
return this.list.get(row);
}
else {
return row;
}
}
public int getRowCount() {
return this.list.size();
}
public int getColumnCount() {
return 2;
}
这篇关于使用ArrayList在java中填写一个JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!