下面列出了javax.swing.JList#getSelectionBackground ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
LibsRenderer(@NonNull final JList list) {
this.root = new JLabel();
this.root.setHorizontalAlignment(LEFT);
this.root.setOpaque(false);
this.root.setFont(list.getFont());
this.profile = new JLabel();
this.profile.setHorizontalAlignment(RIGHT);
this.profile.setOpaque(false);
this.profile.setFont(list.getFont());
this.container = new JPanel();
this.container.setLayout(new BorderLayout());
this.container.add (this.root, BorderLayout.WEST);
this.container.add (this.profile, BorderLayout.EAST);
fgColor = list.getForeground();
fgColorLighter = new Color(
Math.min(255, fgColor.getRed() + LIGHTER_COLOR_COMPONENT),
Math.min(255, fgColor.getGreen() + LIGHTER_COLOR_COMPONENT),
Math.min(255, fgColor.getBlue() + LIGHTER_COLOR_COMPONENT));
bgColor = new Color(list.getBackground().getRGB());
bgSelectionColor = list.getSelectionBackground();
fgSelectionColor = list.getSelectionForeground();
}
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);
}
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();
}
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();
}
@Override
public Component getListCellRendererComponent(JList list, Object pValue, int pIndex, boolean pSelected, boolean pHasFocus) {
try {
setOpaque(true);
String value = (String) pValue;
String[] amountAndName = value.split(" ");
String name = amountAndName[1].trim();
UnitHolder unit = DataHolder.getSingleton().getUnitByPlainName(name);
ImageIcon icon = ImageManager.getUnitIcon(unit);
setIcon(icon);
setHorizontalTextPosition(SwingConstants.LEFT);
setHorizontalAlignment(SwingConstants.CENTER);
setText(amountAndName[0]);
setOpaque(true);
if (pSelected) {
setForeground(list.getSelectionForeground());
super.setBackground(list.getSelectionBackground());
} else {
setForeground(Color.BLACK);
if (pIndex % 2 == 0) {
setBackground(Constants.DS_ROW_B);
} else {
setBackground(Constants.DS_ROW_A);
}
}
} catch (Exception e) {
//cast problem
setText("-Fehler-");
}
return this;
}
@Override
public Component getListCellRendererComponent(JList list, Object pValue, int pIndex, boolean pSelected, boolean pHasFocus) {
// Component c = super.getListCellRendererComponent(list, pValue, pIndex, pSelected, pHasFocus);
setOpaque(true);
if (pSelected) {
setForeground(list.getSelectionForeground());
super.setBackground(list.getSelectionBackground());
} else {
if (pIndex % 2 == 0) {
setBackground(Constants.DS_ROW_B);
} else {
setBackground(Constants.DS_ROW_A);
}
}
try {
setHorizontalAlignment(SwingConstants.CENTER);
if (pValue == null) {
//no icon!?
setText("-");
setIcon(null);
} else {
setText("");
setIcon(ImageManager.getUnitIcon((UnitHolder) pValue));
}
} catch (Exception e) {
//cast problem
}
return this;
}
@Override
public Component getListCellRendererComponent(JList list, Object pValue, int pIndex, boolean pSelected, boolean pHasFocus) {
try {
setOpaque(true);
TroopSplit value = (TroopSplit) pValue;
StringBuilder builder = new StringBuilder();
builder.append(value.getVillage());
builder.append(" [");
int splitCount = value.getSplitCount();
builder.append(splitCount);
builder.append((splitCount == 1) ? " Split" : " Splits");
builder.append("]");
setText(builder.toString());
if (pSelected) {
setForeground(list.getSelectionForeground());
super.setBackground(list.getSelectionBackground());
} else {
if (splitCount == 0) {
setForeground(Color.RED);
} else if (splitCount == 1) {
setForeground(Color.ORANGE.darker());
} else {
setForeground(Color.GREEN.darker());
}
if (pIndex % 2 == 0) {
setBackground(Constants.DS_ROW_B);
} else {
setBackground(Constants.DS_ROW_A);
}
}
} catch (Exception e) {
//cast problem
setText("-Fehler-");
}
return this;
}
@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;
}
@Override
public Component getListCellRendererComponent(JList list, Object pValue, int pIndex, boolean pSelected, boolean pHasFocus) {
try {
setOpaque(true);
TimeSpan span = (TimeSpan) pValue;
setBorder(null);
switch (span.getDirection()) {
case SEND:
setIcon(new ImageIcon(TimeFrameListCellRenderer.class.getResource("/res/ui/move_out.png")));
break;
case ARRIVE:
setIcon(new ImageIcon(TimeFrameListCellRenderer.class.getResource("/res/ui/move_in.png")));
break;
case NONE:
setIcon(null);
setForeground(Constants.DS_BACK_LIGHT);
setBackground(Constants.DS_BACK_LIGHT);
setSize(list.getWidth(), 2);
/// setBorder(LineBorder.createGrayLineBorder());
setText(" ");
return this;
}
if (pSelected) {
setForeground(list.getSelectionForeground());
super.setBackground(list.getSelectionBackground());
} else {
if (!span.isValid()) {
setBackground(Color.red);
setForeground(Color.white);
setToolTipText(span.getValidityInfo());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
setToolTipText(null);
}
}
setText(span.toString());
} catch (Exception e) {
//cast problem
}
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;
}
@Override
public Component getListCellRendererComponent(
JList<? extends Page> list,
Page value,
@SuppressWarnings("unused") int index,
boolean isSelected,
@SuppressWarnings("unused") boolean cellHasFocus) {
// Retrieve data
String pageName = (value != null) ? value.toString() : "";
String text = pageName;
Boolean disambiguation = null;
Boolean exist = null;
boolean redirect = false;
InternalLinkCount count = null;
if (value != null) {
Page pageElement = value;
pageName = pageElement.getTitle();
text = pageName;
disambiguation = pageElement.isDisambiguationPage();
exist = pageElement.isExisting();
count = (analysis != null) ? analysis.getLinkCount(pageElement) : null;
if (showCountOccurrence &&
(count != null) &&
(count.getTotalLinkCount() > 0)) {
text += " → " + count.getTotalLinkCount();
}
redirect = pageElement.getRedirects().isRedirect();
if (redirect && showRedirectBacklinks) {
Integer backlinks = pageElement.getBacklinksCountInMainNamespace();
if ((backlinks != null) && (backlinks.intValue() > 0)) {
text += " ← " + backlinks;
}
}
}
// Text
setText(text);
// Color
Color background = isSelected ? list.getSelectionBackground() : Color.WHITE;
Color foreground = isSelected ? list.getSelectionForeground() : list.getForeground();
if (showDisambiguation) {
if (disambiguation == null) {
if (!isSelected) {
foreground = Color.DARK_GRAY;
}
} else if (disambiguation.booleanValue()) {
if (count == null) {
foreground = Color.RED;
} else if ((count.getInternalLinkCount() > 0) || (count.getIncorrectTemplateCount() > 0)) {
foreground = Color.RED;
} else if ((count.getHelpNeededCount() > 0)) {
foreground = Color.ORANGE;
} else {
foreground = Color.BLUE;
}
}
} else if (pageProperties != null) {
String property = pageProperties.getProperty(pageName);
if (Configuration.VALUE_PAGE_NORMAL.equals(property)) {
foreground = Color.GREEN;
} else if (Configuration.VALUE_PAGE_HELP_NEEDED.equals(property)) {
foreground = Color.ORANGE;
}
}
setBackground(background);
setForeground(foreground);
// Font
if (showMissing && Boolean.FALSE.equals(exist)) {
setFont(missingFont);
} else if (showRedirect && redirect) {
setFont(redirectFont);
} else {
setFont(normalFont);
}
return this;
}