Home Answers Viewqa Java-Beginners how to mark different colors for diffent rows

 
 


mohammed afsar
how to mark different colors for diffent rows
1 Answer(s)      3 years and 10 months ago
Posted in : Java Beginners

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 Pages:
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
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
links with different colors.
links with different colors.  How can I have two sets of links with different colors
specify colors
specify colors  How can I specify 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
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...() method  reset the mark at  previous position.   
display of colors in a table - JSP-Servlet
display of colors in a table  Hi, If i have a table of 4 by 4 boxes, numbering from 1-16 in sequence, how do i make them display one column of one color and another column of another color? Thanks!  Hi Friend
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
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, The ORDER BY clause allows you to sort the records in your result set. The ORDER BY clause can only be used in SELECT statements
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
JSlider with RGB Colors
JSlider with RGB Colors In this section, you will learn how to create sliders to show RGB colors. In this example, we  have created three horizontal sliders, first one as red, the second one as green and the third one as blue
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
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
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
Comparison of corresponding rows in a DB
Comparison of corresponding rows in a DB  How to compare corressponding rows in a database using Java????? ex...!!! Thankin u
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
Count Rows - JSP-Servlet
Count Rows  How to count rows in Java. Thanks
Grid rows delete issue
Grid rows delete issue  I have a grid having rows with Remove button to remove rows,But we also havae an facility to remove those rows on onchange event of dropdown also,so if we delete middle row and then remove all rows
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
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      
Php Sql num rows
Php Sql num rows This example illustrates how to find number of rows... and find the number of rows of the table by mysql_num_rows methods. The output...);   $num_rows = mysql_num_rows($result);   
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
MySQL Affected Rows
MySQL Affected Rows This example illustrates how to show the affected rows... rows affected (0.08 sec)   Here update a table "...; Query OK, 0 rows affected (0.00 sec) Rows matched: 1 Changed: 0
Php Sql Number of Rows
Php Sql Number of Rows  This example illustrates how to count the rows... of rows of the table. To find the number of the rows we use the mysql_num_rows..._rows($res);     print("$number_of_rows rows 
Displaying Rows - JSP-Servlet
Displaying Rows  Hi, I need your support on how to display data from ms sql 2000 database into an html form text box and text area, using java servlet or jsp  Hi friend, This is form code, display data
JavaScript Hide Table Rows
; In this section, you will learn how to hide table rows using JavaScript... JavaScript Hide Table Rows... object grabs the id of the table. Then we have determined the length of rows
return all rows mysql php
return all rows mysql php  How to find or return results that are not available in another table .. using join method in PHP and MYSql.   SELECT * FROM Declined LEFT JOIN Request ON Declined.RequestID
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
html page viewed on different os
html page viewed on different os  The colors on my page look different when viewed on a Mac and a PC
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
JDBC: Rows Count Example
JDBC: Rows Count Example In this section, You will learn how to count number of rows in a table using JDBC API. Counting number of rows - To count the number of rows of a table we use COUNT(*). It will returns the number of rows
PHP SQL Number of Rows
PHP SQL Number of Rows       This example illustrates how to count the rows of the table. To understand how to count the number of rows in a table, we have created a sql_num_rows.php
Getting the Number of Rows and Columns in JTable
will learn how to get the number of rows and columns in JTable.  This program simply helps us for getting the number of rows and columns from the JTable by using... Getting the Number of Rows and Columns in JTable 
Java get number of rows in resultset
Java get number of rows in resultset       In this section, you will learn how to retrieve the number of rows from the database table. As you know that the data
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
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
Adding checkbox in table using that delete table rows
for how to add checkbox in table,the table data from database.Using that checkbox select more than than one rows,and using delete button i want to delete the selected rows using swing in java
Php Sql num rows
Php Sql num rows       This tutorial illustrates how to find number of rows of the table... to find the number of rows in the table. We created a connection from database
Inserting Rows in a JTable
in a table. This tutorial helps you in how to insert rows in a JTable to specified... Inserting Rows in a JTable     ... 3 rows and 2 columns. If  you want to insert a data at any location
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
How to Explain different way of using thread?
How to Explain different way of using thread?  Hi, How to explain how to using different thread in Java program
insert rows from browsed file to sql database
insert rows from browsed file to sql database  i need to insert rows from excel to database by browsing the file in jsp. by connecting both..., content of the file has to go to database. how can i insert record into database

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.