-1

I have An Object called Student and in this Object i have A arrayList of Object That Contains material(name,note)

Public Class Student{
private String StudentFirstName;
private String StudentLastName;
private List<Material> materials;

}  
public Class Material{
String String materialName;
String Float note;

}

i want to display All Data in Jtable like that:

StudentFirstName:Jack StudentLastName:Dupont materialName:Math note:15 StudentFirstName:Jack StudentLastName:Dupont materialName:french note:12 StudentFirstName:Jack StudentLastName:Dupont materialName:Math note:15 StudentFirstName:Jack StudentLastName:Dupont materialName:Sport note:10

StudentFirstName:peter StudentLastName:sanchez materialName:Math note:14 StudentFirstName:peter StudentLastName:sanchez materialName:french note:17 StudentFirstName:peter StudentLastName:sanchez materialName:Arabic note:11

this Is My Table Model

package com.orange.tableModel;
public class DataTableModel extends AbstractTableModel {
String[] entete = {"StudentFisrtName", "StudentLastNameName", 
"Mat    erialName", "MaterialNote"};
List<Student> allStudents;
public DataTableModel() {
allStudents = new ArrayList<>();

}

@Override
public int getRowCount() {
    return allStudents.size();

}

@Override
public int getColumnCount() {
    return entete.length;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {

    switch (columnIndex) {
        case 0: {
           return allStudents.get(rowIndex).getStudentFirstName();

        }
        case 1: {
            return allStudents.get(rowIndex).getStudentLastName();

        }
        case 2: {

            return allStudents.get(rowIndex).materials(rowIndex).getMaterialName();
        }
        case 3: {
            return allStudents.get(rowIndex).materials(rowIndex).getNote();
        }

        default:
            throw new IllegalArgumentException();
    }

}

@Override
public String getColumnName(int column) {

    return entete[column];
}

}

Jtable dataTable=new Jtable();
dataTable.setModel(new DataTableModel());     `

So the result Is:

StudentFirstName:Jack StudentLastName:Dupont materialName:Math note:15

StudentFirstName:peter StudentLastName:sanchez materialName:Math note:14

user3299124
  • 23
  • 2
  • 7

1 Answers1

0

I'll do something like this:

First of all, the method getRowCount()

returns allStudents.size()

and actually this value is two, so you tell to your DataTableModel that your tables contains 2 rows and that is incorrect. Try to modify this method doing something like that:

@Override
public int getRowCount() {
  int row_count=0;
  for (int i=0; i<allStudents.size(); i++) {
    row_cont+=allStudents.get(i).getMaterials().size();
} 

in this way, you'll force the jtable to have how many rows you need. Next, you should write a smarter method getValueAt. You should maintain a variable (e.g. count_row) that tells you how many rows are dedicated to the same Student and doing a control over that, for example

if (count_row< allStudents.get(rowIndex).getMaterials().size()) {
  //show information of the same Student but with materials(count_row)
}

or something like this. In a nutshell, you tell to your jTable to display the same Student until every Material is displayed.

You can solve this problem following another way: you should create an ArrayList with redundant data, for example doing something like this

Student A_B=new Student("A","B","Math",12);    //first_name, last_name, subject, note
Student A_B=new Student("A","B","History",4);
Student B_C=new Student("B","C","Geograph",10);

Hope that this can be to you an useful tip. Hi !!

user2896152
  • 762
  • 1
  • 9
  • 32
  • thx for your answer i will test your first solution :) for the second i do it before i asked the question i mix the two object :D and it worked but i don't appreciate,i want to better modelize and follow my class diagram @user2896152 – user3299124 Jul 20 '15 at 09:59