swing - Syncing a JTable with ArrayList of Objects values in Java -


my program has jtable displays information student. e.g.

columns contain: "name", "surname", "age", "year" 

i have object class students follow:

public class student {     private final string name;     private final string surname;     private final int age;     private int year;      public student(string name, string surname, int age, int year) {         this.name = name;         this.surname = surname;         this.age = age;         this.year = year;     }      public string getname() {         return this.name;     }      public string getsurname() {         return this.surname;     }      public int getage() {         return this.age;     }      public int getyear() {         return this.year;     }      public void setyear(int i) {         this.year = i;     } } 

i have studentmanager below:

public class studentmanager {     private static arraylist<student> students = new arraylist<student>();      public static void addstudent(student obj) {         this.students.add(obj);     }      public static void removestudent(student obj) {         this.students.remove(obj);     }      public static student getstudentbyname(string n) {         for(student s : this.students) {             if(s.getname() == n)                 return s;         }         return null;     } } 

what want know:

i want when change value in student class object jtable update new information.

i want jtable remove row of student if remove student class object arraylist.

also same adding student, want add row jtable students name, surname, age , year.

you need create studenttablemodel (which extends abstracttablemodel) supply data jtable , when change occures fire updates (fire* methods).

next, studenttablemodel needs know change occured. use propertychangelistener that. see propertychangesupport see how used.

basically, studenttablemodel listen changes studentmanager , each student , propagate updates jtable.

alternative ways:

  • you can use observable/observer instead of propertychangelistener
  • you can merge functionality of studenttablemodel , studentmanager

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -