Example usage for javax.swing JTable getSelectionModel

List of usage examples for javax.swing JTable getSelectionModel

Introduction

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

Prototype

public ListSelectionModel getSelectionModel() 

Source Link

Document

Returns the ListSelectionModel that is used to maintain row selection state.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    JTable table = new JTable();

    int selMode = table.getSelectionModel().getSelectionMode();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    int rows = 10;
    int cols = 5;
    JTable table = new JTable(rows, cols);

    int rowIndex = table.getSelectionModel().getAnchorSelectionIndex();
    int vColIndex = table.getColumnModel().getSelectionModel().getAnchorSelectionIndex();
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable t = new JTable(10, 1);
    frame.add(new JScrollPane(t));

    t.getSelectionModel().clearSelection();
    t.getSelectionModel().addSelectionInterval(5, 6);
    t.getSelectionModel().addSelectionInterval(8, 8);
    frame.pack();// w  ww. ja v a2  s. c o  m
    frame.setVisible(true);
}

From source file:SharedListSelectionHandler.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Modifying Model");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String[] columnNames = { "French", "Spanish", "Italian" };
    String[][] tableData = { { "un", "uno", "uno" }, { "deux", "dos", "due" }, { "trois", "tres", "tre" },
            { "quatre", "cuatro", "quattro" }, { "cinq", "cinco", "cinque" }, { "six", "seis", "sei" },
            { "sept", "siete", "sette" } };

    JTable table = new JTable(tableData, columnNames);
    table.getSelectionModel().addListSelectionListener(new SharedListSelectionHandler());
    JScrollPane scrollPane1 = new JScrollPane(table);
    frame.add(scrollPane1, BorderLayout.CENTER);

    frame.setSize(640, 300);/*  w ww. j  av  a 2s.c  o  m*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTable table = new JTable();
    SelectionListener listener = new SelectionListener(table);
    table.getSelectionModel().addListSelectionListener(listener);
    table.getColumnModel().getSelectionModel().addListSelectionListener(listener);
}

From source file:Main.java

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

    if (table.getCellSelectionEnabled()) {
        table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        int rowIndexStart = table.getSelectedRow();
        int rowIndexEnd = table.getSelectionModel().getMaxSelectionIndex();
        int colIndexStart = table.getSelectedColumn();
        int colIndexEnd = table.getColumnModel().getSelectionModel().getMaxSelectionIndex();

    }/*  w w  w  . j  ava  2s. com*/
}

From source file:FixedColumnModel.java

public static void main(String args[]) {

    final Object rowData[][] = { { "1", "one", "I" }, { "2", "two", "II" }, { "3", "three", "III" } };

    final String columnNames[] = { "#", "English", "Roman" };

    final TableModel fixedColumnModel = new AbstractTableModel() {
        public int getColumnCount() {
            return 1;
        }//w ww  . j a  v  a2  s .c o m

        public String getColumnName(int column) {
            return columnNames[column];
        }

        public int getRowCount() {
            return rowData.length;
        }

        public Object getValueAt(int row, int column) {
            return rowData[row][column];
        }
    };

    final TableModel mainModel = new AbstractTableModel() {
        public int getColumnCount() {
            return columnNames.length - 1;
        }

        public String getColumnName(int column) {
            return columnNames[column + 1];
        }

        public int getRowCount() {
            return rowData.length;
        }

        public Object getValueAt(int row, int column) {
            return rowData[row][column + 1];
        }
    };

    JTable fixedTable = new JTable(fixedColumnModel);
    fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    JTable mainTable = new JTable(mainModel);
    mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    ListSelectionModel model = fixedTable.getSelectionModel();
    mainTable.setSelectionModel(model);

    JScrollPane scrollPane = new JScrollPane(mainTable);
    Dimension fixedSize = fixedTable.getPreferredSize();
    JViewport viewport = new JViewport();
    viewport.setView(fixedTable);
    viewport.setPreferredSize(fixedSize);
    viewport.setMaximumSize(fixedSize);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
    scrollPane.setRowHeaderView(viewport);

    JFrame frame = new JFrame("Fixed Column Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.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]);
                }/*from  www  . 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:Main.java

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

    if (table.getCellSelectionEnabled()) {
        // In the other modes, the set of selected cells can be retrieved using
        table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        // Get the min and max ranges of selected cells
        int rowIndexStart = table.getSelectedRow();
        int rowIndexEnd = table.getSelectionModel().getMaxSelectionIndex();
        int colIndexStart = table.getSelectedColumn();
        int colIndexEnd = table.getColumnModel().getSelectionModel().getMaxSelectionIndex();

        // Check each cell in the range
        for (int r = rowIndexStart; r <= rowIndexEnd; r++) {
            for (int c = colIndexStart; c <= colIndexEnd; c++) {
                if (table.isCellSelected(r, c)) {
                    System.out.println("cell is selected");
                }//from   ww w .  j a  v  a  2s . c  o m
            }
        }
    }
}

From source file:Main.java

/**
 * Selects a the specified row in the specified JTable and scrolls
 * the specified JScrollpane to the newly selected row. More importantly,
 * the call to repaint() delayed long enough to have the table
 * properly paint the newly selected row which may be offscre
 * @param table should belong to the specified JScrollPane
 *//*from w  w  w .  jav a  2s .  com*/
public static void selectRow(int row, JTable table, JScrollPane pane) {
    if (table == null || pane == null) {
        return;
    }
    if (contains(row, table.getModel()) == false) {
        return;
    }
    moveAdjustable(row * table.getRowHeight(), pane.getVerticalScrollBar());
    selectRow(row, table.getSelectionModel());
    // repaint must be done later because moveAdjustable
    // posts requests to the swing thread which must execute before
    // the repaint logic gets executed.
    repaintLater(table);
}