Jgoodies JTable Binding,Swing

2.9k views Asked by At

Can anyone provide jgoodies Jtable binding simple example using swing taking a List of javabeans extending Jgoodies Model class.I could not find a simple example of doing it.

Thanks in Advance

1

There are 1 answers

2
rahul pasricha On

For JTable binding, you need to write a TableModel for your JTable

For example, you have a java bean such as

public class Employee {
     private String employeeName;
     private String employeeNumber;

     // And the getters and setters for both the variables
}

Create a table,

List <Employee> myList = new ArrayList<Employee>(); 
// add few Employee objects to this list and pass it into MyTableModel class
Jtable t1 = new Jtable();
t1.setModel(new MyTableModel(myList));

The class MyTableModel takes list as an argument and it will extend com.jgoodies.binding.adapter.AbstractTableAdapter

MyTableModel.java

public class MyTableModel extends AbstractTableAdapter<Employee> {

SelectionInList<Employee> listModel = new SelectionInList<Employee>();

   public SourceCodeFolderTableAdapter(SelectionInList<Employee> listModel) {
     super(listModel, new String [] {"Employee Name","Employee Number"});
     this.listModel = listModel;
   }

    @Override
public Object getValueAt(int rowIndex, int columnIndex) {
    Employee emp = (Employee) getRow(rowIndex);
    if (columnIndex == 0) {
        return emp.getEmployeeName();
    } else if (columnIndex == 1) {
        return emp.getEmployeeNumber();
    } 
}

}

Hope this helps.