Author: tchemit Date: 2013-10-11 15:04:20 +0200 (Fri, 11 Oct 2013) New Revision: 2739 Url: http://nuiton.org/projects/jaxx/repository/revisions/2739 Log: fixes #2874: Add some nice methods to select rows and columns from jtable Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/SwingUtil.java Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/SwingUtil.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/SwingUtil.java 2013-10-08 13:27:38 UTC (rev 2738) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/SwingUtil.java 2013-10-11 13:04:20 UTC (rev 2739) @@ -589,8 +589,7 @@ * Try to load the Nimbus look and feel. * <p/> * - * @throws UnsupportedLookAndFeelException - * if nimbus is not applicable + * @throws UnsupportedLookAndFeelException if nimbus is not applicable * @throws ClassNotFoundException * @throws InstantiationException * @throws IllegalAccessException @@ -722,6 +721,90 @@ } /** + * Return the selected rows of the table in the model coordinate or empty + * array if selection is empty. + * + * @param table the table to seek + * @return the selected rows of the table in the model coordinate or empty + * array if selection is empty. + * @since 2.5.29 + */ + public static int[] getSelectedModelRows(JTable table) { + int[] selectedRows = table.getSelectedRows(); + int length = selectedRows.length; + int[] result = new int[length]; + for (int i = 0; i < length; i++) { + int selectedRow = selectedRows[i]; + result[i] = table.convertRowIndexToModel(selectedRow); + } + return result; + } + + /** + * Return the selected row of the table in the model coordinate or + * {@code -1} if selection is empty. + * + * @param table the table to seek + * @return the selected row of the table in the model coordinate or + * {@code -1} if selection is empty. + * @since 2.5.29 + */ + public static int getSelectedModelRow(JTable table) { + int result = table.getSelectedRow(); + if (result != -1) { + // can convert to model coordinate + result = table.convertRowIndexToModel(result); + } + return result; + } + + /** + * Return the selected column of the table in the model coordinate or + * {@code -1} if selection is empty. + * + * @param table the table to seek + * @return the selected column of the table in the model coordinate or + * {@code -1} if selection is empty. + * @since 2.5.29 + */ + public static int getSelectedModelColumn(JTable table) { + int result = table.getSelectedColumn(); + if (result != -1) { + // can convert to model coordinate + result = table.convertColumnIndexToModel(result); + } + return result; + } + + /** + * Select the given row index {@code rowIndex} (from the model coordinate) + * in the selection of the given table. + * + * @param table the table where to set the selection + * @param rowIndex the row index in the model coordinate to set as selection + * @since 2.5.29 + */ + public static void setSelectionInterval(JTable table, int rowIndex) { + + int rowViewIndex = table.convertRowIndexToView(rowIndex); + table.getSelectionModel().setSelectionInterval(rowViewIndex, rowViewIndex); + } + + /** + * Add the given row index {@code rowIndex} (from the model coordinate) + * in the selection of the given table. + * + * @param table the table where to set the selection + * @param rowIndex the row index in the model coordinate to add to selection + * @since 2.5.29 + */ + public static void addRowSelectionInterval(JTable table, int rowIndex) { + + int rowViewIndex = table.convertRowIndexToView(rowIndex); + table.getSelectionModel().addSelectionInterval(rowViewIndex, rowViewIndex); + } + + /** * A simple iterator on a {@link JTabbedPane}. * <p/> * Implements the method {@link #get(int, Component)} to obtain @@ -1298,7 +1381,7 @@ * @param row row index of cell to editing * @param colummn column index of cell to editing * @return {@code false} if for any reason the cell cannot be edited, - * or if the indices are invalid + * or if the indices are invalid */ public static boolean editCell(JTable table, int row, int colummn) {
participants (1)
-
tchemit@users.nuiton.org