Example usage for com.jgoodies.binding.list SelectionInList setSelectionIndex

List of usage examples for com.jgoodies.binding.list SelectionInList setSelectionIndex

Introduction

In this page you can find the example usage for com.jgoodies.binding.list SelectionInList setSelectionIndex.

Prototype

public void setSelectionIndex(int newSelectionIndex) 

Source Link

Document

Sets a new selection index.

Usage

From source file:com.songbook.pc.ui.view.MainFormView.java

License:Open Source License

private static void bind2Way(final JTable jTable, final SelectionInList<?> selectionInListModel) {
    // Set single row selection
    jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    // Bind TABLE => SelectionInList
    jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override//from  w w w  .ja  v a  2s . co  m
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                int viewIndex = jTable.getSelectedRow();
                if (viewIndex >= 0) {
                    int modelIndex = jTable.convertRowIndexToModel(viewIndex);
                    selectionInListModel.setSelectionIndex(modelIndex);
                } else {
                    selectionInListModel.clearSelection();
                }
            }
        }
    });

    // Bind SelectionInList => Table
    selectionInListModel.getSelectionIndexHolder().addValueChangeListener(new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            Integer modelIndex = (Integer) evt.getNewValue();
            if (modelIndex > 0) {
                int rowIndex = jTable.convertRowIndexToView(modelIndex);
                jTable.getSelectionModel().setSelectionInterval(rowIndex, rowIndex);
            } else {
                jTable.clearSelection();
            }
        }
    });
}