Example usage for javax.swing ListSelectionModel setSelectionInterval

List of usage examples for javax.swing ListSelectionModel setSelectionInterval

Introduction

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

Prototype

void setSelectionInterval(int index0, int index1);

Source Link

Document

Changes the selection to be between index0 and index1 inclusive.

Usage

From source file:Main.java

protected static void selectRow(int row, ListSelectionModel model) {
    if (model == null) {
        return;//w w w. j a  v a2  s.c om
    }
    model.setSelectionInterval(row, row);
}

From source file:com.att.aro.ui.model.ImageBPTable.java

/**
 * Marks the specified item in the table as selected, if it exists.If the
 * item exists in the table and is already marked as selected, the the
 * selection is cleared./*from   www  .ja v  a 2  s  .c om*/
 * 
 * @param item
 *            The item in the table to mark as selected.
 * @return A boolean value that is true if the specified item was found and
 *         marked as selected.
 */
public boolean selectItem(T item) {
    int index;
    if (item != null && (index = getDataTableModel().indexOf(item)) >= 0) {
        index = convertRowIndexToView(index);
        ListSelectionModel selectionModel = getSelectionModel();
        if (selectionModel != null) {
            selectionModel.setSelectionInterval(index, index);
            scrollRectToVisible(getCellRect(index, 0, true));
            return true;
        }
    } else {
        clearSelection();
    }
    return false;
}

From source file:me.mayo.telnetkek.MainPanel.java

public final void updatePlayerList(final String selectedPlayerName) {
    EventQueue.invokeLater(() -> {
        playerListTableModel.fireTableDataChanged();

        MainPanel.this.txtNumPlayers.setText("" + playerList.size());

        if (selectedPlayerName != null) {
            final JTable table = MainPanel.this.tblPlayers;
            final ListSelectionModel selectionModel = table.getSelectionModel();

            playerList.stream().filter((player) -> (player.getName().equals(selectedPlayerName)))
                    .forEach((player) -> {
                        selectionModel.setSelectionInterval(0,
                                table.convertRowIndexToView(playerList.indexOf(player)));
                    });//w ww .j  a v a 2 s.co m
        }
    });
}

From source file:edu.ku.brc.ui.tmanfe.SearchReplacePanel.java

public void updateTableUiForFoundValue(TableSearcherCell cell, int replacementCount, boolean isReplace) {
    //log.debug("updateTableUiForFoundValue()");
    //boolean found = cell.isFound();
    int curRow = -1;
    int curCol = -1;
    if (replacementCount > 0) {
        //log.debug("updateTableUiForFoundValue() - update for found cell");

        curRow = cell.getRow();//from   w ww  . jav  a  2  s . com
        curCol = cell.getColumn();
        //log.debug("updateTableUiForFoundValue() - Cell row[" + curRow + "] ");
        //log.debug("                               Cell col[" + curCol + "] ");
        //log.debug("updateTableUiForFoundValue() - preoapring to set selection model");
        if (curRow != -1 && curCol != -1) {
            ListSelectionModel rsm = table.getSelectionModel();
            ListSelectionModel csm = table.getColumnModel().getSelectionModel();
            Rectangle rect = table.getCellRect(curRow, curCol, false);
            if (!isSearchSelection()) {
                rsm.setSelectionInterval(curRow, curRow);
                csm.setSelectionInterval(curCol, curCol);
            } else {
                table.setEmphasizedCell(curRow, curCol);
                if (rect != null) {
                    table.repaint(rect);
                } else {
                    table.repaint();
                }
            }
            if (rect != null && table.getAutoscrolls()) {
                //log.debug("updateTableUiForFoundValue() - preparing to scroll");
                table.scrollRectToVisible(rect);
                //log.debug("updateTableUiForFoundValue() - done scrolling");
            }
        }
        if (isSearchDown()) {
            enablePreviousButton();
        } else {
            enableNextButton();
        }
        updateStatusLabel(replacementCount, isReplace);
    } else {
        //log.debug("updateTableUiForFoundValue() found nothing");
        if (isSearchDown()) {
            setFinishedSearchingDown(true);
            if (!getWrapSearchFlag()) {
                disableNextButton();
                enablePreviousButton();
            }
            setStatusLabelWithFailedFind();
        } else {
            setFinishedSearchingUp(true);
            if (!getWrapSearchFlag()) {
                disablePreviousButton();
                enableNextButton();
            }
            setStatusLabelWithFailedFind();
        }
    }
}

From source file:org.jas.gui.MainWindow.java

