< prev index next >

src/java.desktop/share/classes/javax/swing/table/TableRowSorter.java

Print this page

        

*** 29,94 **** import javax.swing.DefaultRowSorter; import javax.swing.RowFilter; import javax.swing.SortOrder; /** ! * An implementation of <code>RowSorter</code> that provides sorting ! * and filtering using a <code>TableModel</code>. ! * The following example shows adding sorting to a <code>JTable</code>: * <pre> * TableModel myModel = createMyTableModel(); * JTable table = new JTable(myModel); * table.setRowSorter(new TableRowSorter(myModel)); * </pre> * This will do all the wiring such that when the user does the appropriate * gesture, such as clicking on the column header, the table will * visually sort. * <p> ! * <code>JTable</code>'s row-based methods and <code>JTable</code>'s * selection model refer to the view and not the underlying * model. Therefore, it is necessary to convert between the two. For ! * example, to get the selection in terms of <code>myModel</code> * you need to convert the indices: * <pre> * int[] selection = table.getSelectedRows(); * for (int i = 0; i &lt; selection.length; i++) { * selection[i] = table.convertRowIndexToModel(selection[i]); * } * </pre> ! * Similarly to select a row in <code>JTable</code> based on * a coordinate from the underlying model do the inverse: * <pre> * table.setRowSelectionInterval(table.convertRowIndexToView(row), * table.convertRowIndexToView(row)); * </pre> * <p> * The previous example assumes you have not enabled filtering. If you ! * have enabled filtering <code>convertRowIndexToView</code> will return * -1 for locations that are not visible in the view. * <p> ! * <code>TableRowSorter</code> uses <code>Comparator</code>s for doing ! * comparisons. The following defines how a <code>Comparator</code> is * chosen for a column: * <ol> ! * <li>If a <code>Comparator</code> has been specified for the column by the ! * <code>setComparator</code> method, use it. ! * <li>If the column class as returned by <code>getColumnClass</code> is ! * <code>String</code>, use the <code>Comparator</code> returned by ! * <code>Collator.getInstance()</code>. ! * <li>If the column class implements <code>Comparable</code>, use a ! * <code>Comparator</code> that invokes the <code>compareTo</code> * method. ! * <li>If a <code>TableStringConverter</code> has been specified, use it ! * to convert the values to <code>String</code>s and then use the ! * <code>Comparator</code> returned by <code>Collator.getInstance()</code>. ! * <li>Otherwise use the <code>Comparator</code> returned by ! * <code>Collator.getInstance()</code> on the results from ! * calling <code>toString</code> on the objects. * </ol> * <p> ! * In addition to sorting <code>TableRowSorter</code> provides the ability ! * to filter. A filter is specified using the <code>setFilter</code> * method. The following example will only show rows containing the string * "foo": * <pre> * TableModel myModel = createMyTableModel(); * TableRowSorter sorter = new TableRowSorter(myModel); --- 29,94 ---- import javax.swing.DefaultRowSorter; import javax.swing.RowFilter; import javax.swing.SortOrder; /** ! * An implementation of {@code RowSorter} that provides sorting ! * and filtering using a {@code TableModel}. ! * The following example shows adding sorting to a {@code JTable}: * <pre> * TableModel myModel = createMyTableModel(); * JTable table = new JTable(myModel); * table.setRowSorter(new TableRowSorter(myModel)); * </pre> * This will do all the wiring such that when the user does the appropriate * gesture, such as clicking on the column header, the table will * visually sort. * <p> ! * {@code JTable}'s row-based methods and {@code JTable}'s * selection model refer to the view and not the underlying * model. Therefore, it is necessary to convert between the two. For ! * example, to get the selection in terms of {@code myModel} * you need to convert the indices: * <pre> * int[] selection = table.getSelectedRows(); * for (int i = 0; i &lt; selection.length; i++) { * selection[i] = table.convertRowIndexToModel(selection[i]); * } * </pre> ! * Similarly to select a row in {@code JTable} based on * a coordinate from the underlying model do the inverse: * <pre> * table.setRowSelectionInterval(table.convertRowIndexToView(row), * table.convertRowIndexToView(row)); * </pre> * <p> * The previous example assumes you have not enabled filtering. If you ! * have enabled filtering {@code convertRowIndexToView} will return * -1 for locations that are not visible in the view. * <p> ! * {@code TableRowSorter} uses {@code Comparator}s for doing ! * comparisons. The following defines how a {@code Comparator} is * chosen for a column: * <ol> ! * <li>If a {@code Comparator} has been specified for the column by the ! * {@code setComparator} method, use it. ! * <li>If the column class as returned by {@code getColumnClass} is ! * {@code String}, use the {@code Comparator} returned by ! * {@code Collator.getInstance()}. ! * <li>If the column class implements {@code Comparable}, use a ! * {@code Comparator} that invokes the {@code compareTo} * method. ! * <li>If a {@code TableStringConverter} has been specified, use it ! * to convert the values to {@code String}s and then use the ! * {@code Comparator} returned by {@code Collator.getInstance()}. ! * <li>Otherwise use the {@code Comparator} returned by ! * {@code Collator.getInstance()} on the results from ! * calling {@code toString} on the objects. * </ol> * <p> ! * In addition to sorting {@code TableRowSorter} provides the ability ! * to filter. A filter is specified using the {@code setFilter} * method. The following example will only show rows containing the string * "foo": * <pre> * TableModel myModel = createMyTableModel(); * TableRowSorter sorter = new TableRowSorter(myModel);
*** 96,127 **** * JTable table = new JTable(myModel); * table.setRowSorter(sorter); * </pre> * <p> * If the underlying model structure changes (the ! * <code>modelStructureChanged</code> method is invoked) the following ! * are reset to their default values: <code>Comparator</code>s by * column, current sort order, and whether each column is sortable. The default * sort order is natural (the same as the model), and columns are * sortable by default. * <p> ! * <code>TableRowSorter</code> has one formal type parameter: the type * of the model. Passing in a type that corresponds exactly to your * model allows you to filter based on your model without casting. ! * Refer to the documentation of <code>RowFilter</code> for an example * of this. * <p> ! * <b>WARNING:</b> <code>DefaultTableModel</code> returns a column ! * class of <code>Object</code>. As such all comparisons will ! * be done using <code>toString</code>. This may be unnecessarily * expensive. If the column only contains one type of value, such as ! * an <code>Integer</code>, you should override <code>getColumnClass</code> and ! * return the appropriate <code>Class</code>. This will dramatically * increase the performance of this class. * * @param <M> the type of the model, which must be an implementation of ! * <code>TableModel</code> * @see javax.swing.JTable * @see javax.swing.RowFilter * @see javax.swing.table.DefaultTableModel * @see java.text.Collator * @see java.util.Comparator --- 96,127 ---- * JTable table = new JTable(myModel); * table.setRowSorter(sorter); * </pre> * <p> * If the underlying model structure changes (the ! * {@code modelStructureChanged} method is invoked) the following ! * are reset to their default values: {@code Comparator}s by * column, current sort order, and whether each column is sortable. The default * sort order is natural (the same as the model), and columns are * sortable by default. * <p> ! * {@code TableRowSorter} has one formal type parameter: the type * of the model. Passing in a type that corresponds exactly to your * model allows you to filter based on your model without casting. ! * Refer to the documentation of {@code RowFilter} for an example * of this. * <p> ! * <b>WARNING:</b> {@code DefaultTableModel} returns a column ! * class of {@code Object}. As such all comparisons will ! * be done using {@code toString}. This may be unnecessarily * expensive. If the column only contains one type of value, such as ! * an {@code Integer}, you should override {@code getColumnClass} and ! * return the appropriate {@code Class}. This will dramatically * increase the performance of this class. * * @param <M> the type of the model, which must be an implementation of ! * {@code TableModel} * @see javax.swing.JTable * @see javax.swing.RowFilter * @see javax.swing.table.DefaultTableModel * @see java.text.Collator * @see java.util.Comparator
*** 144,187 **** */ private TableStringConverter stringConverter; /** ! * Creates a <code>TableRowSorter</code> with an empty model. */ public TableRowSorter() { this(null); } /** ! * Creates a <code>TableRowSorter</code> using <code>model</code> ! * as the underlying <code>TableModel</code>. * ! * @param model the underlying <code>TableModel</code> to use, ! * <code>null</code> is treated as an empty model */ public TableRowSorter(M model) { setModel(model); } /** ! * Sets the <code>TableModel</code> to use as the underlying model ! * for this <code>TableRowSorter</code>. A value of <code>null</code> * can be used to set an empty model. * ! * @param model the underlying model to use, or <code>null</code> */ public void setModel(M model) { tableModel = model; setModelWrapper(new TableRowSorterModelWrapper()); } /** * Sets the object responsible for converting values from the ! * model to strings. If non-<code>null</code> this * is used to convert any object values, that do not have a ! * registered <code>Comparator</code>, to strings. * * @param stringConverter the object responsible for converting values * from the model to strings */ public void setStringConverter(TableStringConverter stringConverter) { --- 144,187 ---- */ private TableStringConverter stringConverter; /** ! * Creates a {@code TableRowSorter} with an empty model. */ public TableRowSorter() { this(null); } /** ! * Creates a {@code TableRowSorter} using {@code model} ! * as the underlying {@code TableModel}. * ! * @param model the underlying {@code TableModel} to use, ! * {@code null} is treated as an empty model */ public TableRowSorter(M model) { setModel(model); } /** ! * Sets the {@code TableModel} to use as the underlying model ! * for this {@code TableRowSorter}. A value of {@code null} * can be used to set an empty model. * ! * @param model the underlying model to use, or {@code null} */ public void setModel(M model) { tableModel = model; setModelWrapper(new TableRowSorterModelWrapper()); } /** * Sets the object responsible for converting values from the ! * model to strings. If non-{@code null} this * is used to convert any object values, that do not have a ! * registered {@code Comparator}, to strings. * * @param stringConverter the object responsible for converting values * from the model to strings */ public void setStringConverter(TableStringConverter stringConverter) {
*** 197,217 **** public TableStringConverter getStringConverter() { return stringConverter; } /** ! * Returns the <code>Comparator</code> for the specified ! * column. If a <code>Comparator</code> has not been specified using ! * the <code>setComparator</code> method a <code>Comparator</code> * will be returned based on the column class ! * (<code>TableModel.getColumnClass</code>) of the specified column. ! * If the column class is <code>String</code>, ! * <code>Collator.getInstance</code> is returned. If the ! * column class implements <code>Comparable</code> a private ! * <code>Comparator</code> is returned that invokes the ! * <code>compareTo</code> method. Otherwise ! * <code>Collator.getInstance</code> is returned. * * @throws IndexOutOfBoundsException {@inheritDoc} */ public Comparator<?> getComparator(int column) { Comparator<?> comparator = super.getComparator(column); --- 197,217 ---- public TableStringConverter getStringConverter() { return stringConverter; } /** ! * Returns the {@code Comparator} for the specified ! * column. If a {@code Comparator} has not been specified using ! * the {@code setComparator} method a {@code Comparator} * will be returned based on the column class ! * ({@code TableModel.getColumnClass}) of the specified column. ! * If the column class is {@code String}, ! * {@code Collator.getInstance} is returned. If the ! * column class implements {@code Comparable} a private ! * {@code Comparator} is returned that invokes the ! * {@code compareTo} method. Otherwise ! * {@code Collator.getInstance} is returned. * * @throws IndexOutOfBoundsException {@inheritDoc} */ public Comparator<?> getComparator(int column) { Comparator<?> comparator = super.getComparator(column);
< prev index next >