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

The following examples show how to use javax.swing.table.TableRowSorter#setComparator() . 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: CResultsTable.java    From binnavi with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new results table.
 *
 * @param graph Graph where the highlighting happens.
 * @param model Provides the results to display.
 */
public CResultsTable(final ZyGraph graph, final CSpecialInstructionsModel model) {
  m_graph = graph;
  m_model = model;

  m_tableModel = new CResultsTableModel(model);
  setModel(m_tableModel);

  final TableRowSorter<CResultsTableModel> sorter =
      new TableRowSorter<CResultsTableModel>(m_tableModel);
  sorter.setComparator(CResultsTableModel.TYPE_COLUMN, new CTypeComparator());
  sorter.setComparator(CResultsTableModel.ADDRESS_COLUMN, new HexStringComparator());
  setRowSorter(sorter);

  // Make sure that the status cells are colored.
  getColumnModel()
      .getColumn(CResultsTableModel.TYPE_COLUMN).setCellRenderer(new CTypeColumnRenderer());
  getColumnModel()
      .getColumn(CResultsTableModel.INSTRUCTION_COLUMN).setCellRenderer(new CMonospaceRenderer());

  final InternalSelectionListener listener = new InternalSelectionListener();

  getSelectionModel().addListSelectionListener(listener);
  addMouseListener(listener);
}
 
Example 2
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 3
Source File: PollTabPane.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public PollTabPane(Poll poll)
{
	super();
		
	//POLL DETAILS
	this.pollDetailsPanel = new PollDetailsPanel(poll);
	this.addTab("Poll Details", this.pollDetailsPanel);
	
	//ALL VOTES
	VotesTableModel allVotesTableModel = new VotesTableModel(poll.getVotes());
	final JTable allVotesTable = Gui.createSortableTable(allVotesTableModel, 0);
	
	TableRowSorter<VotesTableModel> sorter =  (TableRowSorter<VotesTableModel>) allVotesTable.getRowSorter();
	sorter.setComparator(VotesTableModel.COLUMN_VOTES, new BigDecimalStringComparator());
	
	this.addTab("All Votes", new JScrollPane(allVotesTable));
	
	//MY VOTES
	VotesTableModel myVotesTableModel = new VotesTableModel(poll.getVotes(Controller.getInstance().getAccounts()));
	final JTable myVotesTable = Gui.createSortableTable(myVotesTableModel, 0);
	
	sorter =  (TableRowSorter<VotesTableModel>) myVotesTable.getRowSorter();
	sorter.setComparator(VotesTableModel.COLUMN_VOTES, new BigDecimalStringComparator());
	
	this.addTab("My Votes", new JScrollPane(myVotesTable));
}
 
Example 4
Source File: JPanel_AppInstaller.java    From MobyDroid with Apache License 2.0 4 votes vote down vote up
/**
 * Creates new form JPanel_ManageApps
 *
 */
