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:Main.java

public static void main(String[] argv) throws Exception {
    JTable table = new JTable();

    if (!table.getColumnSelectionAllowed() && table.getRowSelectionAllowed()) {
        // /*from w  w  w  .ja  va  2  s .  com*/
        int[] rowIndices = table.getSelectedRows();
    }
}

From source file:Main.java

public static void main(String args[]) {
    DefaultTableModel model;// w  w w .  j  av a  2 s. c om
    JTable t = new JTable(model = new DefaultTableModel(0, 1));
    for (int i = 0; i < 10; i++) {
        model.addRow(new Object[] { i });
    }
    JButton removeSelected = new JButton("remove");
    removeSelected.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int[] selectedRows = t.getSelectedRows();
            for (int i = selectedRows.length - 1; i >= 0; i--) {
                model.removeRow(selectedRows[i]);
                ;
            }
        }
    });
    JFrame f = new JFrame();
    f.add(new JScrollPane(t));
    f.add(removeSelected, BorderLayout.SOUTH);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTable table;

    String[] columnTitles = { "A", "B", "C", "D" };
    Object[][] rowData = { { "11", "12", "13", "14" }, { "21", "22", "23", "24" }, { "31", "32", "33", "34" },
            { "41", "42", "44", "44" } };

    table = new JTable(rowData, columnTitles);

    table.setCellSelectionEnabled(true);
    ListSelectionModel cellSelectionModel = table.getSelectionModel();
    cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            String selectedData = null;

            int[] selectedRow = table.getSelectedRows();
            int[] selectedColumns = table.getSelectedColumns();

            for (int i = 0; i < selectedRow.length; i++) {
                for (int j = 0; j < selectedColumns.length; j++) {
                    selectedData = (String) table.getValueAt(selectedRow[i], selectedColumns[j]);
                }//w  ww . j  av  a2s .  c om
            }
            System.out.println("Selected: " + selectedData);
        }

    });

    frame.add(new JScrollPane(table));

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:com.comcast.cats.vision.CATSVisionTest.java

protected int[] searchAvailableSettopsAndGetRowsSelected(Window mainWindow, String keyword) {

    JPanel configPanel = CommonTestUtils.getConfigPanel(mainWindow);

    final Button searchButton = CommonTestUtils.getConfigPanelButton(mainWindow, "searchButton");

    final JTextField searchTextField = CommonTestUtils.getConfigPanelTextField(mainWindow, "searchTextField");

    Table uiTable = CommonTestUtils.getTableFromConfigPanel(configPanel, "availableSettopsScrollPane",
            "Available Settops", 0);

    assertTrue(/*from   w ww  . j a  va  2 s  .co  m*/
            "Expected header for the table is - " + Arrays.asList(CommonTestUtils.AVAILABLE_TABLE_STRINGS)
                    + "\n but actual header is - " + Arrays.asList(uiTable.getHeader().getColumnNames()),
            Arrays.equals(uiTable.getHeader().getColumnNames(), CommonTestUtils.AVAILABLE_TABLE_STRINGS));

    int numberOfRows = (uiTable.getRowCount() > 0) ? uiTable.getRowCount() : 0;

    assertTrue("'Available Settops' donot have any rows", numberOfRows > 0);

    searchTextField.setText(keyword);

    searchButton.click();

    Table uiTableAfterSearch = CommonTestUtils.getTableFromConfigPanel(configPanel,
            "availableSettopsScrollPane", "Available Settops", 0);

    int rows = (uiTableAfterSearch.getRowCount() > 0) ? uiTableAfterSearch.getRowCount() : 0;

    assertTrue("No matching settop found", rows > 0);

    uiTableAfterSearch.selectRow(0);

    searchTextField.setText("");

    searchButton.click();

    uiTableAfterSearch = CommonTestUtils.getTableFromConfigPanel(configPanel, "availableSettopsScrollPane",
            "Available Settops", 0);

    JTable table = uiTableAfterSearch.getAwtComponent();

    int numberOfRowsAfterSearch = (table.getSelectedRows().length > 0) ? table.getSelectedRows().length : 0;
    assertTrue(numberOfRowsAfterSearch <= numberOfRows);
    return table.getSelectedRows();
}

