0

I'm new to java. I'm following a tutorial to retrieve images and other data from db to a jTable. Data will retrieve to an ArrayList first, and I get this error saying array required but object found. I have added all my code. The error is in the TheModel.java file. Any help would be appreciated.

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];
}

}

Main Method

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);


}
1

3 Answers 3

1

In TheModel, it should be

public class TheModel extends AbstractTableModel {

    private String[] columns;
    private Object[][] rows; //since data is 2 dimensional array
    ..//rest of code
}
Sign up to request clarification or add additional context in comments.

1 Comment

Error is now fixed thanks to you !. But it was supposed to display the sql results in the table. But nothing is happening. I have 4 columns in the database and 4 columns in the table as well. help me please
1

rows is an Object array in your case, but you need an array of arrays of Objects

Object[] -> Object[][]

Comments

1

Your TableModel is wrong. Don't:

  1. Use an Array to hold the Course objects, because you don't know how big to make the Array to hold all the objects.

  2. Use a 2D Array as a parameter to construct the class.

Instead:

  1. Use an ArrayList to hold the Course objects. The ArrayList size will increase automatically as more space is needed to hold all the objects.

  2. Pass in the ArrayList as a parameter when you create the model.

See Row Table Model for a step by step example on how to create a custom TableModel for a custom Object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.