java 从 ArrayList 创建 JTable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15956187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create JTable from ArrayList
提问by Alexandre Huard
Now that I managed to put the objects from a file into a ArrayList
, I have to display them into a JTable
.
现在我设法将文件中的对象ArrayList
放入JTable
.
These are the 3 objects contained in my ArrayList
这些是包含在我的 3 个对象中 ArrayList
Lieu<Double, String>(45.573715, -73.900295, "p1");
Lieu<Double, String>(45.573882, -73.899748, "p2");
Lieu<Double, String>(45.574438, -73.900099, "p3");
In the Lieu
class I have the methods getX()
and getY()
在Lieu
课堂上我有方法getX()
和getY()
But I can't figure out how to diplay them in a JTable
.
但我不知道如何在JTable
.
Longitude Latitude
45.573715 -73.900295
45.573882 -73.899748
45.574438 -73.900099
Here's what I have for a start:
这是我的开始:
public class MonModel extends AbstractTableModel{
@Override
public int getColumnCount() {
return 2;
}
@Override
public int getRowCount() {
return l.size();//l is the arraylist that contains the 3 elements
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if(columnIndex==0){
return l.get(rowIndex).getX();
}
else if(columnIndex==1){
return l.get(rowIndex).getY();
}
return null;
}
回答by trashgod
Use your TableModel
to create a JTable
and add it to a JFrame
. Also consider overriding getColumnName()
, as shown here. See also How to Use Tables.
使用您TableModel
创建一个JTable
并将其添加到JFrame
. 也可以考虑覆盖getColumnName()
,如图所示这里。另请参阅如何使用表格。
MonModel model = new MonModel();
JTable table = new JTable(model);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(table), BorderLayout.CENTER);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
回答by Amarnath
Use a TableModel
for showing data in the JTable
. For Example:
使用 aTableModel
显示JTable
. 例如:
In UI class, set the table model to the table.
在 UI 类中,将表模型设置为表。
JTable table = new JTable(new MonModel());
Table Model class
表模型类
class MonModel extends AbstractTableModel {
private List<LatNLon> l;
private String[] columnNames = {"Longitude", "Latitude"};
public MonModel() {
l = new ArrayList<LatNLon>();
l.add(new LatNLon("45.573715", "-73.900295"));
l.add(new LatNLon("45.573715", "-73.900295"));
l.add(new LatNLon("45.573715", "-73.900295"));
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
public int getColumnCount() {
return 2;
}
public int getRowCount() {
return l.size();
}
public Object getValueAt(int rowIndex, int columnIndex) {
if(columnIndex==0){
return l.get(rowIndex).getX();
}
else if(columnIndex==1){
return l.get(rowIndex).getY();
}
return null;
}
}
Latitude and Longitude class.
纬度和经度类。
class LatNLon {
private String x;
private String y;
public LatNLon(String x, String y) {
this.x = x;
this.y = y;
}
// Code: For Getters and Setters.
}
Also read How to use Tables.
另请阅读如何使用表格。