Java Code Examples for javax.swing.table.TableRowSorter#setSortKeys()

The following examples show how to use javax.swing.table.TableRowSorter#setSortKeys() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TableViewTopComponent.java    From constellation with Apache License 2.0 5 votes vote down vote up
/**
 * Assign a new filter while keeping the sort keys.
 */
private void setNewFilter() {
    final RowSorter<? extends TableModel> oldSorter = dataTable.getRowSorter();
    final GraphTableModel gtm = (GraphTableModel) dataTable.getModel();
    final TableRowSorter<GraphTableModel> sorter = new TableRowSorter<>(gtm);
    sorter.setSortKeys(oldSorter.getSortKeys());
    if (selectedOnlyButton.isSelected()) {
        sorter.setRowFilter(new SelectionRowFilter(graphNode.getGraph(), currentElementType));
    }
    dataTable.setRowSorter(sorter);
}
 
Example 2
Source File: TableViewTopComponent.java    From constellation with Apache License 2.0 5 votes vote down vote up
/**
 * Assign a new table model and filter while keeping the sort keys.
 */
private void setNewModel() {
    final RowSorter<? extends TableModel> oldSorter = dataTable.getRowSorter();
    final GraphTableModel gtm = new GraphTableModel(graphNode.getGraph(), currentElementType);
    final TableRowSorter<GraphTableModel> sorter = new TableRowSorter<>(gtm);
    sorter.setSortKeys(oldSorter.getSortKeys());
    if (selectedOnlyButton.isSelected()) {
        sorter.setRowFilter(new SelectionRowFilter(graphNode.getGraph(), currentElementType));
    }
    dataTable.setModel(gtm);
    dataTable.setRowSorter(sorter);
}
 
Example 3
Source File: SyncPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void initTableSorter() {
    TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel);
    itemTable.setRowSorter(sorter);
    // default sort keys
    sorter.setSortKeys(sortKeys);
    // sorting
    sorter.setComparator(0, new SyncItemImageIconComparator());
    sorter.setSortable(2, false);
}
 
Example 4
Source File: PanamaHitek_DataBuffer.java    From PanamaHitek_Arduino with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Permite ordenar los datos en una columna de la tabla que genera la clase
 *
 * @param column La columna que se desea ordenar
 * @param ascending si se desea un orden ascendente o descendente
 *
 * @since 3.0.3
 */
public void sortColumn(int column, boolean ascending) {
    TableRowSorter<TableModel> sorter = new TableRowSorter<>(table.getModel());
    table.setRowSorter(sorter);
    List<RowSorter.SortKey> sortKeys = new ArrayList<>();

    if (ascending) {
        sortKeys.add(new RowSorter.SortKey(column, SortOrder.ASCENDING));
    } else {
        sortKeys.add(new RowSorter.SortKey(column, SortOrder.DESCENDING));
    }

    sorter.setSortKeys(sortKeys);
    sorter.sort();
}
 
Example 5
Source File: HighlightTable.java    From jmeter-debugger with Apache License 2.0 5 votes vote down vote up
private void setSorter(TableModel model) {
    TableRowSorter<TableModel> sorter = new TableRowSorter<>(model);
    sorter.setSortsOnUpdates(true);
    LinkedList<RowSorter.SortKey> sortKeys = new LinkedList<>();
    sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
    sorter.setSortKeys(sortKeys);
    sorter.sort();
    setRowSorter(sorter);
}
 
Example 6
Source File: LobbyGameTable.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
LobbyGameTable(final LobbyGameTableModel gameTableModel) {
  super(gameTableModel);
  final TableRowSorter<LobbyGameTableModel> tableSorter = new TableRowSorter<>(gameTableModel);
  // by default, sort by host
  final int hostColumn = gameTableModel.getColumnIndex(LobbyGameTableModel.Column.Host);
  tableSorter.setSortKeys(List.of(new RowSorter.SortKey(hostColumn, SortOrder.DESCENDING)));
  setRowSorter(tableSorter);
}
 