public JPanel_AppInstaller() {
    // initialize components
    initComponents();

    // table dimension
    jTable_Apps.setRowHeight(GuiUtils.APK_ICON_HEIGTH + 6);
    setColumnWidth(0, 256, -1);
    setColumnWidth(1, 64, 128);
    setColumnWidth(2, 64, 128);
    setColumnWidth(3, 96, 128);
    setColumnWidth(4, 96, 128);
    setColumnWidth(5, 96, 128);

    // set Table Row Sorter
    TableRowSorter tableRowSorter = new PackageTableRowSorter(jTable_Apps.getModel());
    jTable_Apps.setRowSorter(tableRowSorter);
    tableRowSorter.setComparator(2, (Comparator<Long>) (o1, o2) -> o1.compareTo(o2));

    // set cell render 1th column
    jTable_Apps.getColumnModel().getColumn(jTable_Apps.convertColumnIndexToView(0)).setCellRenderer(new ApkLablelCellRenderer());

    // right align 2nd column
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setHorizontalAlignment(JLabel.RIGHT);
    jTable_Apps.getColumnModel().getColumn(jTable_Apps.convertColumnIndexToView(2)).setCellRenderer(renderer);

    // set tab action to change focus component outside jtable
    jTable_Apps.getActionMap().put(jTable_Apps.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).get(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0)), new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
        }
    });

    // KeyBinding
    jTable_Apps.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "none");
    jTable_Apps.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_HOME, 0), "none");
    jTable_Apps.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_END, 0), "none");

    // set table selection listner
    listSelectionListener = (ListSelectionEvent lse) -> {
        setPackageDetails(packageTableModel.getPackage(jTable_Apps.getSelectionModel().getLeadSelectionIndex()));
    };
    jTable_Apps.getSelectionModel().addListSelectionListener(listSelectionListener);

    // Handle drag & drop files into jPanel
    dropHandle();

    // load checkbox values
    jCheckBox_Onsdcard.setSelected(Boolean.valueOf(Settings.get("AppInstaller_Onsdcard")));
    jCheckBox_Reinstall.setSelected(Boolean.valueOf(Settings.get("AppInstaller_Reinstall")));
    jCheckBox_Downgrade.setSelected(Boolean.valueOf(Settings.get("AppInstaller_Downgrade")));

    // hide for non expert
    if (!Boolean.valueOf(Settings.get("Expert_Settings"))) {
        jCheckBox_Onsdcard.setVisible(false);
        jCheckBox_Reinstall.setVisible(false);
        jCheckBox_Downgrade.setVisible(false);
        jTable_Apps.removeColumn(jTable_Apps.getColumnModel().getColumn(5));
        jTable_Apps.removeColumn(jTable_Apps.getColumnModel().getColumn(4));
        jTable_Apps.removeColumn(jTable_Apps.getColumnModel().getColumn(3));
    }

}
 
Example 5
Source File: JPanel_AppManager.java    From MobyDroid with Apache License 2.0 4 votes vote down vote up
/**
 * Creates new form JPanel_ManageApps
 *
 */
public JPanel_AppManager() {
    // initialize components
    initComponents();

    // table dimension
    jTable_Apps.setRowHeight(GuiUtils.APK_ICON_HEIGTH + 6);
    setColumnWidth(0, 32, 32);
    setColumnWidth(1, 256, -1);
    setColumnWidth(2, 64, 128);
    setColumnWidth(3, 64, 128);
    setColumnWidth(4, 64, 128);
    setColumnWidth(5, 96, 128);

    // set Table Row Sorter
    TableRowSorter tableRowSorter = new PackageTableRowSorter(jTable_Apps.getModel());
    jTable_Apps.setRowSorter(tableRowSorter);
    tableRowSorter.setComparator(2, (Comparator<Long>) (o1, o2) -> o1.compareTo(o2));

    // set table header for  0nd column
    jTable_Apps.getColumnModel().getColumn(jTable_Apps.convertColumnIndexToView(0)).setHeaderRenderer(new JCheckBoxTableHeaderCellRenderer());

    // set cell render 1th column
    jTable_Apps.getColumnModel().getColumn(jTable_Apps.convertColumnIndexToView(1)).setCellRenderer(new ApkLablelCellRenderer());

    // right align 3rd column
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setHorizontalAlignment(JLabel.RIGHT);
    jTable_Apps.getColumnModel().getColumn(jTable_Apps.convertColumnIndexToView(3)).setCellRenderer(renderer);

    // center align 4th & 5th column
    renderer.setHorizontalAlignment(JLabel.CENTER);
    jTable_Apps.getColumnModel().getColumn(jTable_Apps.convertColumnIndexToView(4)).setCellRenderer(renderer);
    jTable_Apps.getColumnModel().getColumn(jTable_Apps.convertColumnIndexToView(5)).setCellRenderer(renderer);

    // header click event
    jTable_Apps.getTableHeader().addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent mouseEvent) {
            int column = jTable_Apps.convertColumnIndexToModel(jTable_Apps.getColumnModel().getColumnIndexAtX(mouseEvent.getX()));
            if (mouseEvent.getClickCount() == 1 && column != -1) {
                packageTableModel.headerClicked(column);
            }
        }
    });

    // set tab action to change focus component outside jtable
    jTable_Apps.getActionMap().put(jTable_Apps.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).get(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0)), new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
        }
    });

    // KeyBinding
    jTable_Apps.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "none");
    jTable_Apps.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_HOME, 0), "none");
    jTable_Apps.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_END, 0), "none");

    // set table selection listner
    listSelectionListener = (ListSelectionEvent lse) -> {
        setPackageDetails(packageTableModel.getPackage(jTable_Apps.getSelectionModel().getLeadSelectionIndex()));
    };
    jTable_Apps.getSelectionModel().addListSelectionListener(listSelectionListener);

    // hide for non expert
    if (!Boolean.valueOf(Settings.get("Expert_Settings"))) {
        materialButtonH_Backup.setVisible(false);
        materialButtonH_Restore.setVisible(false);
        jTable_Apps.removeColumn(jTable_Apps.getColumnModel().getColumn(5));
    }
}
 
