Java Code Examples for javax.swing.table.TableRowSorter

The following examples show how to use javax.swing.table.TableRowSorter. These examples are extracted from open source projects. 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 Project: PacketProxy   Source File: GUIOptionCharSetDialog.java    License: Apache License 2.0 6 votes vote down vote up
private JScrollPane tableScrollPane(){
	table_model = new CharSetsTableModel(getTableDataWithAvailableCharsets(), columns);
	JTable table = new JTable(table_model);
	TableColumn col = table.getColumnModel().getColumn(0);
	col.setMinWidth(50);
	col.setMaxWidth(50);
	table.addMouseListener(new MouseAdapter() {
		@Override
		public void mousePressed(MouseEvent e) {
			super.mousePressed(e);
			if(0==table.getSelectedColumn()){
				return;
			}
			table.setValueAt(!(Boolean)table.getValueAt(table.getSelectedRow(), 0), table.getSelectedRow(),0);
		}
	});
	sorter = new TableRowSorter<CharSetsTableModel>(table_model);
	table.setRowSorter(sorter);
	JScrollPane jscrollPane = new JScrollPane(table);

	return jscrollPane;
}
 
Example 2
Source Project: mzmine3   Source File: PeakListTableWindow.java    License: GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public void updateFilter() {

  List<RowFilter<Object, Object>> rowSorters = new ArrayList<RowFilter<Object, Object>>();

  TableRowSorter<PeakListTableModel> sorter =
      (TableRowSorter<PeakListTableModel>) table.getRowSorter();

  String textId = "(?i)" + filterTextId.getText();
  String textIdentity = "(?i)" + filterTextIdentity.getText();
  String textComment = "(?i)" + filterTextComment.getText();

  rowSorters.add(RowFilter.regexFilter(textId, 0));
  rowSorters.add(mzFilter);
  rowSorters.add(rtFilter);
  rowSorters.add(RowFilter.regexFilter(textIdentity, 3));
  rowSorters.add(RowFilter.regexFilter(textComment, 4));

  sorter.setRowFilter(RowFilter.andFilter(rowSorters));
  table.setRowSorter(sorter);

}
 
Example 3
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 6 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  table.setRowSorter(new TableRowSorter<>(model));

  JScrollPane scrollPane = new JScrollPane(table);
  scrollPane.getViewport().setBackground(Color.WHITE);
  table.setComponentPopupMenu(new TablePopupMenu());
  table.setFillsViewportHeight(true);
  table.setIntercellSpacing(new Dimension());
  table.setShowGrid(false);
  table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

  TableColumn column = table.getColumnModel().getColumn(0);
  column.setMaxWidth(60);
  column.setMinWidth(60);
  column.setResizable(false);

  JButton button = new JButton("add");
  button.addActionListener(e -> addActionPerformed());
  add(button, BorderLayout.SOUTH);
  add(scrollPane);
  setPreferredSize(new Dimension(320, 240));
}
 
Example 4
Source Project: knife   Source File: ConfigTable.java    License: MIT License 6 votes vote down vote up
private void addClickSort() {
	TableRowSorter<ConfigTableModel> sorter = new TableRowSorter<ConfigTableModel>(ConfigTableModel);
	ConfigTable.this.setRowSorter(sorter);

	JTableHeader header = this.getTableHeader();
	header.addMouseListener(new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent e) {
			try {
				//ConfigTable.this.getRowSorter().getSortKeys().get(0).getColumn();
				sorter.getSortKeys().get(0).getColumn();
				////当Jtable中无数据时,jtable.getRowSorter()是null
			} catch (Exception e1) {
				e1.printStackTrace(new PrintWriter(BurpExtender.callbacks.getStderr(), true));//working?
			}
		}
	});
}
 
Example 5
Source Project: netbeans   Source File: ProfilerTreeTable.java    License: Apache License 2.0 6 votes vote down vote up
protected TableRowSorter createRowSorter() {
        ProfilerRowSorter s = new ProfilerTreeTableSorter(getModel()) {
            public void allRowsChanged() {
                // Must invoke later, JTree.getRowCount() not ready yet
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() { updateColumnsPreferredWidth(); }
                });
            }
            protected void setSortKeysImpl(List newKeys) {
                // TODO: Improve to not call createComparator(newKeys) here and from super
                willBeSorted(Collections.unmodifiableList(newKeys));
                super.setSortKeysImpl(newKeys);
            }
        };
        s.setDefaultSortOrder(SortOrder.DESCENDING);
        s.setDefaultSortOrder(0, SortOrder.ASCENDING);
