List of usage examples for javax.swing.table TableRowSorter TableRowSorter
public TableRowSorter(M model)
TableRowSorter
using model
as the underlying TableModel
. From source file:NewFilterTable.java
public NewFilterTable() { setDefaultCloseOperation(EXIT_ON_CLOSE); String[] columns = { "ID", "Des", "Date", "Fixed" }; Object[][] rows = { { 1, "C", new Date(), new Date() }, { 2, "G", new Date(), new Date() }, { 5, "F", new Date(), new Date() } }; TableModel model = new DefaultTableModel(rows, columns); JTable table = new JTable(model); final TableRowSorter<TableModel> sorter; sorter = new TableRowSorter<TableModel>(model); table.setRowSorter(sorter);//from w w w .j a v a2s . co m getContentPane().add(new JScrollPane(table)); JPanel pnl = new JPanel(); pnl.add(new JLabel("Filter expression:")); final JTextField txtFE = new JTextField(25); pnl.add(txtFE); JButton btnSetFE = new JButton("Set Filter Expression"); ActionListener al; al = new ActionListener() { public void actionPerformed(ActionEvent e) { String expr = txtFE.getText(); sorter.setRowFilter(RowFilter.regexFilter(expr)); sorter.setSortKeys(null); } }; btnSetFE.addActionListener(al); pnl.add(btnSetFE); getContentPane().add(pnl, BorderLayout.SOUTH); setSize(750, 150); setVisible(true); }
From source file:SimpleTableModel.java
public Main() { this.setDefaultCloseOperation(EXIT_ON_CLOSE); JTable table = new JTable(new SimpleTableModel()); TableRowSorter sorter = new TableRowSorter(table.getModel()); table.setRowSorter(sorter);// www .j a v a 2 s .c o m RowFilter<SimpleTableModel, Integer> IDFilter = new RowFilter<SimpleTableModel, Integer>() { @Override public boolean include(Entry<? extends SimpleTableModel, ? extends Integer> entry) { SimpleTableModel model = entry.getModel(); int rowIndex = entry.getIdentifier().intValue(); Integer ID = (Integer) model.getValueAt(rowIndex, 0); if (ID.intValue() <= 100) { return false; // Do not show rows with an ID <= 100 } return true; } }; sorter.setRowFilter(IDFilter); Container contentPane = this.getContentPane(); contentPane.add(new JScrollPane(table), BorderLayout.CENTER); }
From source file:com.tiempometa.muestradatos.JReadTags.java
public JReadTags(Frame frame, boolean modal) { super(frame, modal); initComponents();// ww w . j a va2 s .c o m logger.debug("Adding self to tag read listeners"); ReaderContext.addReadingListener(this); tagReadTable.setModel(tagTableModel); tagReadTable.setAutoCreateRowSorter(true); TableRowSorter<TableModel> sorter = new TableRowSorter<>(tagReadTable.getModel()); sorter.setComparator(0, new BibComparator()); tagReadTable.setRowSorter(sorter); }
From source file:components.TableFilterDemo.java
public TableFilterDemo() { super();//from w w w . j a v a 2 s . c om setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); //Create a table with a sorter. MyTableModel model = new MyTableModel(); sorter = new TableRowSorter<MyTableModel>(model); table = new JTable(model); table.setRowSorter(sorter); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); //For the purposes of this example, better to have a single //selection. table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //When selection changes, provide user with row numbers for //both view and model. table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent event) { int viewRow = table.getSelectedRow(); if (viewRow < 0) { //Selection got filtered away. statusText.setText(""); } else { int modelRow = table.convertRowIndexToModel(viewRow); statusText.setText(String.format("Selected Row in view: %d. " + "Selected Row in model: %d.", viewRow, modelRow)); } } }); //Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); //Add the scroll pane to this panel. add(scrollPane); //Create a separate form for filterText and statusText JPanel form = new JPanel(new SpringLayout()); JLabel l1 = new JLabel("Filter Text:", SwingConstants.TRAILING); form.add(l1); filterText = new JTextField(); //Whenever filterText changes, invoke newFilter. filterText.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { newFilter(); } public void insertUpdate(DocumentEvent e) { newFilter(); } public void removeUpdate(DocumentEvent e) { newFilter(); } }); l1.setLabelFor(filterText); form.add(filterText); JLabel l2 = new JLabel("Status:", SwingConstants.TRAILING); form.add(l2); statusText = new JTextField(); l2.setLabelFor(statusText); form.add(statusText); SpringUtilities.makeCompactGrid(form, 2, 2, 6, 6, 6, 6); add(form); }
From source file:com.tiempometa.muestradatos.JReadTags.java
public JReadTags() { initComponents();//from w w w .jav a2s . c o m ReaderContext.addReadingListener(this); tagReadTable.setModel(tagTableModel); tagReadTable.setAutoCreateRowSorter(true); TableRowSorter<TableModel> sorter = new TableRowSorter<>(tagReadTable.getModel()); sorter.setComparator(0, new BibComparator()); tagReadTable.setRowSorter(sorter); }
From source file:TableRowColumnTest.java
public PlanetTableFrame() { setTitle("TableRowColumnTest"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); TableModel model = new DefaultTableModel(cells, columnNames) { public Class<?> getColumnClass(int c) { return cells[0][c].getClass(); }/*ww w . j ava 2 s . co m*/ }; table = new JTable(model); table.setRowHeight(100); table.getColumnModel().getColumn(COLOR_COLUMN).setMinWidth(250); table.getColumnModel().getColumn(IMAGE_COLUMN).setMinWidth(100); final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model); table.setRowSorter(sorter); sorter.setComparator(COLOR_COLUMN, new Comparator<Color>() { public int compare(Color c1, Color c2) { int d = c1.getBlue() - c2.getBlue(); if (d != 0) return d; d = c1.getGreen() - c2.getGreen(); if (d != 0) return d; return c1.getRed() - c2.getRed(); } }); sorter.setSortable(IMAGE_COLUMN, false); add(new JScrollPane(table), BorderLayout.CENTER); removedRowIndices = new HashSet<Integer>(); removedColumns = new ArrayList<TableColumn>(); final RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>() { public boolean include(Entry<? extends TableModel, ? extends Integer> entry) { return !removedRowIndices.contains(entry.getIdentifier()); } }; // create menu JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu selectionMenu = new JMenu("Selection"); menuBar.add(selectionMenu); rowsItem = new JCheckBoxMenuItem("Rows"); columnsItem = new JCheckBoxMenuItem("Columns"); cellsItem = new JCheckBoxMenuItem("Cells"); rowsItem.setSelected(table.getRowSelectionAllowed()); columnsItem.setSelected(table.getColumnSelectionAllowed()); cellsItem.setSelected(table.getCellSelectionEnabled()); rowsItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { table.clearSelection(); table.setRowSelectionAllowed(rowsItem.isSelected()); updateCheckboxMenuItems(); } }); selectionMenu.add(rowsItem); columnsItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { table.clearSelection(); table.setColumnSelectionAllowed(columnsItem.isSelected()); updateCheckboxMenuItems(); } }); selectionMenu.add(columnsItem); cellsItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { table.clearSelection(); table.setCellSelectionEnabled(cellsItem.isSelected()); updateCheckboxMenuItems(); } }); selectionMenu.add(cellsItem); JMenu tableMenu = new JMenu("Edit"); menuBar.add(tableMenu); JMenuItem hideColumnsItem = new JMenuItem("Hide Columns"); hideColumnsItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { int[] selected = table.getSelectedColumns(); TableColumnModel columnModel = table.getColumnModel(); // remove columns from view, starting at the last // index so that column numbers aren't affected for (int i = selected.length - 1; i >= 0; i--) { TableColumn column = columnModel.getColumn(selected[i]); table.removeColumn(column); // store removed columns for "show columns" command removedColumns.add(column); } } }); tableMenu.add(hideColumnsItem); JMenuItem showColumnsItem = new JMenuItem("Show Columns"); showColumnsItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { // restore all removed columns for (TableColumn tc : removedColumns) table.addColumn(tc); removedColumns.clear(); } }); tableMenu.add(showColumnsItem); JMenuItem hideRowsItem = new JMenuItem("Hide Rows"); hideRowsItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { int[] selected = table.getSelectedRows(); for (int i : selected) removedRowIndices.add(table.convertRowIndexToModel(i)); sorter.setRowFilter(filter); } }); tableMenu.add(hideRowsItem); JMenuItem showRowsItem = new JMenuItem("Show Rows"); showRowsItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { removedRowIndices.clear(); sorter.setRowFilter(filter); } }); tableMenu.add(showRowsItem); JMenuItem printSelectionItem = new JMenuItem("Print Selection"); printSelectionItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { int[] selected = table.getSelectedRows(); System.out.println("Selected rows: " + Arrays.toString(selected)); selected = table.getSelectedColumns(); System.out.println("Selected columns: " + Arrays.toString(selected)); } }); tableMenu.add(printSelectionItem); }
From source file:TableFilterDemo.java
public TableFilterDemo() { super();/*from w w w. jav a2s . c o m*/ setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); // Create a table with a sorter. MyTableModel model = new MyTableModel(); sorter = new TableRowSorter<MyTableModel>(model); table = new JTable(model); table.setRowSorter(sorter); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); // For the purposes of this example, better to have a single // selection. table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // When selection changes, provide user with row numbers for // both view and model. table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent event) { int viewRow = table.getSelectedRow(); if (viewRow < 0) { // Selection got filtered away. statusText.setText(""); } else { int modelRow = table.convertRowIndexToModel(viewRow); statusText.setText(String.format("Selected Row in view: %d. " + "Selected Row in model: %d.", viewRow, modelRow)); } } }); // Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); // Add the scroll pane to this panel. add(scrollPane); // Create a separate form for filterText and statusText JPanel form = new JPanel(new SpringLayout()); JLabel l1 = new JLabel("Filter Text:", SwingConstants.TRAILING); form.add(l1); filterText = new JTextField(); // Whenever filterText changes, invoke newFilter. filterText.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { newFilter(); } public void insertUpdate(DocumentEvent e) { newFilter(); } public void removeUpdate(DocumentEvent e) { newFilter(); } }); l1.setLabelFor(filterText); form.add(filterText); JLabel l2 = new JLabel("Status:", SwingConstants.TRAILING); form.add(l2); statusText = new JTextField(); l2.setLabelFor(statusText); form.add(statusText); SpringUtilities.makeCompactGrid(form, 2, 2, 6, 6, 6, 6); add(form); }
From source file:dmh.kuebiko.view.NoteTable.java
NoteTable(NoteTableModel noteTableModel) { this.noteTableModel = noteTableModel; setModel(noteTableModel);/*w ww .j av a 2 s. c o m*/ sorter = new TableRowSorter<NoteTableModel>(this.noteTableModel); setRowSorter(sorter); }
From source file:com.emental.mindraider.ui.outline.OutlineSorterJPanel.java
public OutlineSorterJPanel() { setLayout(new BorderLayout()); // table with archived concepts (title) // let table model to load discarded concepts itself tableModel = new SorterTableModel(); table = new JTable(tableModel); table.getSelectionModel().addListSelectionListener(new SorterListSelectionListener(table, tableModel)); table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.setFillsViewportHeight(true);// ww w . j a va2s .c om table.setShowHorizontalLines(false); table.setShowVerticalLines(false); table.getColumnModel().getColumn(COLUMN_NAME).setPreferredWidth(150); table.getColumnModel().getColumn(COLUMN_ANNOTATION).setPreferredWidth(220); table.getColumnModel().getColumn(COLUMN_CREATED).setPreferredWidth(60); table.getColumnModel().getColumn(COLUMN_MODIFIED).setPreferredWidth(60); table.getColumnModel().getColumn(COLUMN_REVISION).setPreferredWidth(35); TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel()); table.setRowSorter(sorter); sorter.setComparator(COLUMN_REVISION, new Comparator<Long>() { @Override public int compare(Long o1, Long o2) { return o2.intValue() - o1.intValue(); } }); final Comparator<String> timestampComparator = new Comparator<String>() { @Override public int compare(String o1, String o2) { if (OutlineTreeInstance.getCreatedTimestampFromHtml(o2) > OutlineTreeInstance .getCreatedTimestampFromHtml(o1)) { return 1; } else { if (OutlineTreeInstance.getCreatedTimestampFromHtml(o2) == OutlineTreeInstance .getCreatedTimestampFromHtml(o1)) { return 0; } else { return -1; } } } }; sorter.setComparator(COLUMN_MODIFIED, timestampComparator); sorter.setComparator(COLUMN_CREATED, timestampComparator); JScrollPane scroll = new JScrollPane(table); scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); add(scroll, BorderLayout.CENTER); }
From source file:be.ac.ua.comp.scarletnebula.gui.addserverwizard.ChooseImagePage.java
private final JPanel getAllImagesPanel(final CloudProvider provider) { final JPanel panel = new JPanel(new BorderLayout()); final TableRowSorter<MachineImageTableModel> sorter = new TableRowSorter<MachineImageTableModel>( allImagesModel);//from w ww .j a va2 s . c o m allImagesTable.setRowSorter(sorter); allImagesTable.setFillsViewportHeight(true); allImagesTable.addMouseListener(new SmartImageModelContextMenuMouseListener(provider, allImagesModel, allImagesTable, favoriteImagesPanel.getModel())); final JPanel aboveTable = new JPanel(new BorderLayout()); final JPanel searchPanel = getSearchPanel(sorter); aboveTable.add(searchPanel, BorderLayout.NORTH); aboveTable.add(throbberPanel, BorderLayout.SOUTH); panel.add(aboveTable, BorderLayout.NORTH); final JScrollPane tableScrollPane = new JScrollPane(allImagesTable); tableScrollPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(5, 20, 10, 20), BorderFactory.createBevelBorder(BevelBorder.LOWERED))); panel.add(tableScrollPane, BorderLayout.CENTER); return panel; }