Example usage for javax.swing ListSelectionModel isSelectionEmpty

List of usage examples for javax.swing ListSelectionModel isSelectionEmpty

Introduction

In this page you can find the example usage for javax.swing ListSelectionModel isSelectionEmpty.

Prototype

boolean isSelectionEmpty();

Source Link

Document

Returns true if no indices are selected.

Usage

From source file:Main.java

public Main() {
    super(new GridLayout(1, 0));

    final String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };

    final Object[][] data = { { "Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false) },
            { "Alison", "Huml", "Rowing", new Integer(3), new Boolean(true) },
            { "Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false) },
            { "Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true) },
            { "Philip", "Milne", "Pool", new Integer(10), new Boolean(false) } };

    final JTable table = new JTable(data, columnNames);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);//from w  w w . j av  a2  s.  c o  m

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    if (ALLOW_ROW_SELECTION) { // true by default
        ListSelectionModel rowSM = table.getSelectionModel();
        rowSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                // Ignore extra messages.
                if (e.getValueIsAdjusting())
                    return;

                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No rows are selected.");
                } else {
                    int selectedRow = lsm.getMinSelectionIndex();
                    System.out.println("Row " + selectedRow + " is now selected.");
                }
            }
        });
    } else {
        table.setRowSelectionAllowed(false);
    }

    if (ALLOW_COLUMN_SELECTION) { // false by default
        if (ALLOW_ROW_SELECTION) {
            // We allow both row and column selection, which
            // implies that we *really* want to allow individual
            // cell selection.
            table.setCellSelectionEnabled(true);
        }
        table.setColumnSelectionAllowed(true);
        ListSelectionModel colSM = table.getColumnModel().getSelectionModel();
        colSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                // Ignore extra messages.
                if (e.getValueIsAdjusting())
                    return;

                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No columns are selected.");
                } else {
                    int selectedCol = lsm.getMinSelectionIndex();
                    System.out.println("Column " + selectedCol + " is now selected.");
                }
            }
        });
    }

    if (DEBUG) {
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                printDebugData(table);
            }
        });
    }

    // Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    // Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:SimpleTableSelectionDemo.java

public SimpleTableSelectionDemo() {
    super(new GridLayout(1, 0));

    final String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };

    final Object[][] data = { { "Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false) },
            { "Alison", "Huml", "Rowing", new Integer(3), new Boolean(true) },
            { "Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false) },
            { "Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true) },
            { "Philip", "Milne", "Pool", new Integer(10), new Boolean(false) } };

    final JTable table = new JTable(data, columnNames);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);/*from   ww  w  . j  a v  a2  s  .c o  m*/

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    if (ALLOW_ROW_SELECTION) { // true by default
        ListSelectionModel rowSM = table.getSelectionModel();
        rowSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                // Ignore extra messages.
                if (e.getValueIsAdjusting())
                    return;

                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No rows are selected.");
                } else {
                    int selectedRow = lsm.getMinSelectionIndex();
                    System.out.println("Row " + selectedRow + " is now selected.");
                }
            }
        });
    } else {
        table.setRowSelectionAllowed(false);
    }

    if (ALLOW_COLUMN_SELECTION) { // false by default
        if (ALLOW_ROW_SELECTION) {
            // We allow both row and column selection, which
            // implies that we *really* want to allow individual
            // cell selection.
            table.setCellSelectionEnabled(true);
        }
        table.setColumnSelectionAllowed(true);
        ListSelectionModel colSM = table.getColumnModel().getSelectionModel();
        colSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                // Ignore extra messages.
                if (e.getValueIsAdjusting())
                    return;

                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No columns are selected.");
                } else {
                    int selectedCol = lsm.getMinSelectionIndex();
                    System.out.println("Column " + selectedCol + " is now selected.");
                }
            }
        });
    }

    if (DEBUG) {
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                printDebugData(table);
            }
        });
    }

    // Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    // Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:components.SimpleTableSelectionDemo.java

public SimpleTableSelectionDemo() {
    super(new GridLayout(1, 0));

    final String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };

    final Object[][] data = { { "Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false) },
            { "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
            { "Sue", "Black", "Knitting", new Integer(2), new Boolean(false) },
            { "Jane", "White", "Speed reading", new Integer(20), new Boolean(true) },
            { "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };

    final JTable table = new JTable(data, columnNames);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);/*from  ww  w  .  ja  va  2 s  .com*/

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    if (ALLOW_ROW_SELECTION) { // true by default
        ListSelectionModel rowSM = table.getSelectionModel();
        rowSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                //Ignore extra messages.
                if (e.getValueIsAdjusting())
                    return;

                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No rows are selected.");
                } else {
                    int selectedRow = lsm.getMinSelectionIndex();
                    System.out.println("Row " + selectedRow + " is now selected.");
                }
            }
        });
    } else {
        table.setRowSelectionAllowed(false);
    }

    if (ALLOW_COLUMN_SELECTION) { // false by default
        if (ALLOW_ROW_SELECTION) {
            //We allow both row and column selection, which
            //implies that we *really* want to allow individual
            //cell selection.
            table.setCellSelectionEnabled(true);
        }
        table.setColumnSelectionAllowed(true);
        ListSelectionModel colSM = table.getColumnModel().getSelectionModel();
        colSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                //Ignore extra messages.
                if (e.getValueIsAdjusting())
                    return;

                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No columns are selected.");
                } else {
                    int selectedCol = lsm.getMinSelectionIndex();
                    System.out.println("Column " + selectedCol + " is now selected.");
                }
            }
        });
    }

    if (DEBUG) {
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                printDebugData(table);
            }
        });
    }

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:SimpleTableSelectionDemo.java