//        s.setSortColumn(0);
        return s;
    }
 
Example 6
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 6 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  String[] empty = {"", "", ""};
  String[] columnNames = {"A", "B", "C"};
  Object[][] data = {
    {"aaa", "fff", "ggg"}, {"jjj", "ppp", "ooo"},
    {"bbb", "eee", "hhh"}, {"kkk", "qqq", "nnn"},
    {"ccc", "ddd", "iii"}, {"lll", "rrr", "mmm"},
    empty, empty, empty, empty, empty, empty
  };
  TableModel model = new DefaultTableModel(data, columnNames) {
    @Override public Class<?> getColumnClass(int column) {
      return String.class;
    }
  };
  JTable table = new JTable(model);
  table.setAutoCreateRowSorter(true);
  TableRowSorter<? extends TableModel> sorter = (TableRowSorter<? extends TableModel>) table.getRowSorter();
  IntStream.range(0, 3).forEach(i -> sorter.setComparator(i, new RowComparator(table, i)));

  add(new JScrollPane(table));
  setPreferredSize(new Dimension(320, 240));
}
 
Example 7
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 6 votes vote down vote up
private void deleteActionPerformed() {
  int[] selection = table.getSelectedRows();
  if (selection.length == 0) {
    return;
  }
  for (int i: selection) {
    int mi = table.convertRowIndexToModel(i);
    deletedRowSet.add(mi);
    SwingWorker<?, ?> worker = getSwingWorker(mi);
    if (Objects.nonNull(worker) && !worker.isDone()) {
      worker.cancel(true);
      // executor.remove(worker);
    }
    // worker = null;
  }
  RowSorter<? extends TableModel> sorter = table.getRowSorter();
  ((TableRowSorter<? extends TableModel>) sorter).setRowFilter(new RowFilter<TableModel, Integer>() {
    @Override public boolean include(Entry<? extends TableModel, ? extends Integer> entry) {
      return !deletedRowSet.contains(entry.getIdentifier());
    }
  });
  table.clearSelection();
  table.repaint();
}
 
Example 8
Source Project: littleluck   Source File: DefaultsDisplay.java    License: Apache License 2.0 6 votes vote down vote up
protected JComponent createFilterControl() {
          
    onlyVisualsCheckBox = new JCheckBox("Show Only Visual Defaults");
    onlyVisualsCheckBox.setSelected(true);
    onlyVisualsCheckBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            boolean showOnlyVisuals = onlyVisualsCheckBox.isSelected();
            for(int i = 0; i < tabPane.getTabCount() ; i++) {
                JScrollPane scrollpane = (JScrollPane)tabPane.getComponentAt(i);
                JTable table = (JTable)scrollpane.getViewport().getView();
                TableRowSorter sorter = (TableRowSorter)table.getRowSorter();
                sorter.setRowFilter(showOnlyVisuals? visualsFilter : null);                    
            }
        }
    });
    return onlyVisualsCheckBox;
}
 
Example 9
Source Project: littleluck   Source File: DefaultsDisplay.java    License: Apache License 2.0 6 votes vote down vote up
protected void initFilters(JTable table) {
    TableRowSorter sorter = new TableRowSorter(table.getModel());
    table.setRowSorter(sorter);
    
    if (visualsFilter == null) {
        visualsFilter = new RowFilter<UIDefaultsTableModel,Integer>() {
            public boolean include(Entry<? extends UIDefaultsTableModel, ? extends Integer> entry) {
                UIDefaultsTableModel model = entry.getModel();
                Object defaultsValue = model.getValueAt(entry.getIdentifier().intValue(),
                        UIDefaultsTableModel.VALUE_COLUMN);

                return defaultsValue instanceof Color ||
                        defaultsValue instanceof Font ||
                        defaultsValue instanceof Icon;
            }
        };
    }
    
    if (onlyVisualsCheckBox.isSelected()) {
        sorter.setRowFilter(visualsFilter);
    }
}
 
