javax.swing.table

Class DefaultTableModel

  • All Implemented Interfaces:
    Serializable, TableModel

    public class DefaultTableModel
    extends AbstractTableModel
    implements Serializable
    This is an implementation of TableModel that uses a Vector of Vectors to store the cell value objects.

    Warning: DefaultTableModel returns a column class of Object. When DefaultTableModel is used with a TableRowSorter this will result in extensive use of toString, which for non-String data types is expensive. If you use DefaultTableModel with a TableRowSorter you are strongly encouraged to override getColumnClass to return the appropriate type.

    Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see XMLEncoder.

    See Also:
    TableModel, getDataVector()
    • Field Detail

      • dataVector

        protected Vector dataVector
        The Vector of Vectors of Object values.
      • columnIdentifiers

        protected Vector columnIdentifiers
        The Vector of column identifiers.
    • Constructor Detail

      • DefaultTableModel

        public DefaultTableModel()
        Constructs a default DefaultTableModel which is a table of zero columns and zero rows.
      • DefaultTableModel

        public DefaultTableModel(int rowCount,
                         int columnCount)
        Constructs a DefaultTableModel with rowCount and columnCount of null object values.
        Parameters:
        rowCount - the number of rows the table holds
        columnCount - the number of columns the table holds
        See Also:
        setValueAt(java.lang.Object, int, int)
      • DefaultTableModel

        public DefaultTableModel(Vector columnNames,
                         int rowCount)
        Constructs a DefaultTableModel with as many columns as there are elements in columnNames and rowCount of null object values. Each column's name will be taken from the columnNames vector.
        Parameters:
        columnNames - vector containing the names of the new columns; if this is null then the model has no columns
        rowCount - the number of rows the table holds
        See Also:
        setDataVector(java.util.Vector, java.util.Vector), setValueAt(java.lang.Object, int, int)
      • DefaultTableModel

        public DefaultTableModel(Object[] columnNames,
                         int rowCount)
        Constructs a DefaultTableModel with as many columns as there are elements in columnNames and rowCount of null object values. Each column's name will be taken from the columnNames array.
        Parameters:
        columnNames - array containing the names of the new columns; if this is null then the model has no columns
        rowCount - the number of rows the table holds
        See Also:
        setDataVector(java.util.Vector, java.util.Vector), setValueAt(java.lang.Object, int, int)
      • DefaultTableModel

        public DefaultTableModel(Vector data,
                         Vector columnNames)
        Constructs a DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector method.
        Parameters:
        data - the data of the table, a Vector of Vectors of Object values
        columnNames - vector containing the names of the new columns
        See Also:
        getDataVector(), setDataVector(java.util.Vector, java.util.Vector)
      • DefaultTableModel

        public DefaultTableModel(Object[][] data,
                         Object[] columnNames)
        Constructs a DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector method. The first index in the Object[][] array is the row index and the second is the column index.
        Parameters:
        data - the data of the table
        columnNames - the names of the columns
        See Also:
        getDataVector(), setDataVector(java.util.Vector, java.util.Vector)
    • Method Detail

      • setDataVector

        public void setDataVector(Vector dataVector,
                         Vector columnIdentifiers)
        Replaces the current dataVector instance variable with the new Vector of rows, dataVector. Each row is represented in dataVector as a Vector of Object values. columnIdentifiers are the names of the new columns. The first name in columnIdentifiers is mapped to column 0 in dataVector. Each row in dataVector is adjusted to match the number of columns in columnIdentifiers either by truncating the Vector if it is too long, or adding null values if it is too short.

        Note that passing in a null value for dataVector results in unspecified behavior, an possibly an exception.

        Parameters:
        dataVector - the new data vector
        columnIdentifiers - the names of the columns
        See Also:
        getDataVector()
      • setDataVector

        public void setDataVector(Object[][] dataVector,
                         Object[] columnIdentifiers)
        Replaces the value in the dataVector instance variable with the values in the array dataVector. The first index in the Object[][] array is the row index and the second is the column index. columnIdentifiers are the names of the new columns.
        Parameters:
        dataVector - the new data vector
        columnIdentifiers - the names of the columns
        See Also:
        setDataVector(Vector, Vector)
      • newDataAvailable

        public void newDataAvailable(TableModelEvent event)
        Equivalent to fireTableChanged.
        Parameters:
        event - the change event
      • newRowsAdded

        public void newRowsAdded(TableModelEvent e)
        Ensures that the new rows have the correct number of columns. This is accomplished by using the setSize method in Vector which truncates vectors which are too long, and appends nulls if they are too short. This method also sends out a tableChanged notification message to all the listeners.
        Parameters:
        e - this TableModelEvent describes where the rows were added. If null it assumes all the rows were newly added
        See Also:
        getDataVector()
      • rowsRemoved

        public void rowsRemoved(TableModelEvent event)
        Equivalent to fireTableChanged.
        Parameters:
        event - the change event
      • setNumRows

        public void setNumRows(int rowCount)
        Obsolete as of Java 2 platform v1.3. Please use setRowCount instead.
      • setRowCount

        public void setRowCount(int rowCount)
        Sets the number of rows in the model. If the new size is greater than the current size, new rows are added to the end of the model If the new size is less than the current size, all rows at index rowCount and greater are discarded.

        Since:
        1.3
        See Also:
        setColumnCount(int)
      • addRow

        public void addRow(Vector rowData)
        Adds a row to the end of the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.
        Parameters:
        rowData - optional data of the row being added
      • addRow

        public void addRow(Object[] rowData)
        Adds a row to the end of the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.
        Parameters:
        rowData - optional data of the row being added
      • insertRow

        public void insertRow(int row,
                     Vector rowData)
        Inserts a row at row in the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.
        Parameters:
        row - the row index of the row to be inserted
        rowData - optional data of the row being added
        Throws:
        ArrayIndexOutOfBoundsException - if the row was invalid
      • insertRow

        public void insertRow(int row,
                     Object[] rowData)
        Inserts a row at row in the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.
        Parameters:
        row - the row index of the row to be inserted
        rowData - optional data of the row being added
        Throws:
        ArrayIndexOutOfBoundsException - if the row was invalid
      • moveRow

        public void moveRow(int start,
                   int end,
                   int to)
        Moves one or more rows from the inclusive range start to end to the to position in the model. After the move, the row that was at index start will be at index to. This method will send a tableChanged notification message to all the listeners.

          Examples of moves:
          

        1. moveRow(1,3,5); a|B|C|D|e|f|g|h|i|j|k - before a|e|f|g|h|B|C|D|i|j|k - after

        2. moveRow(6,7,1); a|b|c|d|e|f|G|H|i|j|k - before a|G|H|b|c|d|e|f|i|j|k - after

        Parameters:
        start - the starting row index to be moved
        end - the ending row index to be moved
        to - the destination of the rows to be moved
        Throws:
        ArrayIndexOutOfBoundsException - if any of the elements would be moved out of the table's range
      • removeRow

        public void removeRow(int row)
        Removes the row at row from the model. Notification of the row being removed will be sent to all the listeners.
        Parameters:
        row - the row index of the row to be removed
        Throws:
        ArrayIndexOutOfBoundsException - if the row was invalid
      • setColumnIdentifiers

        public void setColumnIdentifiers(Vector columnIdentifiers)
        Replaces the column identifiers in the model. If the number of newIdentifiers is greater than the current number of columns, new columns are added to the end of each row in the model. If the number of newIdentifiers is less than the current number of columns, all the extra columns at the end of a row are discarded.

        Parameters:
        columnIdentifiers - vector of column identifiers. If null, set the model to zero columns
        See Also:
        setNumRows(int)
      • setColumnIdentifiers

        public void setColumnIdentifiers(Object[] newIdentifiers)
        Replaces the column identifiers in the model. If the number of newIdentifiers is greater than the current number of columns, new columns are added to the end of each row in the model. If the number of newIdentifiers is less than the current number of columns, all the extra columns at the end of a row are discarded.

        Parameters:
        newIdentifiers - array of column identifiers. If null, set the model to zero columns
        See Also:
        setNumRows(int)
      • setColumnCount

        public void setColumnCount(int columnCount)
        Sets the number of columns in the model. If the new size is greater than the current size, new columns are added to the end of the model with null cell values. If the new size is less than the current size, all columns at index columnCount and greater are discarded.
        Parameters:
        columnCount - the new number of columns in the model
        Since:
        1.3
        See Also:
        setColumnCount(int)
      • addColumn

        public void addColumn(Object columnName)
        Adds a column to the model. The new column will have the identifier columnName, which may be null. This method will send a tableChanged notification message to all the listeners. This method is a cover for addColumn(Object, Vector) which uses null as the data vector.
        Parameters:
        columnName - the identifier of the column being added
      • addColumn

        public void addColumn(Object columnName,
                     Vector columnData)
        Adds a column to the model. The new column will have the identifier columnName, which may be null. columnData is the optional vector of data for the column. If it is null the column is filled with null values. Otherwise, the new data will be added to model starting with the first element going to row 0, etc. This method will send a tableChanged notification message to all the listeners.
        Parameters:
        columnName - the identifier of the column being added
        columnData - optional data of the column being added
      • addColumn

        public void addColumn(Object columnName,
                     Object[] columnData)
        Adds a column to the model. The new column will have the identifier columnName. columnData is the optional array of data for the column. If it is null the column is filled with null values. Otherwise, the new data will be added to model starting with the first element going to row 0, etc. This method will send a tableChanged notification message to all the listeners.
        See Also:
        addColumn(Object, Vector)
      • getColumnName

        public String getColumnName(int column)
        Returns the column name.
        Specified by:
        getColumnName in interface TableModel
        Overrides:
        getColumnName in class AbstractTableModel
        Parameters:
        column - the column being queried
        Returns:
        a name for this column using the string value of the appropriate member in columnIdentifiers. If columnIdentifiers does not have an entry for this index, returns the default name provided by the superclass.
      • getValueAt

        public Object getValueAt(int row,
                        int column)
        Returns an attribute value for the cell at row and column.
        Specified by:
        getValueAt in interface TableModel
        Parameters:
        row - the row whose value is to be queried
        column - the column whose value is to be queried
        Returns:
        the value Object at the specified cell
        Throws:
        ArrayIndexOutOfBoundsException - if an invalid row or column was given
      • convertToVector

        protected static Vector convertToVector(Object[] anArray)
        Returns a vector that contains the same objects as the array.
        Parameters:
        anArray - the array to be converted
        Returns:
        the new vector; if anArray is null, returns null
      • convertToVector

        protected static Vector convertToVector(Object[][] anArray)
        Returns a vector of vectors that contains the same objects as the array.
        Parameters:
        anArray - the double array to be converted
        Returns:
        the new vector of vectors; if anArray is null, returns null

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 11/06/2005 gemaakt, de laatste keer de 04/03/2020 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/java-api-rf-javax/swing/table/defaulttablemodel.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.

Referenties

  1. Bekijk - html-document Taal van het document:fr Manuel PHP : https://docs.oracle.com

Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.

Inhoudsopgave Haut