public SimpleTableSelectionDemo() {
    super(new GridLayout(1, 0));

    final String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };

    final Object[][] data = { { "Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false) },
            { "Alison", "Huml", "Rowing", new Integer(3), new Boolean(true) },
            { "Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false) },
            { "Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true) },
            { "Philip", "Milne", "Pool", new Integer(10), new Boolean(false) } };

    final JTable table = new JTable(data, columnNames);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    if (ALLOW_ROW_SELECTION) { // true by default
        ListSelectionModel rowSM = table.getSelectionModel();
        rowSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                //Ignore extra messages.
                if (e.getValueIsAdjusting())
                    return;

                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No rows are selected.");
                } else {
                    int selectedRow = lsm.getMinSelectionIndex();
                    System.out.println("Row " + selectedRow + " is now selected.");
                }//from   w  ww  .ja v a  2  s .  co  m
            }
        });
    } else {
        table.setRowSelectionAllowed(false);
    }

    if (ALLOW_COLUMN_SELECTION) { // false by default
        if (ALLOW_ROW_SELECTION) {
            //We allow both row and column selection, which
            //implies that we *really* want to allow individual
            //cell selection.
            table.setCellSelectionEnabled(true);
        }
        table.setColumnSelectionAllowed(true);
        ListSelectionModel colSM = table.getColumnModel().getSelectionModel();
        colSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                //Ignore extra messages.
                if (e.getValueIsAdjusting())
                    return;

                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No columns are selected.");
                } else {
                    int selectedCol = lsm.getMinSelectionIndex();
                    System.out.println("Column " + selectedCol + " is now selected.");
                }
            }
        });
    }

    if (DEBUG) {
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                printDebugData(table);
            }
        });
    }

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:com.microsoft.alm.plugin.idea.common.ui.workitem.SelectWorkItemsModel.java

public String getComment() {
    final ListSelectionModel selectionModel = getTableSelectionModel();
    if (!selectionModel.isSelectionEmpty()) {
        final StringBuilder sb = new StringBuilder();
        String separator = "";
        for (final WorkItem item : tableModel.getSelectedWorkItems()) {
            sb.append(separator);/*from  w w w.j av a 2 s .c  o  m*/
            sb.append(WorkItemHelper.getWorkItemCommitMessage(item));
            separator = "\n";
        }
        return sb.toString();
    }

    return StringUtils.EMPTY;
}

From source file:gui.TraitViewerDialog.java

public void processTableClick(ListSelectionEvent e) {
    ListSelectionModel lsm = (ListSelectionModel) e.getSource();
    if (lsm.isSelectionEmpty() == true)
        return;/*from  w ww.j  a v  a2 s .c  om*/
    int selectedRow = lsm.getMinSelectionIndex();
    splits.setRightComponent(getChart(selectedRow));

}

From source file:SharedListSelectionHandler.java

public void valueChanged(ListSelectionEvent e) {
    ListSelectionModel lsm = (ListSelectionModel) e.getSource();

    int firstIndex = e.getFirstIndex();
    int lastIndex = e.getLastIndex();
    boolean isAdjusting = e.getValueIsAdjusting();
    System.out.println("Event for indexes " + firstIndex + " - " + lastIndex + "; isAdjusting is " + isAdjusting
            + "; selected indexes:");

    if (lsm.isSelectionEmpty()) {
        System.out.println(" <none>");
    } else {/*w w w  . jav a  2 s .  c o  m*/
        // Find out which indexes are selected.
        int minIndex = lsm.getMinSelectionIndex();
        int maxIndex = lsm.getMaxSelectionIndex();
        for (int i = minIndex; i <= maxIndex; i++) {
            if (lsm.isSelectedIndex(i)) {
                System.out.println(" " + i);
            }
        }
    }
}

From source file:gui.TwopointPWDPanel.java