From source file:ExtendedDnDDemo.java

protected String exportString(JComponent c) {
    JTable table = (JTable) c;
    rows = table.getSelectedRows();
    int colCount = table.getColumnCount();

    StringBuffer buff = new StringBuffer();

    for (int i = 0; i < rows.length; i++) {
        for (int j = 0; j < colCount; j++) {
            Object val = table.getValueAt(rows[i], j);
            buff.append(val == null ? "" : val.toString());
            if (j != colCount - 1) {
                buff.append(",");
            }//from   w w w. j av a2  s  . co  m
        }
        if (i != rows.length - 1) {
            buff.append("\n");
        }
    }

    return buff.toString();
}

From source file:gdt.jgui.tool.JEntityEditor.java

private boolean hasSelectedRows() {
    try {//from  www  .  j  a  v  a 2 s.co  m
        JScrollPane scrollPane = (JScrollPane) tabbedPane.getSelectedComponent();
        JTable table = (JTable) scrollPane.getViewport().getView();
        int[] i = table.getSelectedRows();
        if (i.length > 0)
            return true;
        return false;
    } catch (Exception e) {
        LOGGER.severe(e.toString());
        return false;
    }
}

From source file:de.codesourcery.eve.skills.ui.components.impl.RefiningComponent.java

private Collection<Object> getSelectedRows(JTable table) {
    final Collection<Object> result = new ArrayList<Object>();

    final int[] rows = table.getSelectedRows();
    final TableModel tableModel = table.getModel();
    for (int viewRow : rows) {

        final int modelRow = table.convertRowIndexToModel(viewRow);

        if (tableModel == refinedItemsModel) {
            result.add(refinedItemsModel.getRow(modelRow));
        } else if (tableModel == this.refiningResultsModel) {
            result.add(refiningResultsModel.getRow(modelRow));
        } else {/*from w  ww  .ja  va 2s.c om*/
            throw new IllegalArgumentException("Internal error - table has unsupported model");
        }
    }
    return result;
}

From source file:gdt.jgui.entity.fields.JFieldsEditor.java

private boolean hasSelectedRows() {
    try {/* w  w  w.j ava 2  s. co m*/
        JTable table = (JTable) scrollPane.getViewport().getView();
        int[] i = table.getSelectedRows();
        if (i.length > 0)
            return true;
        return false;
    } catch (Exception e) {
        LOGGER.severe(e.toString());
        return false;
    }
}

From source file:com.mac.tarchan.desktop.event.EventQuery.java

/**
 * ????????//from   w w  w  .  j  av a2 s. com
 * 
 * @return ??
 */
public int[] selected() {
    for (Component child : list) {
        if (child instanceof JTable) {
            JTable table = ((JTable) child);
            return table.getSelectedRows();
        }
    }

    return new int[0];
}

From source file:com.mac.tarchan.desktop.event.EventQuery.java

/**
 * ??????//w  ww  .  j av  a 2 s. c om
 * ?????????????
 * 
 * @return ???
 * @see JTable#getValueAt(int, int)
 * @see ItemSelectable#getSelectedObjects()
 */
public Object[] selectedObjects() {
    for (Component child : list) {
        if (child instanceof JTable) {
            JTable table = ((JTable) child);
            int[] rows = table.getSelectedRows();
            int col = table.getSelectedColumn();
            ArrayList<Object> sublist = new ArrayList<Object>();
            for (int index : rows) {
                sublist.add(table.getValueAt(index, col));
            }
            return sublist.toArray();
        } else if (child instanceof ItemSelectable) {
            ItemSelectable selector = (ItemSelectable) child;
            return selector.getSelectedObjects();
        } else {
            // ignore
        }
    }

    return new Object[0];
}