how to mark different colors for diffent rows

how to mark different colors for diffent rows

View Answers

July 20, 2009 at 10:49 AM

Hi Friend,

Try the following code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;

public class TableRow extends JFrame {
public TableRow() {
JTable table=new JTable(new DefaultTableModel(new Object[][]{
{"A","Delhi","111111"},
{"B","Agra","222222"},
{"C","Mumbai","333333"},
{"D","Chennai","444444"},
{"E","Baroda","555555"},
{"F","Kolkata","666666"}},
new Object[]{"Name","Address","Phone No"}));
ChangeColor renderer=new ChangeColor("Name");


for (int i=0;i<table.getColumnCount();i++)
table.getColumn(table.getColumnName(i)).setCellRenderer(renderer);
JScrollPane scroll=new JScrollPane(table);
this.setContentPane(scroll);
this.setBounds(100,50,300,150);
}
public static void main (String arg[]) {
TableRow tes = new TableRow();
tes.setVisible(true);
tes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class ChangeColor extends JLabel implements TableCellRenderer {
private String columnName;
public ChangeColor(String column) {
this.columnName = column;
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,boolean selected, boolean hasFocus, int row, int column){
Object columnValue=table.getValueAt(row,table.getColumnModel().getColumnIndex(columnName));
if (value != null)
setText(value.toString());
setBackground(table.getBackground());
setForeground(table.getForeground());

if (columnValue.equals("A")) setBackground(Color.green);
if (columnValue.equals("B")) setBackground(Color.red);
if (columnValue.equals("C")) setBackground(Color.blue);
if (columnValue.equals("D")) setBackground(Color.pink);
if (columnValue.equals("E")) setBackground(Color.orange);
if (columnValue.equals("F")) setBackground(Color.yellow);
return this;
}
}

Thanks









Related Tutorials/Questions & Answers:
how to mark different colors for diffent rows - Java Beginners
how to mark different colors for diffent rows  Hi frends, Actually yesterday i had a question that, how can i mark colors to my rows in the table... question is how can i solve this problems means my table should have colored rows
how to mark different colors for diffent rows - Java Beginners
how to mark different colors for diffent rows  Hi, i have drawn a simple table using swings and now i want to mark diffent colors for each row..............my target is to have each color for each row.......first row
Advertisements
links with different colors.
links with different colors.  How can I have two sets of links with different colors
Write an applet program for displaying 3 different sentences with 3 different colors
Write an applet program for displaying 3 different sentences with 3 different colors  Write an applet program for displaying 3 different sentences with 3 different colors
how to print all colors using awt
how to print all colors using awt  how to print all colors using awt
updating rows which contains same id, different value for each row
updating rows which contains same id, different value for each row  ... 30 From the above table we can identify that 4 rows with sid 1 and 3 rows with sid 5. Like this table may contains multiple records with same sid
How to sort the rows in SQL?
How to sort the rows in SQL?  How to sort the rows in SQL?   Hi, Here is the answer,ADS_TO_REPLACE_1 The ORDER BY clause allows you to sort the records in your result set. The ORDER BY clause can only be used
How to insert rows in jTable?
How to insert rows in jTable?  Hi, I need to take input from user using JTable. I want an empty row to appear after clicking a insert button... not figure out how to. I used DefaultTableModel but wasnt able to insert a row
Here is a set of 18 different shades of one of the most favourite Choclate colors
; padding:5px 0px 5px 5px; } Here is a set of 18 different shades of one of the most favourite choclate colors rangeing from light choclate color to darker
Here is a set of 26 different shades of one of the most favourite Red colors
:5px 0px 5px 5px; } Here is a set of 26 different shades of one of the most favourite Red colors rangeing from light Red color to darker Red color
ShortBuffer in java, How to reset the mark of short buffer.
ShortBuffer in java, How to reset the mark of short buffer. In this tutorial, we will see how to reset the mark of short buffer. ShortBuffer API...() The reset() method  reset the mark at  previous position
This is a set of 18 different shades of DarkSalmon colors covering lighter DarkSalmon color to darker DarkSalmon color.
:5px 0px 5px 5px; } This is a set of 18 different shades of DarkSalmon colors covering lighter DarkSalmon color to darker DarkSalmon color
This is a set of 26 different shades of brown colors covering light brown to darker brown.
:5px 0px 5px 5px; } This is a set of 26 different shades of brown colors
HOW TO DISPLAY 2 ROWS OF CONTROLS
HOW TO DISPLAY 2 ROWS OF CONTROLS  i can be display one row by selecting value from drop down box BUT ALSO i need to display 2 rows by selecting value from drop down box of textbox's and radio button,drop down box using
specify colors
specify colors  How can I specify colors
How to show Rainbow Colors
How to show Rainbow Colors       This section illustrates you how to show all the seven colors of colors. The Rainbow consists of seven colors known as VIBGYOR i.e. violet
Updating rows who has same id with different values for each row from java program
Updating rows who has same id with different values for each row from java... rows with same id. student table: sid sname age 2 aruna 25 2... 30 Folllowing is my requirement: If multiple rows have same sid then only we
how to search the string arraylist contains database rows?
how to search the string arraylist contains database rows?  i need... the row values into string arraylist. now i want to print the common rows existed. how do i do
Here is a set of 26 different shades of one of the most favourite Orange colors rangeing from light Orange color to darker Orange color.
:5px 0px 5px 5px; } Here is a set of 26 different shades of one of the most favourite Orange colors rangeing from light Orange color to darker Orange color
Here is a set of 26 different shades of one of the most favourite Pink colors rangeing from light Pink color to darker Pink color.
:5px 0px 5px 5px; } Here is a set of 26 different shades of one of the most favourite Pink colors rangeing from light Pink color to darker Pink color
ModuleNotFoundError: No module named 'mark'
ModuleNotFoundError: No module named 'mark'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'mark' How to remove the ModuleNotFoundError: No module named 'mark' error
Reset( )and Mark( )
Reset( )and Mark( )  i am using buffer reader to read each line using... using mark() i have to specify the size. For a huge file, length has to be stored in long but mark only takes int type value.. any other function that i can
ModuleNotFoundError: No module named 'Colors'
'Colors' How to remove the ModuleNotFoundError: No module named 'Colors'...ModuleNotFoundError: No module named 'Colors'  Hi, My Python... to install padas library. You can install Colors python with following command
ModuleNotFoundError: No module named 'Colors'
'Colors' How to remove the ModuleNotFoundError: No module named 'Colors'...ModuleNotFoundError: No module named 'Colors'  Hi, My Python... to install padas library. You can install Colors python with following command
How can we find the number of rows in a result set using PHP?
How can we find the number of rows in a result set using PHP?   How can we find the number of rows in a result set using PHP
how to select random rows from database through servlet
how to select random rows from database through servlet  hello i want to know, how to select random rows from database through servlet
How to make a button of different behaviour?
How to make a button of different behaviour?  Suppose In a jsp page I have a button SAVE with id="btnSave" value="SAVE" and a javascript function... ajaxInsertUpdateData(document.getElementByID("btnSave1").value,......); How to do
How to print different Messge for the same NumberFormatException on different cause objects in JAVA?
How to print different Messge for the same NumberFormatException on different cause objects in JAVA?  How to print different Messge for the same NumberFormatException on different cause objects in JAVA? try { int
ModuleNotFoundError: No module named 'flake8-pytest-mark'
named 'flake8-pytest-mark' How to remove the ModuleNotFoundError: No module named 'flake8-pytest-mark' error? Thanks   Hi, In your...ModuleNotFoundError: No module named 'flake8-pytest-mark'  Hi, My
ModuleNotFoundError: No module named 'mark-search'
'mark-search' How to remove the ModuleNotFoundError: No module named 'mark-search' error? Thanks   Hi, In your python environment...ModuleNotFoundError: No module named 'mark-search'  Hi, My Python
ModuleNotFoundError: No module named 'mark_utils'
'mark_utils' How to remove the ModuleNotFoundError: No module named 'mark...ModuleNotFoundError: No module named 'mark_utils'  Hi, My Python... have to install padas library. You can install mark_utils python
ModuleNotFoundError: No module named 'mdn-mark'
-mark' How to remove the ModuleNotFoundError: No module named 'mdn-mark...ModuleNotFoundError: No module named 'mdn-mark'  Hi, My Python... to install padas library. You can install mdn-mark python with following
ModuleNotFoundError: No module named 'nester_mark'
'nester_mark' How to remove the ModuleNotFoundError: No module named 'nester_mark' error? Thanks   Hi, In your python environment...ModuleNotFoundError: No module named 'nester_mark'  Hi, My Python
ModuleNotFoundError: No module named 'pyspider-mark'
'pyspider-mark' How to remove the ModuleNotFoundError: No module named 'pyspider-mark' error? Thanks   Hi, In your python...ModuleNotFoundError: No module named 'pyspider-mark'  Hi, My
ModuleNotFoundError: No module named 'flake8-pytest-mark'
named 'flake8-pytest-mark' How to remove the ModuleNotFoundError: No module named 'flake8-pytest-mark' error? Thanks   Hi, In your...ModuleNotFoundError: No module named 'flake8-pytest-mark'  Hi, My
How to insert rows from Excel spreadsheet into database by browsing the excel file?
How to insert rows from Excel spreadsheet into database by browsing the excel file?  I want to insert rows from excel sheet to database.for this i... the excel file using file browsing dialogue through form in JSP. How can i select
how to Insert Array Values Into Separate Rows using java?
how to Insert Array Values Into Separate Rows using java?  how to Insert Array Values Into Separate Rows using java?  class InsertArrayValuesInSeparateRow{ public static void main(String[] args
different borders
; enter the values in that cell then we create different borders and we also sets the different colors. . Code description  The package we need to import... different borders      
How create an overlay button on different containing div
How create an overlay button on different containing div  How to create an overlay button on different containing div
ModuleNotFoundError: No module named 'github-colors'
'github-colors' How to remove the ModuleNotFoundError: No module named 'github-colors' error? Thanks   Hi, In your python...ModuleNotFoundError: No module named 'github-colors'  Hi, My
ModuleNotFoundError: No module named 'mac-colors'
'mac-colors' How to remove the ModuleNotFoundError: No module named 'mac-colors' error? Thanks   Hi, In your python environment you...ModuleNotFoundError: No module named 'mac-colors'  Hi, My Python
ModuleNotFoundError: No module named 'make-colors'
'make-colors' How to remove the ModuleNotFoundError: No module named 'make-colors' error? Thanks   Hi, In your python environment...ModuleNotFoundError: No module named 'make-colors'  Hi, My Python
ModuleNotFoundError: No module named 'mappyfile-colors'
named 'mappyfile-colors' How to remove the ModuleNotFoundError: No module named 'mappyfile-colors' error? Thanks   Hi, In your...ModuleNotFoundError: No module named 'mappyfile-colors'  Hi, My
ModuleNotFoundError: No module named 'mintty-colors'
'mintty-colors' How to remove the ModuleNotFoundError: No module named 'mintty-colors' error? Thanks   Hi, In your python...ModuleNotFoundError: No module named 'mintty-colors'  Hi, My
ModuleNotFoundError: No module named 'mintty-colors'
'mintty-colors' How to remove the ModuleNotFoundError: No module named 'mintty-colors' error? Thanks   Hi, In your python...ModuleNotFoundError: No module named 'mintty-colors'  Hi, My
ModuleNotFoundError: No module named 'mpl-colors'
'mpl-colors' How to remove the ModuleNotFoundError: No module named 'mpl-colors' error? Thanks   Hi, In your python environment you...ModuleNotFoundError: No module named 'mpl-colors'  Hi, My Python
ModuleNotFoundError: No module named 'object-colors'
'object-colors' How to remove the ModuleNotFoundError: No module named 'object-colors' error? Thanks   Hi, In your python...ModuleNotFoundError: No module named 'object-colors'  Hi, My
ModuleNotFoundError: No module named 'python-colors'
'python-colors' How to remove the ModuleNotFoundError: No module named 'python-colors' error? Thanks   Hi, In your python...ModuleNotFoundError: No module named 'python-colors'  Hi, My
ModuleNotFoundError: No module named 'python-colors'
'python-colors' How to remove the ModuleNotFoundError: No module named 'python-colors' error? Thanks   Hi, In your python...ModuleNotFoundError: No module named 'python-colors'  Hi, My
ModuleNotFoundError: No module named 'simple-colors'
'simple-colors' How to remove the ModuleNotFoundError: No module named 'simple-colors' error? Thanks   Hi, In your python...ModuleNotFoundError: No module named 'simple-colors'  Hi, My

Ads