How do I set the 开发者_StackOverflowJTable
column and row color?
Here is a short example.
import java.awt.Color;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
public class JTableExample extends JFrame {
String[][] data = {{"John", "Sutherland", "Student"},
{"George", "Davies", "Student"},
{"Melissa", "Anderson", "Associate"},
{"Stergios", "Maglaras", "Developer"},
};
String[] fields = {"Name", "Surname", "Status"};
public static void main(String[] args) {
new JTableExample();
}
public JTableExample() {
super();
setSize(150, 150);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
System.exit(0);
}
});
init();
pack();
setVisible(true);
}
private void init() {
final CustomCellRenderer renderer = new CustomCellRenderer();
JTable jt = new JTable(data, fields) {
@Override
public TableCellRenderer getCellRenderer(int row, int column) {
return renderer;
}
};
JScrollPane pane = new JScrollPane(jt);
getContentPane().add(pane);
}
/**
* @author suhas, orwellophile
*
*/
private class CustomCellRenderer extends DefaultTableCellRenderer {
/**
* @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(JTable, Object, boolean, boolean, int, int)
*/
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Component rendererComp = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
// Very important to handle selected items (render them inversely colored)
if (isSelected) {
rendererComp.setBackground(getHuedColor(row).darker().darker());
rendererComp.setForeground(getHuedColor(row).brighter().brighter());
}
else {
rendererComp.setBackground(getHuedColor(row).brighter().brighter());
rendererComp.setForeground(getHuedColor(row).darker().darker());
}
return rendererComp;
}
public Color getHuedColor(int hue) {
return new Color(
Color.HSBtoRGB(85 / 360f * hue, 0.7f, 1.0f
));
}
}
}
Answering a vague question from an absent-minded asker takes some good will (and idle time).
For column, you can set a cell renderer that extends DefaultTableCellRenderer
(which extends JLabel) on a column, then set renderer's foreground and background.
For row you have to do it in cell renderer's getTableCellRendererComponent
.
If you need anything beyond trivial (e.g. alternate row background) you should use a flexible custom renderer. There are countless free and commercial table components that provide coloring and everything JTable should have, for example JIDE Grids (not free but super powerful).
DefaultTableCellRenderer rendar1 = new DefaultTableCellRenderer();
rendar1.setForeground(Color.red);
jTable1.getColumnModel().getColumn(0).setCellRenderer(rendar1);
colum change color this code to constructor
精彩评论