Example 10
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 6 votes vote down vote up
private void deleteActionPerformed() {
  int[] selection = table.getSelectedRows();
  if (selection.length == 0) {
    return;
  }
  for (int i: selection) {
    int mi = table.convertRowIndexToModel(i);
    deletedRowSet.add(mi);
    SwingWorker<?, ?> worker = getSwingWorker(mi);
    if (Objects.nonNull(worker) && !worker.isDone()) {
      worker.cancel(true);
      // executor.remove(worker);
    }
    // worker = null;
  }
  RowSorter<? extends TableModel> sorter = table.getRowSorter();
  ((TableRowSorter<? extends TableModel>) sorter).setRowFilter(new RowFilter<TableModel, Integer>() {
    @Override public boolean include(Entry<? extends TableModel, ? extends Integer> entry) {
      return !deletedRowSet.contains(entry.getIdentifier());
    }
  });
  table.clearSelection();
  table.repaint();
}
 
Example 11
Source Project: iBioSim   Source File: SBOLInputDialog.java    License: Apache License 2.0 6 votes vote down vote up
private void updateFilter(String filterText) 
{
	filterText = "(?i)" + filterText;
	@SuppressWarnings({ "rawtypes", "unchecked" })
	TableRowSorter<TopLevelTableModel> sorter = (TableRowSorter) table.getRowSorter();
	if (filterText.length() == 0) {
		sorter.setRowFilter(null);
	} else {
		try {
			RowFilter<TopLevelTableModel, Object> rf = RowFilter.regexFilter(filterText, 0, 1);
			sorter.setRowFilter(rf);
		} catch (java.util.regex.PatternSyntaxException e) {
			sorter.setRowFilter(null);
		}
	}

	tableLabel.setText("Matching parts (" + sorter.getViewRowCount() + ")");
}
 
Example 12
Source Project: azure-devops-intellij   Source File: ImportForm.java    License: MIT License 6 votes vote down vote up
public void setTeamProjectTable(final ServerContextTableModel tableModel, final ListSelectionModel selectionModel) {
    teamProjectTable.setModel(tableModel);
    teamProjectTable.setSelectionModel(selectionModel);

    // Setup table sorter
    final RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel);
    teamProjectTable.setRowSorter(sorter);

    // Attach an index converter to fix the indexes if the user sorts the list
    tableModel.setSelectionConverter(new TableModelSelectionConverter() {
        @Override
        public int convertRowIndexToModel(int viewRowIndex) {
            if (viewRowIndex >= 0) {
                return teamProjectTable.convertRowIndexToModel(viewRowIndex);
            }

            return viewRowIndex;
        }
    });
}
 
Example 13
public Discrete1BandTabularForm(ColorManipulationForm parentForm) {
    this.parentForm = parentForm;
    tableModel = new ImageInfoTableModel();
    moreOptionsForm = new MoreOptionsForm(this, false);

    final JTable table = new JTable(tableModel);
    table.setRowSorter(new TableRowSorter<>(tableModel));
    table.setDefaultRenderer(Color.class, new ColorTableCellRenderer());
    table.setDefaultEditor(Color.class, new ColorTableCellEditor());
    table.getTableHeader().setReorderingAllowed(false);
    table.getColumnModel().getColumn(1).setPreferredWidth(140);
    table.getColumnModel().getColumn(3).setCellRenderer(new PercentageRenderer());

    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    final JScrollPane tableScrollPane = new JScrollPane(table);
    tableScrollPane.getViewport().setPreferredSize(table.getPreferredSize());
    contentPanel = tableScrollPane;
}
 
Example 14
Source Project: binnavi   Source File: CTrackingResultsTable.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new results tracking table.
 *
 * @param container Results container from where the results can be read.
 */
public CTrackingResultsTable(final CTrackingResultContainer container) {
  m_container =
      Preconditions.checkNotNull(container, "IE01691: Container argument can not be null");

  m_model = new CTrackingResultsTableModel(container);
  setModel(m_model);

  m_sorter = new TableRowSorter<TableModel>(m_model);
  setRowSorter(m_sorter);
  m_sorter.setComparator(ADDRESS_COLUMN, new LexicalComparator());

  // Make sure that the status cells are colored.
  getColumnModel().getColumn(STATUS_COLUMN).setCellRenderer(new CStatusColumnRenderer());
  getColumnModel().getColumn(INSTRUCTION_COLUMN).setCellRenderer(new CMonospaceRenderer());

  final InternalSelectionListener listener = new InternalSelectionListener();

  getSelectionModel().addListSelectionListener(listener);
  addMouseListener(listener);
}
 
