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

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

Introduction

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

Prototype

public ValueModel getSelectionIndexHolder() 

Source Link

Document

Returns the selection index holder.

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.j  a va 2 s  .  c  o 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();
            }
        }
    });
}