Example usage for javax.swing JList setSelectedIndex

List of usage examples for javax.swing JList setSelectedIndex

Introduction

In this page you can find the example usage for javax.swing JList setSelectedIndex.

Prototype

@BeanProperty(bound = false, description = "The index of the selected cell.")
public void setSelectedIndex(int index) 

Source Link

Document

Selects a single cell.

Usage

From source file:org.yccheok.jstock.gui.IndicatorPanel.java

private void Install() {
    if (false == promptToSaveSignificantEdits()) {
        return;//from w  w  w  .  j  a v  a  2  s .  c o m
    }
    final Wizard wizard = this.getWizardDialog();
    int ret = wizard.showModalDialog(650, 400, true);
    if (ret != Wizard.FINISH_RETURN_CODE) {
        final JList jList = this.getCurrentActiveJList();
        // Although cancel button is pressed, possible some indicators had
        // already installed from JStock server.
        //
        // Refresh JList after install from JStock server.
        this.syncJListWithIndicatorProjectManager(jList, this.getCurrentActiveIndicatorProjectManager());
        if (jList.getModel().getSize() > 0 && jList.getSelectedIndex() < 0) {
            jList.setSelectedIndex(0);
        }
        return;
    }
    final WizardModel wizardModel = wizard.getModel();
    final WizardPanelDescriptor wizardSelectInstallIndicatorMethodDescriptor = wizardModel
            .getPanelDescriptor(WizardSelectInstallIndicatorMethodDescriptor.IDENTIFIER);
    final WizardSelectInstallIndicatorMethodJPanel wizardSelectInstallIndicatorMethodJPanel = (WizardSelectInstallIndicatorMethodJPanel) wizardSelectInstallIndicatorMethodDescriptor
            .getPanelComponent();
    if (wizardSelectInstallIndicatorMethodJPanel.isLocalFileSelected()) {
        final File file = wizardSelectInstallIndicatorMethodJPanel.getSelectedFile();
        assert (file != null);
        if (this.jTabbedPane1.getSelectedIndex() == 0) {
            this.alertIndicatorInstall(file);
        } else if (this.jTabbedPane1.getSelectedIndex() == 1) {
            this.moduleIndicatorInstall(file);
        }
    } else {
        final JList jList = this.getCurrentActiveJList();
        // Refresh JList after install from JStock server.
        this.syncJListWithIndicatorProjectManager(jList, this.getCurrentActiveIndicatorProjectManager());
        if (jList.getModel().getSize() > 0 && jList.getSelectedIndex() < 0) {
            jList.setSelectedIndex(0);
        }
    }
}

From source file:org.yccheok.jstock.gui.IndicatorPanel.java

private void syncJListWithIndicatorProjectManager(JList jList,
        IndicatorProjectManager indicatorProjectManager) {
    final String projectName = (String) jList.getSelectedValue();
    boolean isProjectNameBeingRemoved = false;
    int newSelection = -1;

    final ListModel listModel = jList.getModel();
    for (int i = 0; i < listModel.getSize(); i++) {
        if (indicatorProjectManager.contains(listModel.getElementAt(i).toString()) == false) {
            // Remove from JList, as it is not found in indicator project manager.
            Object removedObject = ((DefaultListModel) listModel).remove(i);
            if (projectName.equals(removedObject)) {
                isProjectNameBeingRemoved = true;
                newSelection = i;/* w  ww.ja va 2s  .  c om*/
            }
            i--;
        }
    }
    for (int i = 0; i < indicatorProjectManager.getNumOfProject(); i++) {
        final String p = indicatorProjectManager.getProject(i);
        if (((DefaultListModel) listModel).contains(p) == false) {
            // Add to JList, as it is found in indicator project manager.
            ((DefaultListModel) listModel).addElement(p);
        }
    }

    if (!isProjectNameBeingRemoved) {
        // Ensure list cell renderer is being triggered.
        jList.setSelectedValue(projectName, true);
    } else {
        if (newSelection >= jList.getModel().getSize()) {
            // Select last row.
            jList.setSelectedIndex(jList.getModel().getSize() - 1);
        } else {
            jList.setSelectedIndex(newSelection);
        }
    }
}

From source file:util.ui.UiUtilities.java

/**
 * Moves Selected Items from one List to another
 *
 * @param fromList//from   w w  w . j  av a2 s  . c o  m
 *          Move from this List
 * @param toList
 *          Move into this List
 * @return Moved Elements
 */
public static Object[] moveSelectedItems(JList fromList, JList toList) {
    DefaultListModel fromModel = (DefaultListModel) fromList.getModel();
    DefaultListModel toModel = (DefaultListModel) toList.getModel();

    // get the selection
    int[] selection = fromList.getSelectedIndices();

    if (selection.length == 0) {
        return new Object[] {};
    }

    Object[] objects = new Object[selection.length];
    for (int i = 0; i < selection.length; i++) {
        objects[i] = fromModel.getElementAt(selection[i]);
    }

    // get the target insertion position
    int targetPos = toList.getMaxSelectionIndex();
    if (targetPos == -1) {
        targetPos = toModel.getSize();
    } else {
        targetPos++;
    }

    // suppress updates on both lists
    if (selection.length >= 5) {
        fromList.setModel(new DefaultListModel());
        toList.setModel(new DefaultListModel());
    }

    // move the elements
    for (int i = selection.length - 1; i >= 0; i--) {
        Object value = fromModel.remove(selection[i]);
        toModel.add(targetPos, value);
    }

    if (selection.length >= 5) {
        fromList.setModel(fromModel);
        toList.setModel(toModel);
    }

    // change selection of the fromList
    if (fromModel.getSize() > 0) {
        int newSelection = selection[0];
        if (newSelection >= fromModel.getSize()) {
            newSelection = fromModel.getSize() - 1;
        }
        fromList.setSelectedIndex(newSelection);
    }

    if (selection.length >= 5) {
        fromList.repaint();
        fromList.revalidate();
        toList.repaint();
        toList.revalidate();
    }

    // change selection of the toList
    toList.setSelectionInterval(targetPos, targetPos + selection.length - 1);

    // ensure the selection is visible
    toList.ensureIndexIsVisible(toList.getMaxSelectionIndex());
    toList.ensureIndexIsVisible(toList.getMinSelectionIndex());

    return objects;
}