Java Swing - Generate JTable model by using JavaBean properties and reflection

[Updated: Aug 22, 2017, Created: Jul 22, 2017]

In the following example, the TableModel will be created automatically by using the JavaBean properties of the provided object and Java reflection.

Creating the TableModel

public class TableModelCreator {

  public static <T> TableModel createTableModel(Class<T> beanClass, List<T> list) {
      try {
          BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
          List<String> columns = new ArrayList<>();
          List<Method> getters = new ArrayList<>();

          for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
              String name = pd.getName();
              if (name.equals("class")) {
                  continue;
              }
              name = Character.toUpperCase(name.charAt(0)) + name.substring(1);
              String[] s = name.split("(?=\\p{Upper})");
              String displayName = "";
              for (String s1 : s) {
                  displayName += s1 + " ";
              }

              columns.add(displayName);
              Method m = pd.getReadMethod();
              getters.add(m);
          }

          TableModel model = new AbstractTableModel() {
              @Override
              public String getColumnName(int column) {
                  return columns.get(column);
              }

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

              @Override
              public int getColumnCount() {
                  return columns.size();
              }

              @Override
              public Object getValueAt(int rowIndex, int columnIndex) {
                  try {
                      return getters.get(columnIndex).invoke(list.get(rowIndex));
                  } catch (Exception e) {
                      throw new RuntimeException(e);
                  }
              }
          };
          return model;

      } catch (Exception e) {
          throw new RuntimeException(e);
      }
  }
}

Following is an usage of above generic method.

public class Employee {
  private String name;
  private String dept;
  private String phone;
  private String cellPhone;
    .............
}
public class ExampleMain {
  public static void main(String[] args) {
      JFrame frame = createFrame();
      List<Employee> list = new ArrayList<>();
      for (int i = 1; i <= 30; i++) {
          Employee e = new Employee();
          e.setName("name" + i);
          e.setPhone("phone" + i);
          e.setDept("dept" + i);
          e.setCellPhone("cell" + i);

          list.add(e);
      }
      TableModel tableModel = TableModelCreator.createTableModel(Employee.class, list);
      JTable table = new JTable(tableModel);
      JScrollPane pane = new JScrollPane(table);
      frame.add(pane);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
  }

  private static JFrame createFrame() {
      JFrame frame = new JFrame("JTable example");

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(new Dimension(600, 300));
      return frame;
  }
}

Output

Example Project

Dependencies and Technologies Used :

  • JDK 1.8
  • Maven 3.3.9

Generate JTable model Example Select All Download
  • table-model-generator
    • src
      • main
        • java
          • com
            • logicbig
              • example

See Also