@EventMethod(Events.TRACKS_LOADED)
void onTracksLoaded() {
    Set<File> filesWithoutMinimumMetadata = controlEngineConfigurator.getControlEngine()
            .get(Model.FILES_WITHOUT_MINIMUM_METADATA);
    List<Metadata> metadatas = controlEngineConfigurator.getControlEngine().get(Model.METADATA);
    if (!filesWithoutMinimumMetadata.isEmpty()) {
        File file = new File("Music File");
        Iterator<File> iterator = filesWithoutMinimumMetadata.iterator();
        while (iterator.hasNext()) {
            file = iterator.next();//from w w  w  . ja v  a 2s.com
        }
        if (filesWithoutMinimumMetadata.size() == 1) {
            StringBuilder sb = new StringBuilder();
            sb.append(file.getName());
            sb.append(ApplicationState.METADATA_FROM_FILE_LABEL);
            dialogHelper.showMessageDialog(this, sb.toString());
        } else {
            int otherFiles = filesWithoutMinimumMetadata.size() - 1;
            StringBuilder sb = new StringBuilder();
            sb.append(file.getName());
            sb.append(ApplicationState.AND_ANOTHER);
            sb.append(otherFiles);
            sb.append(ApplicationState.METADATA_FROM_FILE_LABEL);
            dialogHelper.showMessageDialog(this, sb.toString());
        }
    }
    resetStatus();
    ListSelectionModel listSelectionModel = getDescriptionTable().getSelectionModel();
    listSelectionModel.setSelectionInterval(0, 0);
    selectedRow = 0;
    updateImage(selectedRow);
    tableLoaded = true;
}

From source file:it.cnr.icar.eric.client.ui.swing.RegistryObjectsTable.java

/**
 * This fine grain notification tells listeners the exact range
 * of cells, rows, or columns that changed.
 *
 * Overrides base class behaviour by setting selection when first
 * row (destination) is added to model.//www .j av a2 s.c  o  m
 */
public void tableChanged(TableModelEvent e) {
    if (e == null || e.getFirstRow() == TableModelEvent.HEADER_ROW) {
        // The whole thing changed
        // This will effect invalidation of the JTable and JTableHeader.
        createDefaultColumnsFromModel();
        // Reset cachedTableCellRenderer
        cachedTableCellRenderers = new TableCellRenderer[getColumnCount()];
        return;
    } else {
        super.tableChanged(e);
    }
    // If no selectedRow, set selectedRow to firstRow
    if ((selectedRow == -1) && (e.getType() == TableModelEvent.INSERT)) {
        // Following will result in a software initiated selection
        // of the first row in table
        ListSelectionModel rowSM = getSelectionModel();
        rowSM.setSelectionInterval(0, 0);
    }
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java

protected void initComponent() {
    layout = new MigLayout("flowy, fill, insets 0", "", "[min!][fill]");
    panel = new JPanel(layout);

    topPanel = new JPanel(new BorderLayout());
    topPanel.setVisible(false);/*  www .ja  v  a2  s.  co  m*/
    panel.add(topPanel, "growx");

    scrollPane = new JScrollPane(impl);
    impl.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    impl.setFillsViewportHeight(true);
    panel.add(scrollPane, "grow");

    impl.setShowGrid(true);
    impl.setGridColor(Color.lightGray);

    impl.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
                handleClickAction();
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
            showPopup(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            showPopup(e);
        }

        protected void showPopup(MouseEvent e) {
            if (e.isPopupTrigger() && contextMenuEnabled) {
                // select row
                Point p = e.getPoint();
                int viewRowIndex = impl.rowAtPoint(p);

                int rowNumber;
                if (viewRowIndex >= 0) {
                    rowNumber = impl.convertRowIndexToModel(viewRowIndex);
                } else {
                    rowNumber = -1;
                }
                ListSelectionModel model = impl.getSelectionModel();

                if (!model.isSelectedIndex(rowNumber)) {
                    model.setSelectionInterval(rowNumber, rowNumber);
                }

                // show popup menu
                JPopupMenu popupMenu = createPopupMenu();
                if (popupMenu.getComponentCount() > 0) {
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        }
    });

    ColumnControlButton columnControlButton = new ColumnControlButton(impl) {
        @Override
        protected ColumnVisibilityAction createColumnVisibilityAction(TableColumn column) {
            ColumnVisibilityAction columnVisibilityAction = super.createColumnVisibilityAction(column);

            columnVisibilityAction.addPropertyChangeListener(evt -> {
                if ("SwingSelectedKey".equals(evt.getPropertyName()) && evt.getNewValue() instanceof Boolean) {
                    ColumnVisibilityAction action = (ColumnVisibilityAction) evt.getSource();

                    String columnName = action.getActionCommand();
                    boolean collapsed = !((boolean) evt.getNewValue());

                    Column col = getColumn(columnName);
                    if (col != null) {
                        col.setCollapsed(collapsed);
                    }
                }
            });

            return columnVisibilityAction;
        }
    };
    impl.setColumnControl(columnControlButton);

    impl.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
    impl.getActionMap().put("enter", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (enterPressAction != null) {
                enterPressAction.actionPerform(DesktopAbstractTable.this);
            } else {
                handleClickAction();
            }
        }
    });

    Messages messages = AppBeans.get(Messages.NAME);
    // localize default column control actions
    for (Object actionKey : impl.getActionMap().allKeys()) {
        if ("column.packAll".equals(actionKey)) {
            BoundAction action = (BoundAction) impl.getActionMap().get(actionKey);
            action.setName(messages.getMessage(DesktopTable.class, "DesktopTable.packAll"));
        } else if ("column.packSelected".equals(actionKey)) {
            BoundAction action = (BoundAction) impl.getActionMap().get(actionKey);
            action.setName(messages.getMessage(DesktopTable.class, "DesktopTable.packSelected"));
        } else if ("column.horizontalScroll".equals(actionKey)) {
            BoundAction action = (BoundAction) impl.getActionMap().get(actionKey);
            action.setName(messages.getMessage(DesktopTable.class, "DesktopTable.horizontalScroll"));
        }
    }

    // Ability to configure fonts in table
    // Add action to column control
    String configureFontsLabel = messages.getMessage(DesktopTable.class, "DesktopTable.configureFontsLabel");
    impl.getActionMap().put(ColumnControlButton.COLUMN_CONTROL_MARKER + "fonts",
            new AbstractAction(configureFontsLabel) {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Component rootComponent = SwingUtilities.getRoot(impl);
                    final FontDialog fontDialog = FontDialog.show(rootComponent, impl.getFont());
                    fontDialog.addWindowListener(new WindowAdapter() {
                        @Override
                        public void windowClosed(WindowEvent e) {
                            Font result = fontDialog.getResult();
                            if (result != null) {
                                impl.setFont(result);
                                packRows();
                            }
                        }
                    });
                    fontDialog.open();
                }
            });

    // Ability to reset settings
    String resetSettingsLabel = messages.getMessage(DesktopTable.class, "DesktopTable.resetSettings");
    impl.getActionMap().put(ColumnControlButton.COLUMN_CONTROL_MARKER + "resetSettings",
            new AbstractAction(resetSettingsLabel) {
                @Override
                public void actionPerformed(ActionEvent e) {
                    resetPresentation();
                }
            });

    scrollPane.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            if (!columnsInitialized) {
                adjustColumnHeaders();
            }
            columnsInitialized = true;
        }
    });

    // init default row height
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            if (!fontInitialized) {
                applyFont(impl, impl.getFont());
            }
        }
    });
}

