
com.globalmentor.swing.SimpleListCellRenderer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of globalmentor-swing Show documentation
Show all versions of globalmentor-swing Show documentation
GlobalMentor Java Swing library.
The newest version!
/*
* Copyright © 1996-2009 GlobalMentor, Inc.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package com.globalmentor.swing;
import java.awt.*;
import javax.swing.*;
/**
* An class for simple rendering of custom strings and icons in a list cell. A child class should override
* {@link SimpleListCellRenderer#getListCellRendererString(Object)} and return the correct string for display. If that method is not overridden, the value will
* be converted to a string by default.
*
* If it is desired that an icon be displayed, a child class should override getListCellRendererIcon()
and return the correct icon for display.
*
* @author Garret Wilson
*/
public class SimpleListCellRenderer extends DefaultListCellRenderer {
/** Default constructor. */
public SimpleListCellRenderer() {
}
/**
* @return A renderer with the correct text for this list item.
* @param list The list component.
* @param value The value of this list item.
* @param index The index of this list item.
* @param isSelected Whether the list item is selected.
* @param cellHasFocus Whether the list item has the focus.
*/
public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
final Component component = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); //get the default component
final String string = getListCellRendererString(list, value, index, isSelected, cellHasFocus); //get the string to display
final Icon icon = getListCellRendererIcon(list, value, index, isSelected, cellHasFocus); //get the icon to display
//TODO assert the string is not null
assert component instanceof JLabel : "List cell renderer component is not a JLabel";
final JLabel label = (JLabel)component; //cast the component to a label, assuming that the component is a JLabel (which is likely the implementation used by the default)
label.setText(string); //set the correct text
if(icon != null) //if we have an icon
label.setIcon(icon); //set the icon
return component; //return the component, now using our custom text
}
/**
* Retrieves text for a list item. This can be overridden in a derived class to return custom text for the specified value. This default version delegates to
* the one-parameter version.
* @param list The list component.
* @param value The value of this list item.
* @param index The index of this list item.
* @param isSelected Whether the list item is selected.
* @param cellHasFocus Whether the list item has the focus.
* @return The correct text for this list item.
*/
protected String getListCellRendererString(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
return getListCellRendererString(value); //delegate to the simple version
}
/**
* Retrieves text for a list item. This can be overridden in a derived class to return custom text for the specified value. This default version is called
* from the multi-parameter version, and returns the object's default toString()
value.
* @param value The value of this list item.
* @return The correct text for this list item.
*/
protected String getListCellRendererString(final Object value) {
return value.toString(); //return the string value of the object by default
}
/**
* Retrieves an icon for a list item. This can be overridden in a derived class to return a custom icon for the specified value. This default version
* delegates to the one-parameter version.
* @param list The list component.
* @param value The value of this list item.
* @param index The index of this list item.
* @param isSelected Whether the list item is selected.
* @param cellHasFocus Whether the list item has the focus.
* @return The correct icon for this list item, or null
if no icon should be displayed.
*/
protected Icon getListCellRendererIcon(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
return getListCellRendererIcon(value); //return the result of the simple version
}
/**
* Retrieves an icon for a list item. This can be overridden in a derived class to return a custom icon for the specified value. This default version is
* called from the multi-parameter version, and returns null
.
* @param value The value of this list item.
* @return The correct icon for this list item, or null
if no icon should be displayed.
*/
protected Icon getListCellRendererIcon(final Object value) {
return null; //we don't know the icon in this version
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy