Example usage for javax.swing JTable setAutoCreateRowSorter

List of usage examples for javax.swing JTable setAutoCreateRowSorter

Introduction

In this page you can find the example usage for javax.swing JTable setAutoCreateRowSorter.

Prototype

@BeanProperty(preferred = true, description = "Whether or not to turn on sorting by default.")
public void setAutoCreateRowSorter(boolean autoCreateRowSorter) 

Source Link

Document

Specifies whether a RowSorter should be created for the table whenever its model changes.

Usage

From source file:org.rivalry.swingui.SortTablePanel.java

/**
 * @param tableModel Table model.//from   www .ja v a  2  s. c om
 *
 * @return a new table.
 */
private JTable createTable(final TableModel tableModel) {
    final JTable answer = new ColumnNameToolTipTable(tableModel);

    answer.setAutoCreateRowSorter(true);
    answer.setGridColor(Color.GRAY);

    final RowSorter<? extends TableModel> rowSorter = answer.getRowSorter();
    rowSorter.addRowSorterListener(new RowSorterListener() {
        @Override
        public void sorterChanged(final RowSorterEvent event) {
            final List<? extends SortKey> sortKeys = rowSorter.getSortKeys();

            if (CollectionUtils.isNotEmpty(sortKeys)) {
                _userPreferences.putSortKey(sortKeys.get(0));
            }
        }
    });

    return answer;
}

From source file:org.simmi.GeneSetHead.java

License:asdf

public Set<String> getSelspec(Component comp, final List<String> specs, final JCheckBox... contigs) {
    final JTable table = new JTable();
    table.setDragEnabled(true);// w  w w  . j  a va2 s  .  c  om
    JScrollPane scroll = new JScrollPane(table);
    table.setAutoCreateRowSorter(true);

    final List<Sequence> ctgs = new ArrayList<Sequence>(geneset.contigmap.values());
    final TableModel contigmodel = new TableModel() {
        @Override
        public int getRowCount() {
            return ctgs.size();
        }

        @Override
        public int getColumnCount() {
            return 1;
        }

        @Override
        public String getColumnName(int columnIndex) {
            return "Contigs/Scaffolds";
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return String.class;
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return ctgs.get(rowIndex).toString();
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        }

        @Override
        public void addTableModelListener(TableModelListener l) {
        }

        @Override
        public void removeTableModelListener(TableModelListener l) {
        }
    };
    final TableModel specmodel = new TableModel() {
        @Override
        public int getRowCount() {
            return specs.size();
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public String getColumnName(int columnIndex) {
            return columnIndex == 0 ? "Id" : "Spec";
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return String.class;
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex == 1)
                return Sequence.nameFix(specs.get(rowIndex), true);
            else
                return specs.get(rowIndex);
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        }

        @Override
        public void addTableModelListener(TableModelListener l) {
        }

        @Override
        public void removeTableModelListener(TableModelListener l) {
        }
    };
    table.setModel(specmodel);

    TransferHandler th = dragRows(table, specs);
    table.setTransferHandler(th);

    if (contigs != null && contigs.length > 0 && !contigs[0].getText().equals("Plasmid"))
        contigs[0].addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if (contigs[0] != null && contigs[0].isSelected())
                    table.setModel(contigmodel);
                else
                    table.setModel(specmodel);
            }
        });

    Object[] ctls = new Object[] { scroll, contigs };
    //Object[] ctls2 = new Object[] { scroll };

    //if( contigs != null ) 
    JOptionPane.showMessageDialog(comp, ctls);
    //else JOptionPane.showMessageDialog( comp, ctls2 );
    Set<String> selspec = new LinkedHashSet<>();
    for (int i = 0; i < table.getRowCount(); i++) {
        if (table.isRowSelected(i)) {
            String spec = (String) table.getValueAt(i, 0);
            System.err.println("test " + spec);
            selspec.add(spec);
        }
    }
    return selspec;
}

From source file:org.simmi.GeneSetHead.java

License:asdf

public List getSelspecContigs(List<JComponent> complist, final Map<String, List<Sequence>> speccontigMap,
        final String... selspec) {
    List<Sequence> contigs = null;
    final List<String> specs = new ArrayList<String>(speccontigMap.keySet());
    final JTable stable = new JTable();
    stable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    final TableModel stablemodel = new TableModel() {
        @Override//from w  w  w. j ava2s .  c om
        public int getRowCount() {
            return specs.size();
        }

        @Override
        public int getColumnCount() {
            return 1;
        }

        @Override
        public String getColumnName(int columnIndex) {
            return "Species";
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return String.class;
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            if (rowIndex >= 0 && rowIndex < specs.size())
                return specs.get(rowIndex);
            return null;
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        }

        @Override
        public void addTableModelListener(TableModelListener l) {
        }

        @Override
        public void removeTableModelListener(TableModelListener l) {
        }
    };
    stable.setModel(stablemodel);

    final JTable ctable = new JTable();
    ctable.setAutoCreateRowSorter(true);
    final TableModel ctablemodel = new TableModel() {
        @Override
        public int getRowCount() {
            int r = stable.getSelectedRow();
            String spec = selspec.length > 0 ? selspec[0] : (String) stable.getValueAt(r, 0);
            if (spec != null) {
                List<Sequence> contigs = geneset.speccontigMap.get(spec);
                return contigs.size();
            }
            return 0;
        }

        @Override
        public int getColumnCount() {
            return 1;
        }

        @Override
        public String getColumnName(int columnIndex) {
            return "Contigs";
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return Sequence.class;
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            int r = stable.getSelectedRow();
            String spec = selspec.length > 0 ? selspec[0] : (String) stable.getValueAt(r, 0);
            List<Sequence> contigs = geneset.speccontigMap.get(spec);
            return contigs.get(rowIndex);
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        }

        @Override
        public void addTableModelListener(TableModelListener l) {
        }

        @Override
        public void removeTableModelListener(TableModelListener l) {
        }
    };
    ctable.setModel(ctablemodel);

    JScrollPane sscrollpane = new JScrollPane(stable);
    JScrollPane cscrollpane = new JScrollPane(ctable);

    FlowLayout flowlayout = new FlowLayout();
    JComponent c = new JComponent() {
    };
    c.setLayout(flowlayout);
    if (selspec.length == 0)
        c.add(sscrollpane);
    c.add(cscrollpane);

    if (complist != null) {
        for (JComponent comp : complist) {
            c.add(comp);
        }
    }

    stable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            ctable.tableChanged(new TableModelEvent(ctablemodel));
        }
    });
    JOptionPane.showMessageDialog(this, c);

    int[] rr = stable.getSelectedRows();
    if (rr.length > 1) {
        List slist = new ArrayList();
        for (int r : rr) {
            int i = stable.convertRowIndexToModel(r);
            slist.add(specs.get(i));
        }

        return slist;
    } else {
        int sr = stable.getSelectedRow();
        String spec = selspec.length > 0 ? selspec[0] : (String) stable.getValueAt(sr, 0);
        if (spec != null) {
            List<Sequence> ctgs = geneset.speccontigMap.get(spec);
            rr = ctable.getSelectedRows();
            contigs = new ArrayList<Sequence>();
            for (int r : rr) {
                int i = ctable.convertRowIndexToModel(r);
                contigs.add(ctgs.get(i));
            }
        }

        return contigs;
    }
}