From source file:com.peterbochs.PeterBochsDebugger.java

private void jELFTableMouseClicked(MouseEvent evt) {
    if (SwingUtilities.isRightMouseButton(evt)) {
        // select
        Point p = evt.getPoint();
        int rowNumber = elfTable.rowAtPoint(p);
        int columnNumber = elfTable.columnAtPoint(p);
        ListSelectionModel model = elfTable.getSelectionModel();
        model.setSelectionInterval(rowNumber, rowNumber);
        elfTable.getColumnModel().getSelectionModel().setSelectionInterval(columnNumber, columnNumber);
        // end select

        getJELFTablePopupMenu().show(evt.getComponent(), evt.getX(), evt.getY());
    }// w  ww.j  a  v a  2  s .com
}

From source file:com.peterbochs.PeterBochsDebugger.java

private void jHexTable1MouseClicked(MouseEvent evt) {
    if (SwingUtilities.isRightMouseButton(evt)) {
        // select
        Point p = evt.getPoint();
        int rowNumber = jHexTable1.rowAtPoint(p);
        int columnNumber = jHexTable1.columnAtPoint(p);
        ListSelectionModel model = jHexTable1.getSelectionModel();
        model.setSelectionInterval(rowNumber, rowNumber);
        jHexTable1.getColumnModel().getSelectionModel().setSelectionInterval(columnNumber, columnNumber);
        // end select

        getJHexTablePopupMenu().show(evt.getComponent(), evt.getX(), evt.getY());
    }/*ww  w. j a v a  2 s .  c  o m*/
}

From source file:com.peterbochs.PeterBochsDebugger.java

public void instructionTableMouseClicked(MouseEvent evt) {
    if (SwingUtilities.isRightMouseButton(evt)) {
        JTable jTable = (JTable) evt.getSource();

        if (jTable == instructionTable) {
            Global.clickedWhichInstructionPanel = 0;
        } else {//from   www  .j a va  2  s . c  o m
            Global.clickedWhichInstructionPanel = 1;
        }

        // select
        Point p = evt.getPoint();
        int rowNumber = jTable.rowAtPoint(p);
        int columnNumber = jTable.columnAtPoint(p);
        ListSelectionModel model = jTable.getSelectionModel();
        model.setSelectionInterval(rowNumber, rowNumber);

        jTable.getColumnModel().getSelectionModel().setSelectionInterval(columnNumber, columnNumber);
        // end select

        getJInstructionPanelPopupMenu().show(evt.getComponent(), evt.getX(), evt.getY());
    }
}