我是java的新手。我正在按照教程从db检索图像和其他数据到jTable。数据将首先检索到ArrayList,我得到这个错误,说数组需要但找到了对象。我添加了所有代码。错误发生在TheModel.java
文件中。任何帮助将不胜感激。
Course.java
package my.welcomescreen;
public class Course {
private int id;
private String name;
private byte[] imag;
private String desc;
public Course(){}
public Course(int Id, String Name, byte[] image, String description){
this.id = Id;
this.name = Name;
this.imag = image;
this.desc = description;
}
public int getID(){
return id;
}
public void setID(int ID){
this.id = ID;
}
public String getName(){
return name;
}
public void setName(String Name){
this.name = Name;
}
public byte[] getImage(){
return imag;
}
public String getDesc(){
return desc;
}
public void setDesc(String Description){
this.desc = Description;
}
}
MainQuery.java
package my.welcomescreen;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MainQuery {
public ArrayList<Course> BindTable(){
ArrayList<Course> list = new ArrayList<Course>();
db databaseCon = new db();
Connection dbconnect = db.dbconnect();
Connection con = databaseCon.dbconnect();
Statement st;
ResultSet rs;
try{
st = con.createStatement();
String sql = "select id,name,img,description from courses";
rs = st.executeQuery(sql);
Course c;
while(rs.next()){
c = new Course(
rs.getInt("id"),
rs.getString("name"),
rs.getBytes("img"),
rs.getString("description")
);
}
} catch (SQLException ex) {
Logger.getLogger(Admin_Panel.class.getName()).log(Level.SEVERE, null, ex);
}
return list;
}
}
TheModel.java
package my.welcomescreen;
import javax.swing.Icon;
import javax.swing.table.AbstractTableModel;
public class TheModel extends AbstractTableModel {
private String[] columns;
private Object[] rows;
public TheModel(){}
public TheModel(Object[][] data, String[] columnName){
this.rows = data;
this.columns = columnName;
}
public Class getColumnClass(int Column){
if(Column == 2){
return Icon.class;
} else {
return getValueAt(0,Column).getClass();
}
}
public int getRowCount() {
return this.rows.length;
}
public int getColumnCount() {
return this.columns.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return this.rows[rowIndex][columnIndex]; //this is the error line : array required but object found
}
public String getColumnName(int col){
return this.columns[col];
}
}
主要方法
public void displayJTable(){
MainQuery mq = new MainQuery();
ArrayList<Course> list = mq.BindTable();
String[] columnName = {"Id","Course Name","Image","Description"};
Object[][] rows = new Object[list.size()][3];
for(int i = 0; i < list.size(); i++){
rows[i][0] = list.get(i).getID();
rows[i][1] = list.get(i).getName();
if(list.get(i).getImage() != null){
ImageIcon image = new ImageIcon(new ImageIcon(list.get(i).getImage()).getImage()
.getScaledInstance(150, 120, Image.SCALE_SMOOTH) );
rows[i][2] = image;
}
rows[i][3] = list.get(i).getDesc();
}
TheModel model = new TheModel(rows, columnName);
jTable1.setModel(model);
jTable1.setRowHeight(120);
jTable1.getColumnModel().getColumn(3).setPreferredWidth(150);
}
答案 0 :(得分:0)
在TheModel
中,它应该是
public class TheModel extends AbstractTableModel {
private String[] columns;
private Object[][] rows; //since data is 2 dimensional array
..//rest of code
}
答案 1 :(得分:0)
rows
在您的案例中是一个Object
数组,但您需要一个Object
的数组数组
Object[]
- &gt; Object[][]
答案 2 :(得分:0)
您的TableModel错误。唐&#39; T:
使用数组来保存Course
个对象,因为您不知道让数组保存所有对象有多大。
使用2D数组作为参数构建类。
相反:
使用ArrayList来保存Course
个对象。 ArrayList大小将自动增加,因为需要更多空间来容纳所有对象。
创建模型时,将ArrayList作为参数传递。
有关如何为自定义对象创建自定义TableModel的详细步骤示例,请参阅Row Table Model。