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

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

Introduction

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

Prototype

public void clearSelection() 

Source Link

Document

Clears the selection of this SelectionInList - if any.

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 va2  s . c om*/
        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();
            }
        }
    });
}