Java JTable由ArrayList填充,结果不好
我必须使用Java JTable由ArrayList填充,结果不好,java,swing,arraylist,jtable,tablemodel,Java,Swing,Arraylist,Jtable,Tablemodel,我必须使用ArrayList填充JTable,但是当该表显示时,它将填充除正确信息之外的其他信息。单击下一个链接可以查看我的表的外观 gui1是主类的名称,theObject是包含要在表上显示的信息结构的类的名称 请帮我解决这个问题 ArrayList定义: static ArrayList<theObject> datos = new ArrayList<theObject>(); 该表由以下行填充: tabla = new JTable(new MyModel(
ArrayList
填充JTable
,但是当该表显示时,它将填充除正确信息之外的其他信息。单击下一个链接可以查看我的表的外观
gui1
是主类的名称,theObject
是包含要在表上显示的信息结构的类的名称
请帮我解决这个问题
ArrayList
定义:
static ArrayList<theObject> datos = new ArrayList<theObject>();
该表由以下行填充:
tabla = new JTable(new MyModel(datos));
现在是代码中涉及的类:
class theObject {
String sap,cte,pep,dis1,dis2,norma,nvent,ntc,tmando,tparalel,equip;
theObject(String dato[]) {
this.sap = dato[0];
this.cte = dato[1];
this.pep = dato[2];
this.dis1 = dato[3];
this.dis2 = dato[4];
this.norma = dato[5];
this.nvent = dato[6];
this.ntc = dato[7];
this.tmando = dato[8];
this.tparalel = dato[9];
this.equip = dato[10];
}
}
class MyModel extends AbstractTableModel {
private String[] columnNames = { "FERT","CLIENTE ","PEP","A","B","C","D","E","F","G","H" };
ArrayList<theObject> arr1 = null;
MyModel(ArrayList<theObject> arr1) {
this.arr1 = arr1;
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return pru2.datos.size();
}
public Object getValueAt(int row, int col) {
theObject a1 = datos.get(row);
return a1;
}
}
对对象进行分类{
串sap、cte、pep、dis1、dis2、norma、nvent、ntc、tmando、tparalel、设备;
对象(字符串dato[]){
this.sap=dato[0];
this.cte=dato[1];
this.pep=dato[2];
this.dis1=dato[3];
this.dis2=dato[4];
this.norma=dato[5];
this.nvent=dato[6];
this.ntc=dato[7];
this.tmando=dato[8];
this.tparalel=dato[9];
此参数=dato[10];
}
}
类MyModel扩展了AbstractTableModel{
私有字符串[]列名={“FERT”、“CLIENTE”、“PEP”、“A”、“B”、“C”、“D”、“E”、“F”、“G”、“H”};
ArrayList arr1=null;
MyModel(ArrayList arr1){
这是arr1=arr1;
}
public int getColumnCount(){
返回columnNames.length;
}
public int getRowCount(){
返回pru2.datos.size();
}
公共对象getValueAt(整数行,整数列){
对象a1=datos.get(行);
返回a1;
}
}
在Eclipse中编译之后,我没有发现任何错误,因此我无法确定问题的原因$theObject@4ab60e21对于每个单元格都是因为您在
getValueAt()
中返回一个完整的theObject
,例如,它应该只返回theObject
的一个字段,你的班级看起来像下面
public class TheObject { // please notice the Naming convention
// and follow it. Class begins with capital
private fert;
private cliente;
private pep;
// getter and setters
}
getValueAt()
中的your值应该只返回一个基于列的字段
private String[] columnNames = { "FERT","CLIENTE ","PEP" };
private ArrayList<TheObject> data;
...
public Object getValueAt(int row, int col) {
Object value = null;
TheObject obj = data.get(row);
switch(col) {
case 0: value = obj.getFert(); break;
case 1: value = obj.getCliente(); break;
case 2: value = obj.getPep(); break;
default: break;
}
return value;
}
private String[]columnNames={“FERT”、“CLIENTE”、“PEP”};
私有数组列表数据;
...
公共对象getValueAt(整数行,整数列){
对象值=空;
对象对象=data.get(行);
开关(col){
案例0:value=obj.getFert();中断;
案例1:value=obj.getCliente();break;
案例2:value=obj.getPep();break;
默认:中断;
}
返回值;
}
因此,不,您的单元格值将只是对象的一个字段,而不是整个对象。我们无法调试不可见的代码。请发布用于生成这些结果的代码。我马上看到的一件事是,您可能不想要
guiObject@4ab60e21
。这只是覆盖那些对象的toString
方法的问题。@Aaron发布你的代码抱歉我刚刚添加了一些代码非常感谢peeskillet你的评论是正确的,现在mi表工作得很好。
private String[] columnNames = { "FERT","CLIENTE ","PEP" };
private ArrayList<TheObject> data;
...
public Object getValueAt(int row, int col) {
Object value = null;
TheObject obj = data.get(row);
switch(col) {
case 0: value = obj.getFert(); break;
case 1: value = obj.getCliente(); break;
case 2: value = obj.getPep(); break;
default: break;
}
return value;
}