Example 6
Source File: CNodeChooserTable.java    From binnavi with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new node chooser table.
 * 
 * @param graph Provides the nodes for the table.
 * @param searchField Search field whose results are displayed in the table.
 */
public CNodeChooserTable(final ZyGraph graph, final CGraphSearchField searchField) {
  m_graph = Preconditions.checkNotNull(graph, "IE01773: Graph argument can't be null.");
  m_searchField =
      Preconditions.checkNotNull(searchField, "IE01774: Search field argument can not be null");

  m_model = new CNodeChooserModel(graph);
  setModel(m_model);

  final TableRowSorter<CNodeChooserModel> tableSorter =
      new TableRowSorter<CNodeChooserModel>(m_model);
  setRowSorter(tableSorter);
  tableSorter.setComparator(CNodeChooserModel.COLUMN_IN, new IntComparator());
  tableSorter.setComparator(CNodeChooserModel.COLUMN_OUT, new IntComparator());
  tableSorter.setComparator(CNodeChooserModel.COLUMN_ADDRESS, new LexicalComparator());
  tableSorter.setComparator(CNodeChooserModel.COLUMN_COLOR, new IntComparator());

  final CNodeChooserRenderer renderer =
      new CNodeChooserRenderer(this, m_graph, m_searchField.getGraphSearcher());

  setRowSelectionAllowed(true);
  setColumnSelectionAllowed(false);
  setCellSelectionEnabled(false);
  setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  getTableHeader().setReorderingAllowed(false);

  getColumnModel().getColumn(0).setCellRenderer(renderer);
  getColumnModel().getColumn(1).setCellRenderer(renderer);
  getColumnModel().getColumn(2).setCellRenderer(renderer);
  getColumnModel().getColumn(3).setCellRenderer(renderer);

  getColumnModel().getColumn(0).setPreferredWidth(35);
  getColumnModel().getColumn(1).setPreferredWidth(35);
  getColumnModel().getColumn(3).setPreferredWidth(50);
  getColumnModel().getColumn(0).setMaxWidth(50);
  getColumnModel().getColumn(1).setMaxWidth(50);
  getColumnModel().getColumn(3).setMaxWidth(50);

  m_searchField.addListener(m_searchFieldListener);

  m_mouselistener = new CNodeChooserMouseListener(this, graph);
  addMouseListener(m_mouselistener);
  m_graph.addListener((INaviGraphListener) m_viewListener);
  m_graph.addListener((IZyGraphSelectionListener) m_viewListener);
  initializeViewListeners(m_graph.getRawView());
}
 
