package miaplicacion.util; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import javax.swing.table.AbstractTableModel; /** * * @author dinfantas */ public class ArraylistTableModel extends AbstractTableModel { ArrayList data; private String[] columnNames; public ArraylistTableModel() { } public ArraylistTableModel(ArrayList datos, String[] cols) { this.data = datos; int numCols = cols.length; columnNames = new String[numCols]; System.arraycopy(cols, 0, columnNames, 0, numCols); } /** * Overrides AbstractTableModel method. Returns the number of columns in * table * * @return int number of columns in the table. */ public int getColumnCount() { return columnNames.length; } /** * Overrides AbstractTableModel method. Returns the number of rows in table * * @return int number of rows in the table. */ public int getRowCount() { return data.size(); } /** * Overrides AbstractTableModel method. * * @param colcolumn number column number * * @return String column name. */ @Override public String getColumnName(int col) { return columnNames[col]; } /** * Overrides AbstractTableModel method. * * @param row row number * @param col column number * * @return Object the value at the specified cell. */ public Object getValueAt(int row, int col) { ArrayList colArrayList = (ArrayList) data.get(row); // System.out.println("Celda[" + row + "," + col + "]" + colArrayList.get(col).toString()); return colArrayList.get(col); } @Override public Class getColumnClass(int i) { ArrayList colArrayList = (ArrayList) data.get(0); return colArrayList.get(i).getClass(); } @Override public boolean isCellEditable(int row, int col) { return super.isCellEditable(row, col); } @Override public void setValueAt(Object value, int row, int col) { ArrayList aux=(ArrayList) data.get(row); if(value instanceof Date){//para el caso de fechas SimpleDateFormat formateador=new SimpleDateFormat("yyyy-M-d"); value=formateador.format(value); } aux.set(col, value); data.set(row, aux); if (col != 0){ fireTableCellUpdated(row, col); } } @Override public void fireTableCellUpdated(int row, int col) { if(this.getValueAt(row, 0) == Boolean.FALSE){ this.setValueAt(Boolean.TRUE, row, 0); } } }
lunes, 18 de octubre de 2010
Clase TableModel basada en ArrayList [ ArraylistTableModel]
Este codigo implementa una simple clase donde los datos de un JTable se almacenan un model basado en ArrayList donde cada fila es otro ArrayList.
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario