abstract table model (AbstractTableModel)
A convenient way to represent information about a particular topic using model life tables is easy to program our own style and convenience. Java offers component called abstract table model (class AbstractTableModel javax.swing.table package) that implements the TableModel interface whose methods to implement are:
Saturday, January 1, 2011
Pleurodiaphragmatic Adhesion Left
public int getRowCount () - Returns the number of rows that contains the table.
public int getColumnCount () - Returns the number of columns table.
public Object getValueAt ( int row, int column) - Returns the element / value at the position (row, column) of the table.
Then, to illustrate the example of using AbstractTableModel class will have the following sample code to the methods mentioned above:
AbstractTableModel}
class MyTable extends {
@ Override public int getRowCount () {
return
10;
} @ Override public int
getColumnCount ()
{return 10;}
@ Override public Object
getValueAt (int row, int column)
{return "# VALUE #";}
addition to these methods we can override other four AbstractTableModel own class. These methods are:
public getColumnClass Class ( int column) - Returns the class type of a particular column, that is the type of object that is hosting this column in the table.
public String getColumnName ( int column) - Returns the name of the column.
public boolean isCellEditable ( int row, int column) - Returns true if the cell specified by the position (row, column) has enabled the edit mode, otherwise returns false.
public void setValueAt (Object value, int row int column) Sets a value (object) within the table at the position specified by row, column. The description of other methods can be found in the documentation page AbstractTableModel.
worth mentioning that this type of tables they can apply custom styles to make them more pleasing to the eye of the user, such as changing the color of the rows and columns, text font and even add Swing components as part data. Below is an example of the implementation of the above methods. To this end incorporates a table in a form containing key, name and address of users from a fictitious company. We will use the Person class that defines the necessary methods to insert and collect these values, its structure is as follows:
Once we have the Person class can write the Table class that defines the behavior of the table.
class Table extends AbstractTableModel
public getColumnClass Class ( int column) - Returns the class type of a particular column, that is the type of object that is hosting this column in the table.
public String getColumnName ( int column) - Returns the name of the column.
public boolean isCellEditable ( int row, int column) - Returns true if the cell specified by the position (row, column) has enabled the edit mode, otherwise returns false.
public void setValueAt (Object value, int row int column) Sets a value (object) within the table at the position specified by row, column. The description of other methods can be found in the documentation page AbstractTableModel.
worth mentioning that this type of tables they can apply custom styles to make them more pleasing to the eye of the user, such as changing the color of the rows and columns, text font and even add Swing components as part data. Below is an example of the implementation of the above methods. To this end incorporates a table in a form containing key, name and address of users from a fictitious company. We will use the Person class that defines the necessary methods to insert and collect these values, its structure is as follows:
final class Person {public
Person () {
this (0, "", "");
} public Person (int key, String name, String address)
{
setClave (key);
setNombre (name);
setDireccion (address);
;} public void
setClave (int key) {
this.clave = key;
;}
public void setNombre( String nombre )
{
this.nombre = nombre;
}
public void setDireccion( String direccion )
{
this.direccion = direccion;
}
public void setPersona( Persona persona )
{
this.persona = person;}
getClave public int () {
return key;}
public String getName () {
; return name;
} Public String
getDireccion ()
{return address;}
GetPerson public Person ()
{return this;}
key private int ;
private String name;
private String address;
private Person person;}
class Table extends AbstractTableModel
{public Table () {
names = new String [] {"KEY" "NAME", "ADDRESS"};
data = new Vector (0);}
@ Override public Class
getColumnClass (int column)
; {return
getValueAt (0, column). getClass ();}
@ Override getColumnName public String (int column)
{return names [column];}
@ Override public
int getRowCount () {
datos.size return ();}
@ Override ; public int getColumnCount ()
{return
nombres.length;
} @ Override public
getValueAt Object (int row, int column)
{if (datos.size ()> 0)
switch (column)
{
case 0:
return datos.get( fila ).getClave();
case 1:
datos.get return (row). getNombre ();
case 2:
; datos.get return (row). getDireccion ();}
return "";}
Public void
agregarRegistro (Person person) {
datos.add (person);
fireTableStructureChanged ();}
private Vector data;
private String names [];}
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment