Created
May 27, 2018 05:36
-
-
Save mustofa-id/d3143e1328151b3afe828fd488141ad2 to your computer and use it in GitHub Desktop.
Swing JTable model with generic parameter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.beans.IntrospectionException; | |
import java.beans.PropertyDescriptor; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.swing.table.AbstractTableModel; | |
/** | |
* @param <T> entity class | |
* @author Habib Mustofa | |
* @deprecated use JTableLoader instead | |
*/ | |
public class JSimpleTableModel<T> extends AbstractTableModel { | |
private static final long serialVersionUID = -5231227604182946329L; | |
private final String[] columnName; | |
private final List<T> data; | |
public JSimpleTableModel(String[] columnName) { | |
this.columnName = columnName; | |
this.data = new ArrayList<>(); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see javax.swing.table.AbstractTableModel#getColumnName(int) | |
*/ | |
@Override | |
public String getColumnName(int column) { | |
return columnName[column]; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see javax.swing.table.AbstractTableModel#getColumnClass(int) | |
*/ | |
@Override | |
public Class<?> getColumnClass(int column) { | |
return (getValueAt(0, column).getClass()); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see javax.swing.table.TableModel#getColumnCount() | |
*/ | |
@Override | |
public int getColumnCount() { | |
return columnName.length; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see javax.swing.table.TableModel#getRowCount() | |
*/ | |
@Override | |
public int getRowCount() { | |
return data.size(); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see javax.swing.table.TableModel#getValueAt(int, int) | |
*/ | |
@Override | |
public Object getValueAt(int row, int column) { | |
Object[] values = new Object[getColumnCount()]; | |
if (data.size() > 0) { | |
Class<T> entity = (Class<T>) data.get(row).getClass(); | |
for (int i = 0; i < columnName.length; i++) { | |
try { | |
final Method readMethod = new PropertyDescriptor(columnName[i], entity).getReadMethod(); | |
values[i] = readMethod.invoke(data.get(row)); | |
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | |
| IntrospectionException e) { | |
throw new RuntimeException(e.getMessage()); | |
} | |
} | |
} | |
return values[column]; | |
} | |
public void loadList(List<T> data) { | |
if (data.size() > 0) { | |
int index = 0; | |
for (T t : data) { | |
set(t, index++); | |
} | |
} | |
} | |
private void set(T data, int row) { | |
Class<T> entity = (Class<T>) data.getClass(); | |
try { | |
for (int i = 0; i < columnName.length; i++) { | |
Object values = new PropertyDescriptor(columnName[i], entity).getReadMethod().invoke(data); | |
setValueAt(values, row, i); | |
} | |
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | |
| IntrospectionException e) { | |
throw new RuntimeException(e.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment