Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Component;
- import javax.swing.JFrame;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- import javax.swing.table.AbstractTableModel;
- import javax.swing.table.DefaultTableCellRenderer;
- import javax.swing.table.TableCellRenderer;
- import javax.swing.table.TableModel;
- public class App {
- public static class BGCellRenderer extends DefaultTableCellRenderer {
- private Color bgcolor;
- public BGCellRenderer(Color bgcolor) {
- this.bgcolor = bgcolor;
- }
- @Override
- public Component getTableCellRendererComponent(JTable table,
- Object obj, boolean isSelected, boolean hasFocus, int row,
- int col) {
- Component comp = super.getTableCellRendererComponent(table, obj,
- isSelected, hasFocus, row, col);
- if (!isSelected) {
- if (obj instanceof EmptyCell) {
- comp.setBackground(bgcolor);
- } else {
- comp.setBackground(Color.WHITE);
- }
- }
- return comp;
- }
- }
- public static class Cell { }
- public static class EmptyCell extends Cell {
- }
- public static class RealCell extends Cell {
- }
- public static class MyModel extends AbstractTableModel {
- private Cell[][] data;
- private int width;
- private int height;
- public MyModel(int width, int height) {
- this.width = width;
- this.height = height;
- data = new Cell[width][height];
- for (int i = 0; i < width; i++) {
- for (int j = 0; j < height; j++) {
- if (i == j) {
- data[i][j] = new EmptyCell();
- } else {
- data[i][j] = new RealCell();
- }
- }
- }
- }
- @Override
- public int getColumnCount() {
- return height;
- }
- @Override
- public int getRowCount() {
- return width;
- }
- @Override
- public Object getValueAt(int i, int j) {
- return data[i][j];
- }
- }
- public static void main(String[] args) {
- TableModel model = new MyModel(5, 10);
- final BGCellRenderer bgrenderer = new BGCellRenderer(Color.GREEN);
- JTable table = new JTable(model) {
- public TableCellRenderer getCellRenderer(int row, int column) {
- return bgrenderer;
- }
- };
- JFrame f = new JFrame("rejtvény");
- f.getContentPane().add(new JScrollPane(table));
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.setSize(600, 400);
- f.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment