需要时更改 JList 项目颜色
Change JList item colour when I want
我正在尝试构建一个 JList
,其中某些项目的背景颜色与其他项目不同,但仅在我需要时才会出现。
我一直在尝试做这样的事情,但它会产生错误。
static DefaultListModel<String> model = new DefaultListModel<>();
for(int K=0;K<Collectionsize();K++){
Jlist.setForeground(Color.red);
model.addElement(type 1);
for(int I=0;I<(subcollection.size();I++){
Jlist.setForeground(Color.white);
model.addElement(type 2);
}
}
类型 1 和类型 2 之间没有模式,所以我想在需要时更改颜色,而不是依赖 if 语句。
我看到很多人在谈论构建自定义渲染 类 但我的目标是一些更简单的东西。
注意:每个项目包含两条信息的项目列表更适合显示在 table 而不是列表中,但如果需要,您可以将其调整为列表。同样的基本原则适用(使用渲染组件)。
这就是我的意思:
这是通过这个渲染实现的class:
class TrackCellRenderer extends DefaultTableCellRenderer {
HashMap<String, Color> colorMap = new HashMap<>();
Random r = new Random();
@Override
public Component getTableCellRendererComponent(
JTable table,
Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
Component c = super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
JLabel l = (JLabel) c;
String s = (String) value;
if (column == 0) {
if (!colorMap.containsKey(s)) {
Color clr = new Color(
150 + r.nextInt(105),
150 + r.nextInt(105),
150 + r.nextInt(105));
colorMap.put(s, clr);
}
Color color = colorMap.get(s);
l.setBackground(color);
l.setOpaque(true);
} else {
l.setOpaque(false);
}
return l;
}
}
注意:最好使用枚举而不是随机分配颜色,但是有 3 个相册和超过一百万种可能的颜色,我们应该很安全。
在这(两个class)的MCVE中只有100多行代码:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.*;
import java.util.*;
public class AlbumTrackTable {
private JComponent ui = null;
String[][] playList = {
{"The Will to Live", "Faded"},
{"The Will to Live", "Homeless Child"},
{"Oh Mercy", "Political Wrold"},
{"Oh Mercy", "What Was it You Wanted?"},
{"Red Sails in the Sunset", "Helps Me Helps You"},
{"Red Sails in the Sunset", "Redneck Wonderland"}
};
String[] columnNames = {"Album", "Track"};
AlbumTrackTable() {
initUI();
}
public void initUI() {
if (ui != null) {
return;
}
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
DefaultTableModel trackModel = new DefaultTableModel(playList, columnNames);
JTable table = new JTable(trackModel);
ui.add(new JScrollPane(table));
TableCellRenderer renderer = new TrackCellRenderer();
table.setDefaultRenderer(Object.class, renderer);
table.setAutoCreateRowSorter(true);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
AlbumTrackTable o = new AlbumTrackTable();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
class TrackCellRenderer extends DefaultTableCellRenderer {
HashMap<String, Color> colorMap = new HashMap<>();
Random r = new Random();
@Override
public Component getTableCellRendererComponent(
JTable table,
Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
Component c = super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
JLabel l = (JLabel) c;
String s = (String) value;
if (column == 0) {
if (!colorMap.containsKey(s)) {
Color clr = new Color(
150 + r.nextInt(105),
150 + r.nextInt(105),
150 + r.nextInt(105));
colorMap.put(s, clr);
}
Color color = colorMap.get(s);
l.setBackground(color);
l.setOpaque(true);
} else {
l.setOpaque(false);
}
return l;
}
}
我正在尝试构建一个 JList
,其中某些项目的背景颜色与其他项目不同,但仅在我需要时才会出现。
我一直在尝试做这样的事情,但它会产生错误。
static DefaultListModel<String> model = new DefaultListModel<>();
for(int K=0;K<Collectionsize();K++){
Jlist.setForeground(Color.red);
model.addElement(type 1);
for(int I=0;I<(subcollection.size();I++){
Jlist.setForeground(Color.white);
model.addElement(type 2);
}
}
类型 1 和类型 2 之间没有模式,所以我想在需要时更改颜色,而不是依赖 if 语句。
我看到很多人在谈论构建自定义渲染 类 但我的目标是一些更简单的东西。
注意:每个项目包含两条信息的项目列表更适合显示在 table 而不是列表中,但如果需要,您可以将其调整为列表。同样的基本原则适用(使用渲染组件)。
这就是我的意思:
这是通过这个渲染实现的class:
class TrackCellRenderer extends DefaultTableCellRenderer {
HashMap<String, Color> colorMap = new HashMap<>();
Random r = new Random();
@Override
public Component getTableCellRendererComponent(
JTable table,
Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
Component c = super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
JLabel l = (JLabel) c;
String s = (String) value;
if (column == 0) {
if (!colorMap.containsKey(s)) {
Color clr = new Color(
150 + r.nextInt(105),
150 + r.nextInt(105),
150 + r.nextInt(105));
colorMap.put(s, clr);
}
Color color = colorMap.get(s);
l.setBackground(color);
l.setOpaque(true);
} else {
l.setOpaque(false);
}
return l;
}
}
注意:最好使用枚举而不是随机分配颜色,但是有 3 个相册和超过一百万种可能的颜色,我们应该很安全。
在这(两个class)的MCVE中只有100多行代码:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.*;
import java.util.*;
public class AlbumTrackTable {
private JComponent ui = null;
String[][] playList = {
{"The Will to Live", "Faded"},
{"The Will to Live", "Homeless Child"},
{"Oh Mercy", "Political Wrold"},
{"Oh Mercy", "What Was it You Wanted?"},
{"Red Sails in the Sunset", "Helps Me Helps You"},
{"Red Sails in the Sunset", "Redneck Wonderland"}
};
String[] columnNames = {"Album", "Track"};
AlbumTrackTable() {
initUI();
}
public void initUI() {
if (ui != null) {
return;
}
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
DefaultTableModel trackModel = new DefaultTableModel(playList, columnNames);
JTable table = new JTable(trackModel);
ui.add(new JScrollPane(table));
TableCellRenderer renderer = new TrackCellRenderer();
table.setDefaultRenderer(Object.class, renderer);
table.setAutoCreateRowSorter(true);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
AlbumTrackTable o = new AlbumTrackTable();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
class TrackCellRenderer extends DefaultTableCellRenderer {
HashMap<String, Color> colorMap = new HashMap<>();
Random r = new Random();
@Override
public Component getTableCellRendererComponent(
JTable table,
Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
Component c = super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
JLabel l = (JLabel) c;
String s = (String) value;
if (column == 0) {
if (!colorMap.containsKey(s)) {
Color clr = new Color(
150 + r.nextInt(105),
150 + r.nextInt(105),
150 + r.nextInt(105));
colorMap.put(s, clr);
}
Color color = colorMap.get(s);
l.setBackground(color);
l.setOpaque(true);
} else {
l.setOpaque(false);
}
return l;
}
}