/** * Javier Abellán, 26 Oct 2003 * * ModeloTabla.java * * Modelo de tabla para el ejmplo de uso del JTable */ import javax.swing.table.*; import javax.swing.event.*; import java.util.LinkedList; /** * Modelo de personas. Cada fila es una persona y las columnas son los datos * de la persona. * Implementa TableModel y dos métodos para añadir y eliminar Personas del * modelo */ public class ModeloTabla implements TableModel { /** Returns the number of columns in the model. A * JTable uses this method to determine how many columns it * should create and display by default. * * @return the number of columns in the model * @see #getRowCount * */ public int getColumnCount() { // Devuelve el número de columnas del modelo, que coincide con el // número de datos que tenemos de cada persona. return 3; } /** Returns the number of rows in the model. A * JTable uses this method to determine how many rows it * should display. This method should be quick, as it * is called frequently during rendering. * * @return the number of rows in the model * @see #getColumnCount * */ public int getRowCount() { // Devuelve el número de personas en el modelo, es decir, el número // de filas en la tabla. return datos.size(); } /** Returns the value for the cell at columnIndex and * rowIndex. * * @param rowIndex the row whose value is to be queried * @param columnIndex the column whose value is to be queried * @return the value Object at the specified cell * */ public Object getValueAt(int rowIndex, int columnIndex) { Persona aux; // Se obtiene la persona de la fila indicada aux = (Persona)(datos.get(rowIndex)); // Se obtiene el campo apropiado según el valor de columnIndex switch (columnIndex) { case 0: return aux.dameNombre(); case 1: return aux.dameApellido(); case 2: return new Integer(aux.dameEdad()); default: return null; } } /** * Borra del modelo la persona en la fila indicada */ public void borraPersona (int fila) { // Se borra la fila datos.remove(fila); // Y se avisa a los suscriptores, creando un TableModelEvent... TableModelEvent evento = new TableModelEvent (this, fila, fila, TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE); // ... y pasándoselo a los suscriptores avisaSuscriptores (evento); } /** * Añade una persona al final de la tabla */ public void anhadePersona (Persona nuevaPersona) { // Añade la persona al modelo datos.add (nuevaPersona); // Avisa a los suscriptores creando un TableModelEvent... TableModelEvent evento; evento = new TableModelEvent (this, this.getRowCount()-1, this.getRowCount()-1, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT); // ... y avisando a los suscriptores avisaSuscriptores (evento); } /** Adds a listener to the list that is notified each time a change * to the data model occurs. * * @param l the TableModelListener * */ public void addTableModelListener(TableModelListener l) { // Añade el suscriptor a la lista de suscriptores listeners.add (l); } /** Returns the most specific superclass for all the cell values * in the column. This is used by the JTable to set up a * default renderer and editor for the column. * * @param columnIndex the index of the column * @return the common ancestor class of the object values in the model. * */ public Class getColumnClass(int columnIndex) { // Devuelve la clase que hay en cada columna. switch (columnIndex) { case 0: // La columna cero contiene el nombre de la persona, que es // un String return String.class; case 1: // La columna uno contiene el apellido de la persona, que es // un String return String.class; case 2: // La columna dos contine la edad de la persona, que es un // Integer (no vale int, debe ser una clase) return Integer.class; default: // Devuelve una clase Object por defecto. return Object.class; } } /** Returns the name of the column at columnIndex. This is used * to initialize the table's column header name. Note: this name does * not need to be unique; two columns in a table can have the same name. * * @param columnIndex the index of the column * @return the name of the column * */ public String getColumnName(int columnIndex) { // Devuelve el nombre de cada columna. Este texto aparecerá en la // cabecera de la tabla. switch (columnIndex) { case 0: return "Nombre"; case 1: return "Apellido"; case 2: return "Edad"; default: return null; } } /** Returns true if the cell at rowIndex and * columnIndex * is editable. Otherwise, setValueAt on the cell will not * change the value of that cell. * * @param rowIndex the row whose value to be queried * @param columnIndex the column whose value to be queried * @return true if the cell is editable * @see #setValueAt * */ public boolean isCellEditable(int rowIndex, int columnIndex) { // Permite que la celda sea editable. return true; } /** Removes a listener from the list that is notified each time a * change to the data model occurs. * * @param l the TableModelListener * */ public void removeTableModelListener(TableModelListener l) { // Elimina los suscriptores. listeners.remove(l); } /** Sets the value in the cell at columnIndex and * rowIndex to aValue. * * @param aValue the new value * @param rowIndex the row whose value is to be changed * @param columnIndex the column whose value is to be changed * @see #getValueAt * @see #isCellEditable * */ public void setValueAt(Object aValue, int rowIndex, int columnIndex) { // Obtiene la persona de la fila indicada Persona aux; aux = (Persona)(datos.get(rowIndex)); // Cambia el campo de Persona que indica columnIndex, poniendole el // aValue que se nos pasa. switch (columnIndex) { case 0: aux.tomaNombre ((String)aValue); break; case 1: aux.tomaApellido ((String)aValue); break; case 2: aux.tomaEdad (((Integer)aValue).intValue()); break; default: break; } // Avisa a los suscriptores del cambio, creando un TableModelEvent ... TableModelEvent evento = new TableModelEvent (this, rowIndex, rowIndex, columnIndex); // ... y pasándoselo a los suscriptores. avisaSuscriptores (evento); } /** * Pasa a los suscriptores el evento. */ private void avisaSuscriptores (TableModelEvent evento) { int i; // Bucle para todos los suscriptores en la lista, se llama al metodo // tableChanged() de los mismos, pasándole el evento. for (i=0; i