Example 15
private void updateDataTable() {
    dataTable = new JTable();
    dataTable.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    if(data != null) {
        dataModel = new UmbelConceptTableModel(data);
        dataTable.setModel(dataModel);
        dataTable.setRowSorter(new TableRowSorter(dataModel));

        dataTable.setColumnSelectionAllowed(false);
        dataTable.setRowSelectionAllowed(true);
        dataTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        TableColumn column = null;
        for (int i=0; i < dataTable.getColumnCount(); i++) {
            column = dataTable.getColumnModel().getColumn(i);
            column.setPreferredWidth(dataModel.getColumnWidth(i));
        }
        tableScrollPane.setViewportView(dataTable);
    }
}
 
Example 16
Source Project: beautyeye   Source File: DefaultsDisplay.java    License: Apache License 2.0 6 votes vote down vote up
protected void initFilters(JTable table) {
    TableRowSorter sorter = new TableRowSorter(table.getModel());
    table.setRowSorter(sorter);
    
    if (visualsFilter == null) {
        visualsFilter = new RowFilter<UIDefaultsTableModel,Integer>() {
            public boolean include(Entry<? extends UIDefaultsTableModel, ? extends Integer> entry) {
                UIDefaultsTableModel model = entry.getModel();
                Object defaultsValue = model.getValueAt(entry.getIdentifier().intValue(),
                        UIDefaultsTableModel.VALUE_COLUMN);

                return defaultsValue instanceof Color ||
                        defaultsValue instanceof Font ||
                        defaultsValue instanceof Icon;
            }
        };
    }
    
    if (onlyVisualsCheckBox.isSelected()) {
        sorter.setRowFilter(visualsFilter);
    }
}
 
Example 17
Source Project: RipplePower   Source File: ReceiveAddressDialog.java    License: Apache License 2.0 6 votes vote down vote up
public ReceiveAddressDialog(JDialog parent) {
	super(parent, "Receive Addresses", Dialog.ModalityType.DOCUMENT_MODAL);
	setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	tableModel = new AddressTableModel(columnNames, columnClasses);
	table = new AddressTable(tableModel, columnTypes);
	table.setRowSorter(new TableRowSorter<>(tableModel));
	table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	scrollPane = new JScrollPane(table);
	JPanel tablePane = new JPanel();
	tablePane.setBackground(Color.WHITE);
	tablePane.add(Box.createGlue());
	tablePane.add(scrollPane);
	tablePane.add(Box.createGlue());
	JPanel buttonPane = new ButtonPane(this, 10, new String[] { "New", "new" }, new String[] { "Copy", "copy" },
			new String[] { "Edit", "edit" }, new String[] { "Done", "done" });
	buttonPane.setBackground(Color.white);
	JPanel contentPane = new JPanel();
	contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
	contentPane.setOpaque(true);
	contentPane.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
	contentPane.setBackground(Color.WHITE);
	contentPane.add(tablePane);
	contentPane.add(buttonPane);
	setContentPane(contentPane);
}
 
Example 18
Source Project: pcgen   Source File: JTableEx.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void setAutoCreateRowSorter(boolean autoCreateRowSorter)
{
	boolean oldValue = this.sortingEnabled;
	this.sortingEnabled = autoCreateRowSorter;
	if (sortingEnabled)
	{
		TableModel model = getModel();
		if (model instanceof SortableTableModel)
		{
			setRowSorter(new SortableTableRowSorter((SortableTableModel) dataModel));
		}
		else
		{
			setRowSorter(new TableRowSorter<>(model));
		}
	}
	firePropertyChange("autoCreateRowSorter", oldValue, autoCreateRowSorter);
}
 
Example 19
Source Project: Juicebox   Source File: EncodeTableModel.java    License: MIT License 6 votes vote down vote up
public EncodeTableModel(String[] headings, List<EncodeFileRecord> records) {

        this.records = records;

        List<String> tmp = new ArrayList<>();
        tmp.add("");  // Checkbox heading
        for (String h : headings) {
            String heading = h.trim();
            if (heading.length() > 0 && !"path".equals(heading)) {
                tmp.add(heading);
            }
        }
        //tmp.add("path");
        columnHeadings = tmp.toArray(new String[tmp.size()]);


        sorter = new TableRowSorter<>(this);

        sorter.setStringConverter(new TableStringConverter() {
            @Override
            public String toString(TableModel model, int row, int column) {
                final Object value = model.getValueAt(row, column);
                return value == null ? "" : value.toString();
            }
        });
    }
 
