change color of particular row in jtable
Java
import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class Main extends JFrame {
public Main() {
super("TableModel Demonstration");
// create our own custom TableModel
WineTableModel wineModel = new WineTableModel();
JTable table = new JTable(wineModel);
// since we’re using values of floats and boolean here, we need
// to set the cell renderer for every column.
for (int i =0; i<wineModel.getColumnCount();i++) {
table.setDefaultRenderer(table.getColumnClass(i), new WineCellRenderer());
}
// add rows to our TableModel, each row is represented as a Wine object
wineModel.addWine(new Wine("Chateau Meyney, St. Estephe", "1994", 18.75f, true));
wineModel.addWine(new Wine("Chateau Montrose, St. Estephe", "1975", 54.25f, true));
wineModel.addWine(new Wine("Chateau Gloria, St. Julien", "1993", 22.99f, false));
wineModel.addWine(new Wine("Chateau Beychevelle, St. Julien", "1970", 61.63f, false));
wineModel.addWine(new Wine("Chateau La Tour de Mons, Margeaux", "1975", 57.03f, true));
wineModel.addWine(new Wine("Chateau Brane-Cantenac, Margeaux", "1978", 49.92f, false));
// create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
// add the scroll pane to this window.
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
Main main = new Main();
main.pack();
main.setVisible(true);
}
}
// a simple object that holds data about a particular wine
class Wine {
private String name;
private String vintage;
private float price;
private boolean inStock;
public Wine(String name, String vintage, float price, boolean inStock) {
this.name = name;
this.vintage = vintage;
this.price = price;
this.inStock = inStock;
}
public String getName() { return name; }
public String getVintage() { return vintage; }
public float getPrice() { return price; }
public boolean getInStock() { return inStock; }
public String toString() {
return "[" + name + ", " + vintage + ", " + price + ", " + inStock + "]"; }
}
class WineTableModel extends AbstractTableModel {
// holds the strings to be displayed in the column headers of our table
final String[] columnNames = {"Name", "Vintage", "Price", "In stock?"};
// holds the data types for all our columns
final Class[] columnClasses = {String.class, String.class, Float.class, Boolean.class};
// holds our data
final Vector data = new Vector();
// adds a row
public void addWine(Wine w) {
data.addElement(w);
fireTableRowsInserted(data.size()-1, data.size()-1);
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Class getColumnClass(int c) {
return columnClasses1;
}
public Object getValueAt(int row, int col) {
Wine wine = (Wine) data.elementAt(row);
if (col == 0) return wine.getName();
else if (col == 1) return wine.getVintage();
else if (col == 2) return new Float(wine.getPrice());
else if (col == 3) return new Boolean(wine.getInStock());
else return null;
}
public Object getValueAtRow(int row) {
Wine wine = (Wine) data.elementAt(row);
return wine;
}
public boolean isCellEditable(int row, int col) {
return false;
}
}
class WineCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
WineTableModel wtm = (WineTableModel) table.getModel();
Wine wine = (Wine) wtm.getValueAtRow(row);
if (wine.getPrice() < 55) {
setBackground(Color.green);
}
else {
setBackground(Color.red);
}
return super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
}
}
Also in Java:
- Title
- java clear console
- Category
- Java
- Title
- java max
- Category
- Java
- Title
- formatting an integer in java
- Category
- Java
- Title
- fastapi list query parameter
- Category
- Java
- Title
- Java how to copy file
- Category
- Java
- Title
- how to add an item to a list in python
- Category
- Java
- Title
- java list contains object with property
- Category
- Java
- Title
- change fab image programatically
- Category
- Java
- Title
- java 8 hashmap example stackoverflow
- Category
- Java
- Title
- get sha key android
- Category
- Java
- Title
- init admob
- Category
- Java
- Title
- exponents java
- Category
- Java
- Title
- catch array out of bounds exception java
- Category
- Java
- Title
- inline foreach java
- Category
- Java
- Title
- biginteger in java
- Category
- Java
- Title
- not equal to java
- Category
- Java
- Title
- if statement java
- Category
- Java
- Title
- how to create a method java
- Category
- Java
- Title
- how to fill a list with a single value java
- Category
- Java
- Title
- java remove non numbers from string
- Category
- Java
- Title
- double round java integer
- Category
- Java
- Title
- find the triplet sum in java linked list
- Category
- Java
- Title
- create java windows application
- Category
- Java
- Title
- do statement java
- Category
- Java
- Title
- Java app development
- Category
- Java
- Title
- run java classpath command line
- Category
- Java
- Title
- python to java converter
- Category
- Java
- Title
- onbackpressed android
- Category
- Java
- Title
- recursion in java
- Category
- Java
- Title
- java random char a-z
- Category
- Java
- Title
- java hashmap syntax
- Category
- Java
- Title
- programmation android avoir acces à la liste des intents de partage
- Category
- Java
- Title
- bracket balancing program in java
- Category
- Java
- Title
- how take inputs in java with message
- Category
- Java
- Title
- Spring Security JWT Authentication + PostgreSQL – RestAPIs SpringBoot + Spring MVC + Spring JPA
- Category
- Java
- Title
- how to make int array java android
- Category
- Java
- Title
- convert every character in string to arraylist jva
- Category
- Java
- Title
- object orientation in java
- Category
- Java
- Title
- java initialize float to zero
- Category
- Java
- Title
- check if a string is a substring of another string java
- Category
- Java
- Title
- java check if string is number
- Category
- Java
- Title
- random boolean java
- Category
- Java
- Title
- how to get length of integer in java
- Category
- Java
- Title
- get day name from date in java
- Category
- Java
- Title
- arraylist array
- Category
- Java
- Title
- change brightness of image in java
- Category
- Java
- Title
- Write a JAVA method that expands a given binomial (ax + by)n, where integers a, b, n are user inputs. For example, if a = 2, b = -12, n = 4 are entered the method should print or return
- Category
- Java
- Title
- how to pass parameters to xsl file
- Category
- Java
- Title
- java loop through string
- Category
- Java
- Title
- how to count an replace substring string in java
- Category
- Java
- Title
- java concatenate arrays
- Category
- Java
- Title
- how to exit a for loop in java
- Category
- Java
- Title
- iterate through hashtable java
- Category
- Java
- Title
- output statement java
- Category
- Java
- Title
- how to create an action listener in java
- Category
- Java
- Title
- inputstream to string java
- Category
- Java
- Title
- raise number to power java
- Category
- Java
- Title
- covariant type in java
- Category
- Java
- Title
- use regex in if statement java
- Category
- Java
- Title
- java map print
- Category
- Java
- Title
- how to call a static method in java
- Category
- Java
- Title
- java d'intervalle de resultat
- Category
- Java
- Title
- array of objects in java
- Category
- Java
- Title
- how to make an arraylist java
- Category
- Java
- Title
- hashmaps java
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- iterator for array java
- Category
- Java
- Title
- java get an int from a char
- Category
- Java
- Title
- removeeventlistener
- Category
- Java
- Title
- array null pointer java
- Category
- Java
- Title
- how to convert a jsonobject to a dbobject
- Category
- Java
- Title
- java literals w3schools
- Category
- Java
- Title
- java download file from url to string
- Category
- Java
- Title
- recursive function for fibonacci series in java javascript
- Category
- Java
- Title
- math max java
- Category
- Java
- Title
- how to break from a loop in java
- Category
- Java
- Title
- How to create a 2d array in java
- Category
- Java
- Title
- como saber de que tipo es una variable en java
- Category
- Java
- Title
- how to iterate hashmap in java
- Category
- Java
- Title
- spigot cancel repeating task
- Category
- Java
- Title
- java how to program
- Category
- Java
- Title
- error message pushes button down
- Category
- Java
- Title
- for loop in java stack overflow
- Category
- Java
- Title
- java array out of bounds
- Category
- Java
- Title
- java get input
- Category
- Java
- Title
- Primitive Type vs. Reference Type
- Category
- Java
- Title
- numberformatexception
- Category
- Java
- Title
- java 8 string to localdate
- Category
- Java
- Title
- remove duplicates from singly linked list
- Category
- Java
- Title
- how to print in java
- Category
- Java
- Title
- sort elements with sortedset
- Category
- Java
- Title
- how to show the hex detail of a file in java
- Category
- Java
- Title
- indexof java
- Category
- Java
- Title
- getindexrequest elasticsearch
- Category
- Java
- Title
- import java.util.hashset
- Category
- Java
- Title
- java pass array as method parameter
- Category
- Java
- Title
- How do you count characters in a string array in Java?
- Category
- Java
- Title
- java observable to observer
- Category
- Java
- Title
- processing audio android
- Category
- Java
- Title
- read a mail and its content in java mail api
- Category
- Java