Example 7
Source File: ProductListView.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
protected Component createListSelector() {
   TableModel<ProductModel> tableModel = createTableModel();
   TableRowSorter<TableModel<ProductModel>> sorter = new TableRowSorter<>(tableModel);
   sorter.setSortKeys(ImmutableList.<SortKey>of(
         new RowSorter.SortKey(0, SortOrder.UNSORTED),
         new RowSorter.SortKey(1, SortOrder.UNSORTED)
   ));
   Table<ProductModel> table = new Table<>(tableModel);
   table.setRowSorter(sorter);
   table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
   table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
      @Override
      public void valueChanged(ListSelectionEvent e) {
         if(e.getValueIsAdjusting()) {
            return;
         }
         int selected = table.getSelectedRow();
         if(selected == -1) {
            selectionModel.clearSelection();
         }
         else {
            selected = table.getRowSorter().convertRowIndexToModel(selected);
            selectionModel.setSelection(table.getModel().getValue(selected));
         }
      }
   });
   // TODO wrap this into a selection listener
   table.addMouseListener(new MouseAdapter() {
      @Override
      public void mousePressed(MouseEvent me) {
         if (me.getClickCount() == 2) {
            Point p = me.getPoint();
            int row = table.rowAtPoint(p);
            int offset = table.convertRowIndexToModel(row);
            ProductModel model = table.getModel().getValue(offset);
            controller.getProductSelection().setSelection(model);
         }
     }
   });
   return new JScrollPane(table);
}
 
Example 8
Source File: bug6894632.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static void test(final List<RowSorter.SortKey> sortKeys) {
    final JFrame frame = new JFrame();
    try {
        frame.setUndecorated(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        table = new JTable();
        DefaultTableModel tableModel =
                new DefaultTableModel(10, 1) {
                    public Object getValueAt(int row, int column) {
                        return row == getRowCount() - 1 ? row + "==last" :
                                row;
                    }
                };
        table.setModel(tableModel);
        TableRowSorter<TableModel> sorter =
                new TableRowSorter<TableModel>(tableModel);
        sorter.setSortKeys(sortKeys);
        table.setRowSorter(sorter);

        frame.setContentPane(table);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        int lastRow = table.getRowCount() - 1;

        //select last row
        table.setRowSelectionInterval(lastRow, lastRow);

        //remove row above the last
        tableModel.removeRow(lastRow - 1);
        lastRow = table.getRowCount() - 1;
        if (lastRow != table.getSelectedRow()) {
            throw new RuntimeException("last row must be still selected");
        }

        //sort table
        sortKeys.clear();
        sortKeys.add(0, new RowSorter.SortKey(0, SortOrder.ASCENDING));
        sorter.setSortKeys(sortKeys);
        //remove row above the last
        lastRow = table.getRowCount() - 1;
        tableModel.removeRow(lastRow - 1);

        if (!table.getValueAt(table.getSelectedRow(), 0).toString()
                .endsWith("==last")) {
            throw new RuntimeException(
                    "row ends with \"==last\" row must be still selected");
        }

        //make table unsorted again
        sortKeys.clear();
        sortKeys.add(0, new RowSorter.SortKey(0, SortOrder.UNSORTED));
        sorter.setSortKeys(sortKeys);
        //remove row above the last
        lastRow = table.getRowCount() - 1;
        tableModel.removeRow(lastRow - 1);

        lastRow = table.getRowCount() - 1;
        if (lastRow != table.getSelectedRow()) {
            throw new RuntimeException(
                    "last row must be still selected");
        }
    } finally {
        frame.dispose();
    }
}
 
Example 9
Source File: SearchResultsFrame.java    From Zettelkasten with GNU General Public License v3.0 4 votes vote down vote up
/**
 * This method initializes the table.<br><br>
 * - it puts the tab-key as new traversal-key<br>
 * - sets the autosorter<br>
 * - displayes the cellgrid<br>
 * - implements action- and selection-listeners
 */
private void initTable() {
    // usually, the tab key selects the next cell in a jTable. here we override this
    // setting, changing the tab-key to change the focus.

    // bind our new forward focus traversal keys
    Set<AWTKeyStroke> newForwardKeys = new HashSet<>(1);
    newForwardKeys.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB,0));
    jTableResults.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,Collections.unmodifiableSet(newForwardKeys));
    // bind our new backward focus traversal keys
    Set<AWTKeyStroke> newBackwardKeys = new HashSet<>(1);
    newBackwardKeys.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB,KeyEvent.SHIFT_MASK+KeyEvent.SHIFT_DOWN_MASK));
    jTableResults.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,Collections.unmodifiableSet(newBackwardKeys));
    // create new table sorter
    TableRowSorter<TableModel> sorter = new TableRowSorter<>();
    // tell tgis jtable that it has an own sorter
    jTableResults.setRowSorter(sorter);
    // and tell the sorter, which table model to sort.
    sorter.setModel((DefaultTableModel)jTableResults.getModel());
    // in this table, the first column needs a custom comparator.
    try {
        // sorter for titles
        sorter.setComparator(1,new Comparer());
        // sorter for desktop names
        sorter.setComparator(5,new Comparer());
        // this table has two more columns that should be sorted, the columns with
        // the entries timestamps.
        sorter.setComparator(2,new DateComparer());
        sorter.setComparator(3,new DateComparer());
    }
    catch (IndexOutOfBoundsException e) {
        Constants.zknlogger.log(Level.WARNING,e.getLocalizedMessage());
    }
    // get last table sorting
    RowSorter.SortKey sk = settingsObj.getTableSorting(jTableResults);
    // any sorting found?
    if (sk != null) {
        // create array with sort key
        ArrayList l = new ArrayList();
        l.add(sk);
        // set sort key to table
        sorter.setSortKeys(l);
        // sort table
        sorter.sort();
    }
    jTableResults.setShowHorizontalLines(settingsObj.getShowGridHorizontal());
    jTableResults.setShowVerticalLines(settingsObj.getShowGridVertical());
    jTableResults.setIntercellSpacing(settingsObj.getCellSpacing());
    jTableResults.getTableHeader().setReorderingAllowed(false);
    // if the user wants to see grids, we need to change the gridcolor on mac-aqua
    jTableResults.setGridColor(settingsObj.getTableGridColor());
    SelectionListener listener = new SelectionListener(jTableResults);
    jTableResults.getSelectionModel().addListSelectionListener(listener);
    jTableResults.getColumnModel().getSelectionModel().addListSelectionListener(listener);
}
 
