Example usage for javax.swing JTable getSelectedRows

List of usage examples for javax.swing JTable getSelectedRows

Introduction

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

Prototype

@BeanProperty(bound = false)
public int[] getSelectedRows() 

Source Link

Document

Returns the indices of all selected rows.

Usage

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//ww  w. ja  v a 2s  . c o  m
        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;
    }
}

From source file:pcgen.gui2.converter.panel.CampaignPanel.java

/**
 * @see pcgen.gui2.converter.panel.ConvertSubPanel#setupDisplay(javax.swing.JPanel, pcgen.cdom.base.CDOMObject)
 *//*  w  ww. j a v  a 2 s. c  o m*/
@Override
public void setupDisplay(JPanel panel, final CDOMObject pc) {
    panel.setLayout(new GridBagLayout());
    JLabel introLabel = new JLabel("Please select the Campaign(s) to Convert:");
    GridBagConstraints gbc = new GridBagConstraints();
    Utility.buildRelativeConstraints(gbc, GridBagConstraints.REMAINDER, 1, 1.0, 0,
            GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHWEST);
    gbc.insets = new Insets(25, 25, 5, 25);
    panel.add(introLabel, gbc);

    final CampaignTableModel model = new CampaignTableModel(gameModeCampaigns, folderName);
    final JTable table = new JTable(model) {
        //Implement table cell tool tips.
        @Override
        public String getToolTipText(MouseEvent e) {
            java.awt.Point p = e.getPoint();
            int rowIndex = rowAtPoint(p);
            int colIndex = columnAtPoint(p);
            String tip = String.valueOf(getValueAt(rowIndex, colIndex));
            return tip;
        }
    };
    table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent event) {
            pc.removeListFor(ListKey.CAMPAIGN);
            int[] selRows = table.getSelectedRows();
            if (selRows.length == 0) {
                saveSourceSelection(pc);
                fireProgressEvent(ProgressEvent.NOT_ALLOWED);
            } else {
                for (int row : selRows) {
                    Campaign selCampaign = (Campaign) model.getValueAt(row, 0);
                    pc.addToListFor(ListKey.CAMPAIGN, selCampaign);
                }
                saveSourceSelection(pc);
                fireProgressEvent(ProgressEvent.ALLOWED);
            }
        }
    });

    JScrollPane listScroller = new JScrollPane(table);
    Utility.buildRelativeConstraints(gbc, GridBagConstraints.REMAINDER, GridBagConstraints.REMAINDER, 1.0, 1.0);
    gbc.fill = GridBagConstraints.BOTH;
    panel.add(listScroller, gbc);

    initSourceSelection(model, table);
}

From source file:pcgen.gui2.tabs.EquipInfoTab.java

private List<Integer> getMenuTargets(JTable table, MouseEvent e) {
    int row = table.rowAtPoint(e.getPoint());
    if (!table.isRowSelected(row)) {
        table.setRowSelectionInterval(row, row);
    }/*from   w ww.  j a v  a 2s.c o m*/
    List<Integer> targets = new ArrayList<>();
    for (int selRow : table.getSelectedRows()) {
        targets.add(selRow);
    }
    return targets;
}

From source file:utils.ZTransform.java

@Override
public void actionPerformed(ActionEvent e) {
    List<CMatrix> loadedCMatrices = CoolMapMaster.getLoadedCMatrices();
    if (loadedCMatrices == null || loadedCMatrices.isEmpty()) {
        Messenger.showWarningMessage("No datasets were imported.", "No data");
        return;//ww  w .j  a  v  a 2  s .co  m
    }

    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            JTable table = new JTable();
            DefaultTableModel defaultTableModel = Utils.getDefaultTableModel();
            table.setModel(defaultTableModel);
            table.getColumnModel().removeColumn(table.getColumnModel().getColumn(0));
            table.getTableHeader().setReorderingAllowed(false);

            int returnVal = JOptionPane.showConfirmDialog(CoolMapMaster.getCMainFrame(), new JScrollPane(table),
                    "Select data", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
            if (returnVal == JOptionPane.OK_OPTION) {
                int[] selectedRows = table.getSelectedRows();
                ArrayList<CMatrix> selectedMatrices = new ArrayList<CMatrix>();
                for (int row : selectedRows) {

                    int index = table.convertRowIndexToModel(row);
                    try {
                        String ID = table.getModel().getValueAt(index, 0).toString();
                        CMatrix mx = CoolMapMaster.getCMatrixByID(ID);
                        if (mx != null) {
                            selectedMatrices.add(mx);
                        }
                    } catch (Exception e) {

                    }
                }
                //do
                createZTransform(selectedMatrices);
            }
        }
    });

}