Example usage for javax.swing JList setSelectedValue

List of usage examples for javax.swing JList setSelectedValue

Introduction

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

Prototype

public void setSelectedValue(Object anObject, boolean shouldScroll) 

Source Link

Document

Selects the specified object from the list.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String[] items = { "A", "B", "C", "D" };
    JList list = new JList(items);

    boolean scrollIntoView = true;
    list.setSelectedValue("B", scrollIntoView);
}

From source file:main.UIController.java

/************* SETTINGS ****************/

private void fillSettingsForm() {
    UI ui = this.getUi();
    Preferences data = this.getCurrentPreferences();
    /* Country and city */
    String city = data.get(FIELD_CITY, DEF_CITY);
    String country = data.get(FIELD_COUNTRY, DEF_COUNTRY);
    ui.getCity().setText(city);//from   w w w.  j ava2s  .c o m
    ui.getCountry().setText(country);
    /* Lang */
    LangManager langManager = LangManager.getInstance();
    String lang = langManager.getDefinedLang().getShortName();
    JComboBox langsCombo = ui.getLangCombo();
    langsCombo.setSelectedIndex(Arrays.asList(LangManager.getLanguagesShort()).indexOf(lang));
    /* Time Zone */
    String[] tzs = TimeZone.getAvailableIDs();
    Arrays.sort(tzs);
    JList list = ui.getTimeZone();
    list.setListData(tzs);
    String timezone = data.get(FIELD_TIMEZONE, DEF_TIMEZONE);
    list.setSelectedValue(timezone, true);
}

From source file:com.raddle.tools.MergeMain.java

private void selectLine(JList list, PropertyLine line) {
    DefaultComboBoxModel model = (DefaultComboBoxModel) list.getModel();
    for (int i = 0; i < model.getSize(); i++) {
        PropertyLine mLine = (PropertyLine) model.getElementAt(i);
        if (mLine.getKey().equals(line.getKey())) {
            list.setSelectedValue(mLine, true);
            return;
        }//w w  w. jav  a2  s  .c  o  m
    }
}

From source file:org.genedb.jogra.plugins.TermRationaliser.java

/**
 * PRIVATE HELPER METHODS/*from ww  w  .j ava 2s  .c om*/
 */

/* 
 * Synchronise to the item selected in sourcelist 
 */
private void synchroniseLists(JList sourceList, JList targetList) {
    Term term = (Term) sourceList.getSelectedValue();
    targetList.setSelectedValue(term, true);
    targetList.ensureIndexIsVisible(targetList.getSelectedIndex());
}

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;//from w  ww  .j a va  2s.co m
            }
            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);
        }
    }
}