private void processPhaseTableClick(ListSelectionEvent e) {
    ListSelectionModel lsm = (ListSelectionModel) e.getSource();
    if (lsm.isSelectionEmpty())
        return;/*from   w ww .j  ava2  s .c om*/

    // Which two markers are selected?
    CMarker cm1 = (CMarker) markerTable.getValueAt(markerTable.getSelectedRow(), 1);
    CMarker cm2 = (CMarker) phaseTable.getValueAt(phaseTable.getSelectedRow(), 1);

    if (cm1 == cm2) {
        m1Label.setText("");
        m2Label.setText("");
    } else {
        int cm1i = order.getLinkageGroup().getMarkerIndexBySafeName(cm1.safeName);
        int cm2i = order.getLinkageGroup().getMarkerIndexBySafeName(cm2.safeName);
        if (order.translateIndexes != null) {
            cm1i = order.translatePPIndexes[order.getLinkageGroup().getMarkerIndexBySafeName(cm1.safeName)];
            cm2i = order.translatePPIndexes[order.getLinkageGroup().getMarkerIndexBySafeName(cm2.safeName)];
        }

        PhasePair pp = order.getPhasePairArray()[cm1i][cm2i];

        // First determine the marker with the longest name
        int length1 = cm1.marker.getName().length();
        int length2 = cm2.marker.getName().length();
        int max = length1 >= length2 ? length1 : length2;

        // Need to determine *which way round* the phases are
        // stored/displayed
        if (pp.compare(cm1, cm2) == 1) {
            m1Label.setText(cm1.marker.getName(max) + " " + pp.phase1);
            m2Label.setText(cm2.marker.getName(max) + " " + pp.phase2);
        } else {
            m1Label.setText(cm1.marker.getName(max) + " " + pp.phase2);
            m2Label.setText(cm2.marker.getName(max) + " " + pp.phase1);
        }
    }
}

From source file:gui.TwopointPWDPanelnonsnp.java

private void processPhaseTableClick(ListSelectionEvent e) {
    ListSelectionModel lsm = (ListSelectionModel) e.getSource();
    if (lsm.isSelectionEmpty())
        return;/* ww w .  java  2s . c  o  m*/

    // Which two markers are selected?
    CMarker cm1 = (CMarker) markerTable.getValueAt(markerTable.getSelectedRow(), 1);
    CMarker cm2 = (CMarker) phaseTable.getValueAt(phaseTable.getSelectedRow(), 1);

    if (cm1 == cm2) {
        m1Label.setText("");
        m2Label.setText("");
    } else {
        PhasePair pp = order.getPhasePairArray()[cm1.getSafeNameSuffix() - 1][cm2.getSafeNameSuffix() - 1];

        // First determine the marker with the longest name
        int length1 = cm1.marker.getName().length();
        int length2 = cm2.marker.getName().length();
        int max = length1 >= length2 ? length1 : length2;

        // Need to determine *which way round* the phases are
        // stored/displayed
        if (pp.compare(cm1, cm2) == 1) {
            m1Label.setText(cm1.marker.getName(max) + " " + pp.phase1);
            m2Label.setText(cm2.marker.getName(max) + " " + pp.phase2);
        } else {
            m1Label.setText(cm1.marker.getName(max) + " " + pp.phase2);
            m2Label.setText(cm2.marker.getName(max) + " " + pp.phase1);
        }
    }
}

From source file:com.funambol.email.admin.user.ResultSearchUserPanel.java

/**
 * Set up graphic elements for this panel.
 *
 * @throws Exception if error occures during creation of the panel
 *//*w ww  .j  av  a 2s  . c  om*/
private void init() throws Exception {

    // create objects to display
    this.setLayout(new BorderLayout());
    this.setBorder(BorderFactory.createEmptyBorder());

    // create a model for the user table and pass it to the JTable object
    model = new UserTableModel();
    table = new JTable(model);

    table.setShowGrid(true);
    table.setAutoscrolls(true);
    table.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
    JScrollPane scrollpane = new JScrollPane(table);
    table.setPreferredScrollableViewportSize(new Dimension(800, 200));
    table.setFont(GuiFactory.defaultTableFont);
    table.getTableHeader().setFont(GuiFactory.defaultTableHeaderFont);
    this.add(scrollpane, BorderLayout.CENTER);

    //
    // Select user.
    //
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    ListSelectionModel rowSM = table.getSelectionModel();

    rowSM.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent event) {

            if (event.getValueIsAdjusting()) {
                return;
            }

            ListSelectionModel lsm = (ListSelectionModel) event.getSource();

            if (lsm.isSelectionEmpty()) {

                selectedUser = null;
            } else {

                int selectedRow = lsm.getMinSelectionIndex();
                selectedUser = users[selectedRow];
            }
        }
    });

    rowSM.clearSelection();

    table.addMouseListener(new MouseAdapter() {

        public void mouseClicked(MouseEvent event) {

            if (event.getClickCount() < 2) {
                return;
            }

            //
            // If the selected user is already associated to an account
            // then insertion process can't go on.
            //

            String userName = selectedUser.getUsername();
            String value[] = new String[] { userName };
            WhereClause wc = new WhereClause("username", value, WhereClause.OPT_EQ, true);
            MailServerAccount[] tmp = null;
            try {
                tmp = WSDao.getAccounts(wc);
            } catch (Exception e) {
            }

            if (tmp.length > 0) {

                StringBuilder sb = new StringBuilder("The user ");
                sb.append(userName).append(" is already associated to an account");
                Object[] options = { "OK" };
                JOptionPane.showOptionDialog(null, sb.toString(), "Warning", JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.WARNING_MESSAGE, null, options, options[0]);

            } else {
                //
                // Go to next step.
                //
                step.goToNextStep();
            }
        }
    });

}