?
Solved

Adding Object to Custom Table Model

Posted on 2004-10-28
2
Medium Priority
?
195 Views
Last Modified: 2010-03-31
I am using the below code as my custom table model.  Now, when I'm using either method .addMachine or .addMachineList, I do not get the objects to load in the table.  I know the JTable is taking the model because the column headers are being assigned.  Now, if I use my constructor which takes in a List, it still does not render the data.  Am I missing a function from the DefaultTableModel that I need to override?

Thanks,
David

      public class MachineListModel extends AbstractTableModel {
          private ArrayList datalist = new ArrayList();
          private String[] columns={"Name",
                                    "Description",
                                    "Job Number",
                                    "Dept"};
                                   
          public MachineListModel(){
             
          }
         
          public MachineListModel(List l){
              datalist.addAll(l);
          }
         
          public void addMachine(Machine m){
              datalist.add(m);
              fireTableDataChanged();
          }
         
          public void addMachineList(List l){
              datalist.addAll(l);
              fireTableDataChanged();
          }
         
          public Machine removeMachineAt(int row){
              Machine machine = (Machine)datalist.remove(row);
              fireTableDataChanged();
              return machine;
          }
         
          public Machine getMachineAt(int row){
              return (Machine)datalist.get(row);
          }
         
          public String getColumnName(int col){
              return columns[col];
          }
           
          public int getRowCount() {
           return 0;
          }

          public int getColumnCount() {
           return columns.length;
          }

          public Object getValueAt(int row, int col) {
            Machine machine = (Machine)datalist.get(row);
            switch(col){
                case 0:
                    return machine.getName();
                case 1:
                    return machine.getDescription();
                case 2:
                    return machine.getJobnumber();
                case 3:
                    return machine.getDepartment();
                default:
                    return null;
            }
          }
}
0
Comment
Question by:lomidien
2 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 12440637
>          public int getRowCount() {
>           return 0;
>          }

you aren't returning the row count.
should be:

return datalist.size();

0
 

Author Comment

by:lomidien
ID: 12440655
Man Alive!!!  Another pair eyes are indeed a good thing.  :)  Looked right over that one.

Thanks,
David
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month5 days, 3 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question