Java JList Select adaptToList(final JComponent renderer, JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)

Here you can find the source of adaptToList(final JComponent renderer, JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)

Description

Adapts the given component to the specified table instance, making it look just as a DefaultTableCellRenderer would.

License

Apache License

Parameter

Parameter Description
renderer Your renderer component.
list The list the renderer is being used by.
value see ListCellRenderer.
index see ListCellRenderer.
isSelected see ListCellRenderer.
cellHasFocus see ListCellRenderer.

Declaration

public static void adaptToList(final JComponent renderer, JList list, Object value, int index,
        boolean isSelected, boolean cellHasFocus) 

Method Source Code

//package com.java2s;
/**// ww w  .  j a  v  a 2s .  com
 * A small utility class which can be used to mimic the DefaultXXXRenderer behaviour for custom renderer
 * implementations. Basically, it offers methods for tables, lists and treetables, which expose the same signature as
 * the according renderer interface, with the addition of a JComponent. The specified component will be configured in
 * the same way as the default renderer implementation regardings fonts, borders and such.
 * <p/>
 * <hr/> Copyright 2006-2012 Torsten Heup
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

public class Main {
    /**
     * Adapts the given component to the specified table instance, making it look just as a DefaultTableCellRenderer
     * would.
     *
     * @param renderer     Your renderer component.
     * @param list         The list the renderer is being used by.
     * @param value        see ListCellRenderer.
     * @param index        see ListCellRenderer.
     * @param isSelected   see ListCellRenderer.
     * @param cellHasFocus see ListCellRenderer.
     */
    public static void adaptToList(final JComponent renderer, JList list, Object value, int index,
            boolean isSelected, boolean cellHasFocus) {

        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;
        }

        if (isSelected) {
            renderer.setBackground(bg == null ? list.getSelectionBackground() : bg);
            renderer.setForeground(fg == null ? list.getSelectionForeground() : fg);
        } else {
            renderer.setBackground(list.getBackground());
            renderer.setForeground(list.getForeground());
        }

        renderer.setEnabled(list.isEnabled());
        renderer.setFont(list.getFont());

        Border border = null;
        if (cellHasFocus) {
            if (isSelected) {
                border = UIManager.getBorder("List.focusSelectedCellHighlightBorder");
            }
            if (border == null) {
                border = UIManager.getBorder("List.focusCellHighlightBorder");
            }
        } else {
            border = BorderFactory.createEmptyBorder(1, 1, 1, 1);
        }

        renderer.setBorder(border);
    }
}

Related

  1. applyDefaultDisplaySettings(JList list, int index, boolean selected, boolean cellHasFocus, JComponent renderer)
  2. canMoveSelectedItemsUp(JList list)
  3. createStringFromSelectionList( JList aListComponent, boolean createQuotes)
  4. downListSelectedIndex(JList sourceList)