Change Table cell background with column renderer
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
public class ColorTableColumnRenderer extends JFrame{
protected JTable table;
protected MyTableModel tableModel;
public ColorTableColumnRenderer() {
super("Stocks Table");
setSize(300, 300);
tableModel = new MyTableModel();
table = new JTable();
table.setModel(tableModel);
DefaultTableCellRenderer renderer = new ColoredTableCellRenderer();
TableColumn column = new TableColumn(0,190, renderer, null);
table.addColumn(column);
JScrollPane ps = new JScrollPane();
ps.getViewport().add(table);
getContentPane().add(ps, BorderLayout.CENTER);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
setVisible(true);
}
public static void main(String argv[]) {
new ColorTableColumnRenderer();
}
}
class MyTableModel extends AbstractTableModel {
protected Vector vector;
protected int columnsCount = 1;
public MyTableModel() {
vector = new Vector();
vector.addElement("2146400");
vector.addElement("554000");
vector.addElement("24230");
}
public int getRowCount() {
return vector == null ? 0 : vector.size();
}
public int getColumnCount() {
return columnsCount;
}
public String getColumnName(int column) {
return "data";
}
public boolean isCellEditable(int nRow, int nCol) {
return false;
}
public Object getValueAt(int nRow, int nCol) {
if (nRow < 0 || nRow >= getRowCount())
return "";
return vector.elementAt(nRow);
}
public String getTitle() {
return "data ";
}
}
class ColoredTableCellRenderer extends DefaultTableCellRenderer {
public void setValue(Object value) {
setForeground(Color.red);
super.setValue(value);
}
}
Related examples in the same category