/*
* @(#)JTable.java 1.288 06/11/15
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package javax.swing;
import java.util.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.beans.*;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
import javax.accessibility.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
import javax.swing.table.*;
import javax.swing.border.*;
import java.text.NumberFormat;
import java.text.DateFormat;
import java.text.MessageFormat;
import javax.print.attribute.*;
import javax.print.PrintService;
import sun.swing.SwingUtilities2;
import sun.swing.SwingUtilities2.Section;
import static sun.swing.SwingUtilities2.Section.*;
import sun.swing.PrintingStatus;
/**
* The <code>JTable</code> is used to display and edit regular two-dimensional tables
* of cells.
* See <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/table.html">How to Use Tables</a>
* in <em>The Java Tutorial</em>
* for task-oriented documentation and examples of using <code>JTable</code>.
*
* <p>
* The <code>JTable</code> has many
* facilities that make it possible to customize its rendering and editing
* but provides defaults for these features so that simple tables can be
* set up easily. For example, to set up a table with 10 rows and 10
* columns of numbers:
* <p>
* <pre>
* TableModel dataModel = new AbstractTableModel() {
* public int getColumnCount() { return 10; }
* public int getRowCount() { return 10;}
* public Object getValueAt(int row, int col) { return new Integer(row*col); }
* };
* JTable table = new JTable(dataModel);
* JScrollPane scrollpane = new JScrollPane(table);
* </pre>
* <p>
* Note that if you wish to use a <code>JTable</code> in a standalone
* view (outside of a <code>JScrollPane</code>) and want the header
* displayed, you can get it using {@link #getTableHeader} and
* display it separately.
* <p>
* To enable sorting and filtering of rows, use a
* {@code RowSorter}.
* You can set up a row sorter in either of two ways:
* <ul>
* <li>Directly set the {@code RowSorter}. For example:
* {@code table.setRowSorter(new TableRowSorter(model))}.
* <li>Set the {@code autoCreateRowSorter}
* property to {@code true}, so that the {@code JTable}
* creates a {@code RowSorter} for
* you. For example: {@code setAutoCreateRowSorter(true)}.
* </ul>
* <p>
* When designing applications that use the <code>JTable</code> it is worth paying
* close attention to the data structures that will represent the table's data.
* The <code>DefaultTableModel</code> is a model implementation that
* uses a <code>Vector</code> of <code>Vector</code>s of <code>Object</code>s to
* store the cell values. As well as copying the data from an
* application into the <code>DefaultTableModel</code>,
* it is also possible to wrap the data in the methods of the
* <code>TableModel</code> interface so that the data can be passed to the
* <code>JTable</code> directly, as in the example above. This often results
* in more efficient applications because the model is free to choose the
* internal representation that best suits the data.
* A good rule of thumb for deciding whether to use the <code>AbstractTableModel</code>
* or the <code>DefaultTableModel</code> is to use the <code>AbstractTableModel</code>
* as the base class for creating subclasses and the <code>DefaultTableModel</code>
* when subclassing is not required.
* <p>
* The "TableExample" directory in the demo area of the source distribution
* gives a number of complete examples of <code>JTable</code> usage,
* covering how the <code>JTable</code> can be used to provide an
* editable view of data taken from a database and how to modify
* the columns in the display to use specialized renderers and editors.
* <p>
* The <code>JTable</code> uses integers exclusively to refer to both the rows and the columns
* of the model that it displays. The <code>JTable</code> simply takes a tabular range of cells
* and uses <code>getValueAt(int, int)</code> to retrieve the
* values from the model during painting. It is important to remember that
* the column and row indexes returned by various <code>JTable</code> methods
* are in terms of the <code>JTable</code> (the view) and are not
* necessarily the same indexes used by the model.
* <p>
* By default, columns may be rearranged in the <code>JTable</code> so that the
* view's columns appear in a different order to the columns in the model.
* This does not affect the implementation of the model at all: when the
* columns are reordered, the <code>JTable</code> maintains the new order of the columns
* internally and converts its column indices before querying the model.
* <p>
* So, when writing a <code>TableModel</code>, it is not necessary to listen for column
* reordering events as the model will be queried in its own coordinate
* system regardless of what is happening in the view.
* In the examples area there is a demonstration of a sorting algorithm making
* use of exactly this technique to interpose yet another coordinate system
* where the order of the rows is changed, rather than the order of the columns.
* <p>
* Similarly when using the sorting and filtering functionality
* provided by <code>RowSorter</code> the underlying
* <code>TableModel</code> does not need to know how to do sorting,
* rather <code>RowSorter</code> will handle it. Coordinate
* conversions will be necessary when using the row based methods of
* <code>JTable</code> with the underlying <code>TableModel</code>.
* All of <code>JTable</code>s row based methods are in terms of the
* <code>RowSorter</code>, which is not necessarily the same as that
* of the underlying <code>TableModel</code>. For example, the
* selection is always in terms of <code>JTable</code> so that when
* using <code>RowSorter</code> you will need to convert using
* <code>convertRowIndexToView</code> or
* <code>convertRowIndexToModel</code>. The following shows how to
* convert coordinates from <code>JTable</code> to that of the
* underlying model:
* <pre>
* int[] selection = table.getSelectedRows();
* for (int i = 0; i < selection.length; i++) {
* selection[i] = table.convertRowIndexToModel(selection[i]);
* }
* // selection is now in terms of the underlying TableModel
* </pre>
* <p>
* By default if sorting is enabled <code>JTable</code> will persist the
* selection and variable row heights in terms of the model on
* sorting. For example if row 0, in terms of the underlying model,
* is currently selected, after the sort row 0, in terms of the
* underlying model will be selected. Visually the selection may
* change, but in terms of the underlying model it will remain the
* same. The one exception to that is if the model index is no longer
* visible or was removed. For example, if row 0 in terms of model
* was filtered out the selection will be empty after the sort.
* <p>
* J2SE 5 adds methods to <code>JTable</code> to provide convenient access to some
* common printing needs. Simple new {@link #print()} methods allow for quick
* and easy addition of printing support to your application. In addition, a new
* {@link #getPrintable} method is available for more advanced printing needs.
* <p>
* As for all <code>JComponent</code> classes, you can use
* {@link InputMap} and {@link ActionMap} to associate an
* {@link Action} object with a {@link KeyStroke} and execute the
* action under specified conditions.
* <p>
* <strong>Warning:</strong> Swing is not thread safe. For more
* information see <a
* href="package-summary.html#threading">Swing's Threading
* Policy</a>.
* <p>
* <strong>Warning:</s
- 1
- 2
前往页