Java Utililty Methods JList Select

List of utility methods to do JList Select

Description

The list of methods to do JList Select are organized into topic(s).

Method

voidadaptToList(final JComponent renderer, JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
Adapts the given component to the specified table instance, making it look just as a DefaultTableCellRenderer would.
renderer.setComponentOrientation(list.getComponentOrientation());
Color bg = null;
Color fg = null;
JList.DropLocation dropLocation = list.getDropLocation();
if (dropLocation != null && !dropLocation.isInsert() && dropLocation.getIndex() == index) {
    bg = UIManager.getColor("List.dropCellBackground");
    fg = UIManager.getColor("List.dropCellForeground");
    isSelected = true;
...
voidapplyDefaultDisplaySettings(JList list, int index, boolean selected, boolean cellHasFocus, JComponent renderer)
Uses DefaultListCellRenderer to apply default settings to the provided renderer component.
JComponent defRendererComponent = (JComponent) defaultRenderer.getListCellRendererComponent(list,
        cellHasFocus, index, selected, cellHasFocus);
renderer.setForeground(defRendererComponent.getForeground());
renderer.setBackground(defRendererComponent.getBackground());
renderer.setComponentOrientation(defRendererComponent.getComponentOrientation());
if (cellHasFocus) {
    renderer.requestFocus();
renderer.setBorder(defRendererComponent.getBorder());
renderer.setEnabled(defRendererComponent.isEnabled());
renderer.setFont(defRendererComponent.getFont());
booleancanMoveSelectedItemsUp(JList list)
can Move Selected Items Up
int[] indices = list.getSelectedIndices();
return indices.length > 0 && indices[0] > 0;
StringcreateStringFromSelectionList(JList aListComponent, boolean createQuotes)
Creates a String representation of the selected items in a JList component

Example of output with createQuotes : ["123","456","789"]
Example of output with !createQuotes: [123,456,789]
Returns '[]' when no selected items were detected in the JList.
String aList = "[";
if (aListComponent.getModel().getSize() > 0) {
    int[] selectedIndices = aListComponent.getSelectedIndices();
    for (int i = 0; i < selectedIndices.length; i++) {
        if (i > 0) {
            aList += ",";
        if (createQuotes) {
...
voiddownListSelectedIndex(JList sourceList)
down List Selected Index
int selectedIndex = sourceList.getSelectedIndex();
DefaultListModel listModel = (DefaultListModel) sourceList.getModel();
if (selectedIndex < listModel.size() - 1) {
    Object selectedValue = listModel.get(selectedIndex);
    Object newSelectedValue = listModel.get(selectedIndex + 1);
    listModel.set(selectedIndex, newSelectedValue);
    listModel.set(selectedIndex + 1, selectedValue);
    sourceList.setSelectedIndex(selectedIndex + 1);
...
voidensureSelectionIsVisible(final JList list)
ensure Selection Is Visible
voidfillSelectionListFromString(JList aListComponent, String theList, boolean removeQuotes)
Selects items in a JList component using a String representation of the to-be selected contents

Example of input argument theList with !removeQuotes : ["123","456","789"]
Example of input argument theList with removeQuotes: [123,456,789]
String aList = theList;
if (aList.startsWith("[")) {
    aList = aList.substring(1, aList.length());
if (aList.endsWith("]")) {
    aList = aList.substring(0, aList.length() - 1);
if (!aList.equals("")) {
...
voidfireSelectRow(final JList list, final Object value)
fire Select Row
if (value != null) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            list.setSelectedValue(value, true);
    });
voidfireSelectRows(final JList list, final int[] rows)
fire Select Rows
if (rows != null) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                list.setSelectedIndices(rows);
            } catch (Exception ex) {
    });
CollectiongetSelectedIndecies(final ListSelectionModel lsm)
Returns an Collection with all indecies that are selected in the inputted ListSelectionModel .
Collection<Integer> selectedIndecies = new ArrayList<Integer>();
if (!lsm.isSelectionEmpty()) {
    int minIndex = lsm.getMinSelectionIndex();
    int maxIndex = lsm.getMaxSelectionIndex();
    for (int i = minIndex; i <= maxIndex; i++) {
        if (lsm.isSelectedIndex(i)) {
            selectedIndecies.add(i);
return selectedIndecies;