Example 7
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 8
Source File: PollDetailsPanel.java    From Qora with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
public PollDetailsPanel(Poll poll)
{
	this.poll = poll;
	
	//LAYOUT
	this.setLayout(new GridBagLayout());
	
	//PADDING
	this.setBorder(new EmptyBorder(5, 5, 5, 5));
	
	//LABEL GBC
	GridBagConstraints labelGBC = new GridBagConstraints();
	labelGBC.insets = new Insets(0, 5, 5, 0);
	labelGBC.fill = GridBagConstraints.HORIZONTAL;   
	labelGBC.anchor = GridBagConstraints.NORTHWEST;
	labelGBC.weightx = 0;	
	labelGBC.gridx = 0;
	
	//DETAIL GBC
	GridBagConstraints detailGBC = new GridBagConstraints();
	detailGBC.insets = new Insets(0, 5, 5, 0);
	detailGBC.fill = GridBagConstraints.HORIZONTAL;  
	detailGBC.anchor = GridBagConstraints.NORTHWEST;
	detailGBC.weightx = 1;	
	detailGBC.gridwidth = 2;
	detailGBC.gridx = 1;		
	
	//LABEL CREATOR
	labelGBC.gridy = 1;
	JLabel creatorLabel = new JLabel("Creator:");
	this.add(creatorLabel, labelGBC);
	
	//CREATOR
	detailGBC.gridy = 1;
	JTextField creator = new JTextField(poll.getCreator().getAddress());
	creator.setEditable(false);
	this.add(creator, detailGBC);
	
	//LABEL NAME
	labelGBC.gridy = 2;
	JLabel nameLabel = new JLabel("Name:");
	this.add(nameLabel, labelGBC);
	
	//NAME
	detailGBC.gridy = 2;
	JTextField name = new JTextField(poll.getName());
	name.setEditable(false);
	this.add(name, detailGBC);		
	
	//LABEL DESCRIPTION
	labelGBC.gridy = 3;
	JLabel descriptionLabel = new JLabel("Description:");
	this.add(descriptionLabel, labelGBC);
			
	//DESCRIPTION
	detailGBC.gridy = 3;
	JTextArea txtAreaDescription = new JTextArea(poll.getDescription());
	txtAreaDescription.setRows(4);
	txtAreaDescription.setBorder(name.getBorder());
	txtAreaDescription.setEditable(false);
	this.add(txtAreaDescription, detailGBC);		
	
	//LABEL OPTIONS
	labelGBC.gridy = 4;
	JLabel optionsLabel = new JLabel("Options:");
	this.add(optionsLabel, labelGBC);
	
	//OPTIONS
	detailGBC.gridy = 4;
	PollOptionsTableModel pollOptionsTableModel = new PollOptionsTableModel(poll);
	table = Gui.createSortableTable(pollOptionsTableModel, 0);
	
	TableRowSorter<PollOptionsTableModel> sorter =  (TableRowSorter<PollOptionsTableModel>) table.getRowSorter();
	sorter.setComparator(PollOptionsTableModel.COLUMN_VOTES, new BigDecimalStringComparator());
	
	this.add(new JScrollPane(table), detailGBC);
	
	//ADD EXCHANGE BUTTON
	detailGBC.gridy = 5;
	JButton allButton = new JButton("Vote");
	allButton.setPreferredSize(new Dimension(100, 25));
	allButton.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent e)
		{
			onVoteClick();
		}
	});	
	this.add(allButton, detailGBC);
	
	//PACK
	this.setVisible(true);
}
 
Example 9
Source File: MainPanel.java    From java-swing-tips with MIT License 4 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());

  String[] columnNames = {"Group", "Name", "Count"};
  DefaultTableModel model = new DefaultTableModel(null, columnNames) {
    @Override public Class<?> getColumnClass(int column) {
      return RowData.class;
    }
  };

  String colors = "colors";
  addRowData(model, new RowData(colors, "blue", 1));
  addRowData(model, new RowData(colors, "violet", 2));
  addRowData(model, new RowData(colors, "red", 3));
  addRowData(model, new RowData(colors, "yellow", 4));
  String sports = "sports";
  addRowData(model, new RowData(sports, "baseball", 23));
  addRowData(model, new RowData(sports, "soccer", 22));
  addRowData(model, new RowData(sports, "football", 21));
  addRowData(model, new RowData(sports, "hockey", 20));
  String food = "food";
  addRowData(model, new RowData(food, "hot dogs", 10));
  addRowData(model, new RowData(food, "pizza", 11));
  addRowData(model, new RowData(food, "ravioli", 12));
  addRowData(model, new RowData(food, "bananas", 13));

  JTable table = new JTable(model) {
    @Override public void updateUI() {
      super.updateUI();
      setFillsViewportHeight(true);
      setDefaultRenderer(RowData.class, new RowDataRenderer());

      TableRowSorter<TableModel> sorter = new TableRowSorter<>(getModel());
      Comparator<RowData> c = Comparator.comparing(RowData::getGroup);
      sorter.setComparator(0, c);
      sorter.setComparator(1, c.thenComparing(RowData::getName));
      sorter.setComparator(2, c.thenComparing(RowData::getCount));
      setRowSorter(sorter);
    }
  };

  JButton button = new JButton("clear SortKeys");
  button.addActionListener(e -> table.getRowSorter().setSortKeys(null));

  add(new JScrollPane(table));
  add(button, BorderLayout.SOUTH);
  setPreferredSize(new Dimension(320, 240));
}