Example 20
Source Project: pcgen   Source File: JTableEx.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void setAutoCreateRowSorter(boolean autoCreateRowSorter)
{
	boolean oldValue = this.sortingEnabled;
	this.sortingEnabled = autoCreateRowSorter;
	if (sortingEnabled)
	{
		TableModel model = getModel();
		if (model instanceof SortableTableModel)
		{
			setRowSorter(new SortableTableRowSorter((SortableTableModel) dataModel));
		}
		else
		{
			setRowSorter(new TableRowSorter<>(model));
		}
	}
	firePropertyChange("autoCreateRowSorter", oldValue, autoCreateRowSorter);
}
 
Example 21
Source Project: mzmine2   Source File: PeakListTableWindow.java    License: GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public void updateFilter() {

  List<RowFilter<Object, Object>> rowSorters = new ArrayList<RowFilter<Object, Object>>();


  TableRowSorter<PeakListTableModel> sorter =
      (TableRowSorter<PeakListTableModel>) table.getRowSorter();

  String textId = "(?i)" + filterTextId.getText();
  String textIdentity = "(?i)" + filterTextIdentity.getText();
  String textComment = "(?i)" + filterTextComment.getText();

  rowSorters.add(RowFilter.regexFilter(textId, 0));
  rowSorters.add(mzFilter);
  rowSorters.add(rtFilter);
  rowSorters.add(RowFilter.regexFilter(textIdentity, 3));
  rowSorters.add(RowFilter.regexFilter(textComment, 4));

  sorter.setRowFilter(RowFilter.andFilter(rowSorters));
  table.setRowSorter(sorter);

}
 
Example 22
@Test
public void testFilteredTable() {
  JTable jTable = new JTable(10, 1);
  TableRowSorter rowSorter = new TableRowSorter(jTable.getModel());
  jTable.setRowSorter(rowSorter);
  RowFilter<TableModel, Integer> rowFilter = new RowFilter<TableModel, Integer>() {

    public boolean include(Entry<? extends TableModel, ? extends Integer> entry) {
      return entry.getIdentifier().intValue() % 2 == 0;
    }
  };

  rowSorter.setRowFilter(rowFilter);
  NextRowProvider nextRowProvider = NextRowProviderFactory.getNextFilteredTableRow(jTable);
  for (int i = 0; i < 5; i++) {
    int nextRow = nextRowProvider.getNextRow();
    assertEquals(i * 2, nextRow);
  }
  assertEquals(-1, nextRowProvider.getNextRow());
}
 
Example 23
Source Project: constellation   Source File: TableViewTopComponent.java    License: 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 24
Source Project: constellation   Source File: TableViewTopComponent.java    License: 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 25
Source Project: CQL   Source File: GuiUtil.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("serial")
public static JPanel makeTable(Border b, String border, Object[][] rowData, Object... colNames) {
	JTable t = new JTable(rowData, colNames) {
		@Override
		public Dimension getPreferredScrollableViewportSize() {
			Dimension d = getPreferredSize();
			return new Dimension(Integer.max(640, d.width), (d.height));
		}
	};
	JPanel p = new JPanel(new GridLayout(1, 1));
	TableRowSorter<?> sorter = new MyTableRowSorter(t.getModel());
	if (colNames.length > 0) {
		sorter.toggleSortOrder(0);
	}
	t.setRowSorter(sorter);
	sorter.allRowsChanged();
	p.add(new JScrollPane(t));

	for (int row = 0; row < t.getRowCount(); row++) {
		int rowHeight = t.getRowHeight();

		for (int column = 0; column < t.getColumnCount(); column++) {
			Component comp = t.prepareRenderer(t.getCellRenderer(row, column), row, column);
			rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
		}

		t.setRowHeight(row, rowHeight);
	}

	Font font = UIManager.getFont("TableHeader.font");
	p.setBorder(BorderFactory.createTitledBorder(b, border, TitledBorder.DEFAULT_JUSTIFICATION,
			TitledBorder.DEFAULT_POSITION, font, Color.black));
	return p;

}
 
