注意:每個項目包含兩條信息的項目列表更適合在表格中顯示而不是在列表中顯示,但如果需要的話,可以將其調整爲列表。適用相同的基本原則(使用渲染組件)。
因爲我感覺慈善,這是我的意思是:
這是通過這種渲染類實現:
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張專輯和超過一百萬種可能的顏色,我們應該很安全。
在剛剛超過100行代碼本(二級)MCVE:
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;
}
}
*「我看到很多人在談論建立自定義渲染類,但我的目標有些東西更簡單的」 *你錯誤地認爲不同的方法會更簡單。 –
我正在投票結束這個問題,因爲OP想要最簡單的方法,但已經拒絕了! –
(1-)'我看到很多人......' - 那麼爲什麼很多人會使用複雜的解決方案,如果有更簡單的解決方案存在?你不覺得很多人會使用更簡單和普遍接受的方法嗎? – camickr