Jgoodies JTable Binding,Swing

时间:2012-02-04 04:25:44

标签: swing jgoodies

任何人都可以提供jgoodies Jtable绑定简单示例使用swing获取javabeans扩展Jgoodies Model类的列表。我找不到一个简单的例子。

先谢谢

1 个答案:

答案 0 :(得分:3)

对于JTable绑定,您需要为JTable写一个TableModel

例如,您有一个java bean,例如

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

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

创建一个表,

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

类MyTableModel将list作为参数,它将扩展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();
    } 
}

}

希望这有帮助。