Example 10
Source File: CruciblePanel.java    From Crucible4IDEA with MIT License 4 votes vote down vote up
public CruciblePanel(@NotNull final Project project) {
  super(false);
  myProject = project;

  final JBSplitter splitter = new JBSplitter(false, 0.2f);

  myReviewModel = new CrucibleReviewModel(project);
  myReviewTable = new JBTable(myReviewModel);
  myReviewTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  myReviewTable.setStriped(true);
  myReviewTable.setExpandableItemsEnabled(false);

  final TableColumnModel columnModel = myReviewTable.getColumnModel();
  columnModel.getColumn(1).setCellRenderer(new DescriptionCellRenderer());

  setUpColumnWidths(myReviewTable);
  myReviewTable.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
      if (e.getClickCount() == 2) {
        final int viewRow = myReviewTable.getSelectedRow();
        if (viewRow >= 0 &&  viewRow < myReviewTable.getRowCount()) {
          ApplicationManager.getApplication().invokeLater(new Runnable() {
            @Override
            public void run() {
              final Review review =
                CrucibleManager.getInstance(myProject).getDetailsForReview((String)myReviewTable.
                  getValueAt(viewRow, myReviewTable.getColumnModel().getColumnIndex(CrucibleBundle.message("crucible.id"))));
              if (review != null) {
                openDetailsToolWindow(review);
                myReviewTable.clearSelection();
              }
            }
          }, ModalityState.stateForComponent(myReviewTable));

        }
      }
  }});

  final TableRowSorter<TableModel> rowSorter = new TableRowSorter<TableModel>(myReviewModel);
  rowSorter.setSortKeys(Collections.singletonList(new RowSorter.SortKey(4, SortOrder.ASCENDING)));
  rowSorter.sort();
  myReviewTable.setRowSorter(rowSorter);

  final JScrollPane detailsScrollPane = ScrollPaneFactory.createScrollPane(myReviewTable);

  final SimpleTreeStructure reviewTreeStructure = createTreeStructure();
  final DefaultTreeModel model = new CrucibleTreeModel();
  final SimpleTree reviewTree = new SimpleTree(model);

  new AbstractTreeBuilder(reviewTree, model, reviewTreeStructure, null);
  reviewTree.invalidate();

  final JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(reviewTree);
  splitter.setFirstComponent(scrollPane);
  splitter.setSecondComponent(detailsScrollPane);
  setContent(splitter);
}