При рендеринге выбранные цвета никогда не используются, поэтому выбранная строка выглядит так же, как и любая другая строка. Вот соответствующий код.
Код: Выделить всё
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
SearchItem searchItem = (SearchItem)value;
MigLayout layout = new MigLayout(MigLayoutUtils.createDefaultLayoutConstraints().insets("2"));
JPanel panel = new JPanel(layout);
JLabel primaryLabel = new JLabel(searchItem.getTitle() + " (" + searchItem.getDescription() + ")", SwingConstants.LEFT);
JLabel secondaryLabel = new JLabel(searchItem.getPath().toString(), SwingConstants.RIGHT);
Color foreground;
Color background;
if (isSelected) {
// copying the color here works around the color returned from the below being a UIResource
// that gets auto-converted to normal un-selected colors
// TODO: investigate further why this is and see if there's a better solution
// foreground = list.getSelectionForeground();
// background = list.getSelectionBackground();
foreground = SwingUtils.copyColor(list.getSelectionForeground());
background = SwingUtils.copyColor(list.getSelectionBackground());
} else {
foreground = list.getForeground();
background = list.getBackground();
}
primaryLabel.setFont(list.getFont());
secondaryLabel.setFont(list.getFont());
panel.setBackground(background);
primaryLabel.setForeground(foreground);
secondaryLabel.setForeground(foreground);
panel.add(primaryLabel, "growx, pushx");
panel.add(secondaryLabel);
return panel;
}
}
Я подозреваю, что это связано с тем, что статус/флаг «выбрано» не установлен в JLabel. Есть несколько методов SynthLookAndFeel.setSelectedUI/getSelectedUIState, которые выглядят так, как будто им удается добавить это состояние в JLabels для целей рендеринга. Однако я не уверен, как и следует ли мне взаимодействовать с этим, чтобы получить правильное состояние для отображения моих собственных ярлыков.
Буду благодарен за любую информацию.
Подробнее здесь: https://stackoverflow.com/questions/798 ... lor-scheme
Мобильная версия