JTable单元格颜色 [英] JTable Cell Color
本文介绍了JTable单元格颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以给我一个例子,如何获得特定单元格的背景颜色在JTable?我无法找到一个如何做到这一点的例子。
Can someone give me an example of how to get the background color of a particular cell in a JTable? I am unable to find an example of how to do this. Plenty of examples on getting the value in a cell, but not the background color of a cell.
推荐答案
它应该是类似于一个单元格的值,而不是一个单元格的背景颜色。以下(根据所有注释确定):
It should be something like the following (fixed according to all comments):
重要:使用table.prepareRenderer(...)让JTable为您工作
public Color getTableCellBackground(JTable table, int row, int col) {
TableCellRenderer renderer = table.getCellRenderer(row, col);
Component component = table.prepareRenderer(renderer, row, col);
return component.getBackground();
}
完整演示:
public class TableRenderDemo extends JPanel {
public TableRenderDemo() {
super(new GridLayout(1, 0));
final JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(600, 200));
table.setFillsViewportHeight(true);
table.setDefaultRenderer(Object.class, new MyRenderer());
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
JOptionPane.showInternalMessageDialog(TableRenderDemo.this,
"Color: " + getTableCellBackground(table, row, col));
System.out.println("Color: " + getTableCellBackground(table, row, col));
}
});
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
public Color getTableCellBackground(JTable table, int row, int col) {
TableCellRenderer renderer = table.getCellRenderer(row, col);
Component component = table.prepareRenderer(renderer, row, col);
return component.getBackground();
}
class MyRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
JTextField editor = new JTextField();
if (value != null) {
editor.setText(value.toString());
}
editor.setBackground((row % 2 == 0) ? Color.white : Color.BLUE);
return editor;
}
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
private Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
public final Object[] longValues = {"Jane", "Kathy",
"None of the above",
new Integer(20), Boolean.TRUE};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("TableRenderDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableRenderDemo newContentPane = new TableRenderDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
这篇关于JTable单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文