下面列出了javax.swing.JList#getBackground ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
if (!(value instanceof TreeListNode)) {
//shoudln't happen
return new JLabel();
}
TreeListNode node = (TreeListNode) value;
int rowHeight = list.getFixedCellHeight();
int rowWidth = list.getVisibleRect().width;
int dropIndex = -1;
DropLocation dropLocation = list.getDropLocation();
if (dropLocation != null && !dropLocation.isInsert()) {
dropIndex = dropLocation.getIndex();
}
boolean isDropTarget = dropIndex == index;
isSelected = isSelected || isDropTarget;
Color background = isSelected ? list.getSelectionBackground() : list.getBackground();
Color foreground = isSelected ? list.getSelectionForeground() : list.getForeground();
return node.getRenderer(foreground, background, isSelected, cellHasFocus, rowHeight, rowWidth);
}
@Override
public Component getListCellRendererComponent( JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus ) {
if (!(value instanceof ListNode)) {
//shoudln't happen
return new JLabel();
}
if( list instanceof SelectionList ) {
isSelected |= index == ((SelectionList)list).getMouseOverRow();
}
ListNode node = (ListNode) value;
int rowHeight = list.getFixedCellHeight();
int rowWidth = list.getWidth();
JScrollPane scroll = ( JScrollPane ) SwingUtilities.getAncestorOfClass( JScrollPane.class, list);
if( null != scroll )
rowWidth = scroll.getViewport().getWidth();
Color background = isSelected ? list.getSelectionBackground() : list.getBackground();
Color foreground = isSelected ? list.getSelectionForeground() : list.getForeground();
return node.getListRenderer(foreground, background, isSelected, cellHasFocus, rowHeight, rowWidth);
}
@Override
public Component getListCellRendererComponent(final JList list, Object value, final int index,
final boolean isSelected, boolean cellHasFocus) {
Color back = (index % 2 == 1) ? list.getBackground() : evensColor;
if (value instanceof Method) {
final Method method = (Method) value;
return new MethodCell(list, isSelected, back, method, dlg.getTheClass());
} else if (value instanceof Field) {
Field field = (Field) value;
return new FieldCell(list, isSelected, back, field, dlg.getTheClass());
} else if (value instanceof Constructor) {
Constructor cons = (Constructor) value;
return new ConstructorCell(list, isSelected, back, cons, dlg.getTheClass());
} else {
Component comp = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
comp.setBackground(back);
return comp;
}
}
public Renderer(JList list) {
setFont(list.getFont());
fgColor = list.getForeground();
bgColor = list.getBackground();
bgColorDarker = new Color(Math.abs(bgColor.getRed() - DARKER_COLOR_COMPONENT),
Math.abs(bgColor.getGreen() - DARKER_COLOR_COMPONENT),
Math.abs(bgColor.getBlue() - DARKER_COLOR_COMPONENT));
bgSelectionColor = list.getSelectionBackground();
fgSelectionColor = list.getSelectionForeground();
}
public Renderer( JList list ) {
setFont( list.getFont() );
fgColor = list.getForeground();
bgColor = list.getBackground();
bgColorDarker = new Color(
Math.abs(bgColor.getRed() - DARKER_COLOR_COMPONENT),
Math.abs(bgColor.getGreen() - DARKER_COLOR_COMPONENT),
Math.abs(bgColor.getBlue() - DARKER_COLOR_COMPONENT)
);
bgSelectionColor = list.getSelectionBackground();
fgSelectionColor = list.getSelectionForeground();
}
/** Configures a list cell renderer and sets up sizing and the
* backing image from it */
public boolean configure (Object nd, JScrollPane tv, JList list, int row) {
boolean sameVn = setLastRendereredObject(nd);
boolean sameComp = setLastRenderedScrollPane (tv);
Component renderer = null;
bg = list.getBackground();
boolean sel = list.isSelectionEmpty() ? false :
list.getSelectionModel().isSelectedIndex(row);
renderer = list.getCellRenderer().getListCellRendererComponent(list, nd, row, sel, false);
if (renderer != null) {
setComponent (renderer, list);
}
return true;
}
@Override
public Component getListCellRendererComponent(JList<? extends Object> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
// #93658: GTK needs name to render cell renderer "natively"
setName("ComboBox.listRenderer"); // NOI18N
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
Color bgColor = list.getBackground();
Color bgColorDarker = new Color(
Math.abs(bgColor.getRed() - 10),
Math.abs(bgColor.getGreen() - 10),
Math.abs(bgColor.getBlue() - 10));
setBackground(index % 2 == 0 ? bgColor : bgColorDarker);
setForeground(list.getForeground());
}
setFont(list.getFont());
if (value instanceof FrameworkCommand) {
FrameworkCommand task = (FrameworkCommand) value;
String descripton = task.getDescription();
StringBuilder text = new StringBuilder(100);
text.append("<html>"); // NOI18N
text.append("<b>").append(task.getDisplayName()).append("</b>"); // NOI18N
if (descripton != null) {
text.append(" : ").append(descripton); // NOI18N
}
text.append("</html>"); // NOI18N
setText(text.toString());
} else {
setText(value.toString());
}
return this;
}
public Renderer(JList list) {
setFont(list.getFont());
fgColor = list.getForeground();
bgColor = list.getBackground();
bgColorDarker = new Color(
Math.abs(bgColor.getRed() - DARKER_COLOR_COMPONENT),
Math.abs(bgColor.getGreen() - DARKER_COLOR_COMPONENT),
Math.abs(bgColor.getBlue() - DARKER_COLOR_COMPONENT));
bgSelectionColor = list.getSelectionBackground();
fgSelectionColor = list.getSelectionForeground();
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
// #93658: GTK needs name to render cell renderer "natively"
setName("ComboBox.listRenderer"); // NOI18N
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
Color bgColor = list.getBackground();
Color bgColorDarker = new Color(
Math.abs(bgColor.getRed() - 10),
Math.abs(bgColor.getGreen() - 10),
Math.abs(bgColor.getBlue() - 10));
setBackground(index % 2 == 0 ? bgColor : bgColorDarker);
setForeground(list.getForeground());
}
setFont(list.getFont());
if (value instanceof GrailsCommand) {
GrailsCommand task = ((GrailsCommand) value);
String descripton = task.getDescription();
// if (descripton == null) {
// setForeground(Color.GRAY);
// }
StringBuilder text = new StringBuilder("<html>"); // NOI18N
text.append("<b>").append(task.getCommand()).append("</b>"); // NOI18N
if (descripton != null) {
text.append(" : ").append(descripton); // NOI18N
}
text.append("</html>"); // NOI18N
setText(text.toString());
} else {
setText(value.toString());
}
return this;
}
@Override
public Component getListCellRendererComponent(JList<? extends AuthenticationCheckboxListItem> list, AuthenticationCheckboxListItem value,
int index, boolean isSelected, boolean cellHasFocus) {
final Color bc;
final Color fc;
if (isSelected) {
bc = UIManager.getColor("List.selectionBackground"); //NOI18N
fc = UIManager.getColor("List.selectionForeground"); //NOI18N
} else {
bc = list.getBackground();
fc = list.getForeground();
}
setBackground(bc); // NOI18N
setForeground(fc); // NOI18N
label.setBackground(bc);
label.setForeground(fc);
label.setText(value.getDisplayName());
label.setFont(list.getFont());
check.setSelected(value.isSelected());
check.setBackground(bc);
check.setForeground(fc);
check.setEnabled(list.isEnabled());
Border border;
if (cellHasFocus) {
border = focusBorder;
} else {
border = noFocusBorder;
}
setBorder(border);
return this;
}
public Renderer(JList list) {
setFont(list.getFont());
fgColor = list.getForeground();
bgColor = list.getBackground();
bgColorDarker = new Color(Math.abs(bgColor.getRed() - DARKER_COLOR_COMPONENT),
Math.abs(bgColor.getGreen() - DARKER_COLOR_COMPONENT),
Math.abs(bgColor.getBlue() - DARKER_COLOR_COMPONENT));
bgSelectionColor = list.getSelectionBackground();
fgSelectionColor = list.getSelectionForeground();
}
@Override
public Component getListCellRendererComponent(final JList<? extends NamedSelection> list, final NamedSelection value, final int index,
final boolean isSelected, final boolean cellHasFocus) {
lblNamedSelection.setText(value.getName());
if (value.getID() < 0) { // Check if this is the 'current selection' entity, and act accordingly:
// We have the 'current selection' list item, which is not actually a named selection:
lblNamedSelection.setText(Bundle.NamedSelection_CurrentSelection());
lblNamedSelection.setFont(new Font(lblNamedSelection.getFont().getFontName(),
Font.ITALIC, lblNamedSelection.getFont().getSize()));
lblShortcutKey.setText("");
lblShortcutKey.setVisible(false);
lblShortcutText.setVisible(false);
super.setLockedStatus(false);
} else { // We have a regular named selection, so renderer it (along with shortcut if applicable).
lblNamedSelection.setFont(new Font(lblNamedSelection.getFont().getFontName(),
Font.PLAIN, lblNamedSelection.getFont().getSize()));
if (value.getHotkey() != null) {
lblShortcutKey.setText("<Ctrl-" + value.getHotkey() + ">");
lblShortcutKey.setVisible(true);
lblShortcutText.setVisible(true);
} else {
lblShortcutKey.setText("");
lblShortcutKey.setVisible(false);
lblShortcutText.setVisible(false);
}
}
if (isSelected) { // Change the colour if the named selection has been selected:
super.setBackground(list.getSelectionBackground());
super.setLockedStatusSelected(value.isLocked());
lblNamedSelection.setForeground(list.getSelectionForeground());
lblShortcutKey.setForeground(list.getSelectionForeground());
lblShortcutText.setForeground(list.getSelectionForeground());
} else { // Change colouring for non selected items:
super.setBackground(list.getBackground());
super.setLockedStatus(value.isLocked());
lblNamedSelection.setForeground(list.getForeground());
lblShortcutKey.setForeground(list.getSelectionBackground()); // Make the shortcut key a contrasting colour.
lblShortcutText.setForeground(java.awt.SystemColor.controlDkShadow); // Less prominent than selection name.
}
return this;
}
/**
* @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList,
* java.lang.Object, int, boolean, boolean)
*/
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
boolean isSelected, boolean hasFocus) {
JPanel newPanel = new JPanel();
newPanel.setLayout(new OverlayLayout(newPanel));
Color bgColor;
if (isSelected)
bgColor = list.getSelectionBackground();
else
bgColor = list.getBackground();
newPanel.setBackground(bgColor);
if (hasFocus) {
Border border = null;
if (isSelected)
border = UIManager.getBorder("List.focusSelectedCellHighlightBorder");
if (border == null)
border = UIManager.getBorder("List.focusCellHighlightBorder");
if (border != null)
newPanel.setBorder(border);
}
if (value != null) {
if (value instanceof JComponent) {
newPanel.add((JComponent) value);
} else {
JLabel newLabel = new JLabel(value.toString());
if (font != null)
newLabel.setFont(font);
else if (list.getFont() != null)
newLabel.setFont(list.getFont());
newPanel.add(newLabel);
}
}
return newPanel;
}