change color of particular row in jtable

Solutions on MaxInterview for change color of particular row in jtable by the best coders in the world

showing results for - "change color of particular row in jtable"
Rodrigo
25 Jan 2017
1import javax.swing.table.*;
2import java.awt.event.*;
3import javax.swing.*;
4import java.util.*;
5import java.awt.*;
6  
7public class Main extends JFrame {
8   public Main() {
9      super("TableModel Demonstration");
10   
11      // create our own custom TableModel
12      WineTableModel wineModel = new WineTableModel();
13      JTable table = new JTable(wineModel);
14  
15      // since we’re using values of floats and boolean here, we need
16      // to set the cell renderer for every column. 
17      for (int i =0; i<wineModel.getColumnCount();i++) {
18         table.setDefaultRenderer(table.getColumnClass(i), new WineCellRenderer());
19      }
20        
21      // add rows to our TableModel, each row is represented as a Wine object
22      wineModel.addWine(new Wine("Chateau Meyney, St. Estephe", "1994", 18.75f, true));
23      wineModel.addWine(new Wine("Chateau Montrose, St. Estephe", "1975", 54.25f, true));
24      wineModel.addWine(new Wine("Chateau Gloria, St. Julien", "1993", 22.99f, false));
25      wineModel.addWine(new Wine("Chateau Beychevelle, St. Julien", "1970", 61.63f, false));
26      wineModel.addWine(new Wine("Chateau La Tour de Mons, Margeaux", "1975", 57.03f, true));
27      wineModel.addWine(new Wine("Chateau Brane-Cantenac, Margeaux", "1978", 49.92f, false));
28  
29      // create the scroll pane and add the table to it. 
30      JScrollPane scrollPane = new JScrollPane(table);
31  
32      // add the scroll pane to this window.
33      getContentPane().add(scrollPane, BorderLayout.CENTER);
34  
35      addWindowListener(new WindowAdapter() {
36         public void windowClosing(WindowEvent e) {
37            System.exit(0);
38         }
39      });
40   }
41  
42   public static void main(String[] args) {
43      Main main = new Main();
44      main.pack();
45      main.setVisible(true);
46   }
47}
48  
49// a simple object that holds data about a particular wine
50class Wine {
51   private String  name;
52   private String  vintage;
53   private float   price;
54   private boolean inStock;
55  
56   public Wine(String name, String vintage, float price, boolean inStock) {
57      this.name = name;
58      this.vintage = vintage;
59      this.price = price;
60      this.inStock = inStock;
61   }
62  
63   public String getName()     { return name; }
64   public String getVintage()  { return vintage; }
65   public float  getPrice()    { return price; } 
66   public boolean getInStock() { return inStock; }
67  
68   public String toString() { 
69      return "[" + name + ", " + vintage + ", " + price + ", " + inStock + "]"; }
70}
71  
72class WineTableModel extends AbstractTableModel {
73   // holds the strings to be displayed in the column headers of our table
74   final String[] columnNames = {"Name", "Vintage", "Price", "In stock?"};
75  
76   // holds the data types for all our columns
77   final Class[] columnClasses = {String.class, String.class, Float.class, Boolean.class};
78  
79   // holds our data
80   final Vector data = new Vector();
81   
82   // adds a row
83   public void addWine(Wine w) {
84      data.addElement(w);
85      fireTableRowsInserted(data.size()-1, data.size()-1);
86   }
87  
88   public int getColumnCount() {
89      return columnNames.length;
90   }
91          
92   public int getRowCount() {
93      return data.size();
94   }
95  
96   public String getColumnName(int col) {
97      return columnNames[col];
98   }
99  
100   public Class getColumnClass(int c) {
101      return columnClasses1;
102   }
103  
104   public Object getValueAt(int row, int col) {
105      Wine wine = (Wine) data.elementAt(row);
106      if (col == 0)      return wine.getName();
107      else if (col == 1) return wine.getVintage();
108      else if (col == 2) return new Float(wine.getPrice());
109      else if (col == 3) return new Boolean(wine.getInStock());
110      else return null;
111   }
112  
113   public Object getValueAtRow(int row) {
114      Wine wine = (Wine) data.elementAt(row);
115      return wine;
116   }
117  
118   public boolean isCellEditable(int row, int col) {
119      return false;
120   }
121}
122  
123class WineCellRenderer extends DefaultTableCellRenderer {
124   public Component getTableCellRendererComponent(
125            JTable table, Object value, boolean isSelected,
126            boolean hasFocus, int row, int column)
127   {
128      WineTableModel wtm = (WineTableModel) table.getModel();
129      Wine wine = (Wine) wtm.getValueAtRow(row);
130  
131      if (wine.getPrice() < 55) {
132         setBackground(Color.green);
133      }
134      else {
135         setBackground(Color.red);
136      }
137  
138      return super.getTableCellRendererComponent(table, value, isSelected, 
139                                                 hasFocus, row, column);
140   }
141}
142