Example 26
Source Project: dragonwell8_jdk   Source File: bug8032874.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static void createAndShowUI() {
    try {
        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    JFrame frame = new JFrame("bug8032874");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    tableModel = new TestTableModel();
    table = new JTable(tableModel);
    table.setSurrendersFocusOnKeystroke(true);

    final TableRowSorter<TestTableModel> rowSorter = new TableRowSorter<TestTableModel>(tableModel);
    rowSorter.setRowFilter(new RowFilter<TestTableModel, Integer>() {
        @Override
        public boolean include(Entry<? extends TestTableModel, ? extends Integer> entry) {
            return entry.getIdentifier() % 2 == 0;
        }
    });
    table.setRowSorter(rowSorter);

    JScrollPane jScrollPane = new JScrollPane(table);
    panel.add(jScrollPane);

    frame.setContentPane(panel);
    frame.setSize(new Dimension(800, 600));
    frame.setVisible(true);
}
 
Example 27
Source Project: incubator-iotdb   Source File: ResultStatisticTab.java    License: Apache License 2.0 5 votes vote down vote up
ResultStatisticTab(String planName, Map<String, List<TimeSeriesStatistics>>
    timeseriesStatistics, TabCloseCallBack closeCallBack) {
  super(planName, closeCallBack);

  table = new JTable();

  Box box = Box.createVerticalBox();
  // the header and the data should be added separately
  box.add(table.getTableHeader());
  box.add(table);

  // provides a scroll bar for many series
  JScrollPane scrollPane = new JScrollPane(box);
  scrollPane.setLocation(0, 100);
  scrollPane.setSize(800, 600);
  add(scrollPane);

  Object[] header = TimeSeriesStatistics.HEADER;
  List<TimeSeriesStatistics> allStatistics = new ArrayList<>();
  for (List<TimeSeriesStatistics> seriesStatistics : timeseriesStatistics.values()) {
    allStatistics.addAll(seriesStatistics);
  }
  allStatistics.sort(Comparator.comparing(TimeSeriesStatistics::getName));
  Object[][] data = new Object[allStatistics.size()][];
  for (int i = 0; i < allStatistics.size(); i++) {
    data[i] = allStatistics.get(i).toArray();
  }
  tableModel = new DefaultTableModel(data, header);
  table.setModel(tableModel);
  // enable sort by column
  table.setRowSorter(new TableRowSorter<>(tableModel));
}
 
Example 28
Source Project: flowml   Source File: TextTable.java    License: Apache License 2.0 5 votes vote down vote up
public void setSort(int column, SortOrder sortOrder) {
    rowSorter = new TableRowSorter<TableModel>(this.tableModel);
    List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
    // sortKeys.add(new RowSorter.SortKey(1, SortOrder.ASCENDING));
    sortKeys.add(new RowSorter.SortKey(column, sortOrder));
    rowSorter.setSortKeys(sortKeys);
}
 
Example 29
Source Project: openjdk-jdk8u   Source File: bug8032874.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static void createAndShowUI() {
    try {
        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    JFrame frame = new JFrame("bug8032874");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    tableModel = new TestTableModel();
    table = new JTable(tableModel);
    table.setSurrendersFocusOnKeystroke(true);

    final TableRowSorter<TestTableModel> rowSorter = new TableRowSorter<TestTableModel>(tableModel);
    rowSorter.setRowFilter(new RowFilter<TestTableModel, Integer>() {
        @Override
        public boolean include(Entry<? extends TestTableModel, ? extends Integer> entry) {
            return entry.getIdentifier() % 2 == 0;
        }
    });
    table.setRowSorter(rowSorter);

    JScrollPane jScrollPane = new JScrollPane(table);
    panel.add(jScrollPane);

    frame.setContentPane(panel);
    frame.setSize(new Dimension(800, 600));
    frame.setVisible(true);
}
 
Example 30
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 5 votes vote down vote up
protected void fireDocumentChangeEvent(TableRowSorter<? extends TableModel> sorter) {
  field.setBackground(Color.WHITE);
  String pattern = field.getText().trim();
  if (pattern.isEmpty()) {
    sorter.setRowFilter(null);
    renderer.updatePattern("");
  } else if (renderer.updatePattern(pattern)) {
    try {
      sorter.setRowFilter(RowFilter.regexFilter(pattern));
    } catch (PatternSyntaxException ex) {
      field.setBackground(WARNING_COLOR);
    }
  }
}