Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

Have Programming Question? Ask it here!
 
 
 


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



Advertisement
View Answers

July 21, 2009 at 10:11 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 {
JButton button1=new JButton("Add");
JButton button2=new JButton("Delete");
JPanel panel=new JPanel();
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);
panel.add(button1);
panel.add(button2);
panel.add(scroll);
this.setContentPane(panel);
this.setBounds(100,50,400,180);
}
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, 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
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
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.   
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
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
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
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 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
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
specify colors
specify colors  How can I specify colors
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
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
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
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
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
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
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 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 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 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 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
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
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........   Hi, There are different types Thread in Java program. Here is the explain how to using thread
c++ array find the average mark, highest mark, lowest mark, number of students passing,
c++ array find the average mark, highest mark, lowest mark, number of students passing,    i need help in adding the c++ code for the average mark, highest mark, lowest mark, number of students passing, using array. 01
c++ array find the average mark, highest mark, lowest mark, number of students passing,
c++ array find the average mark, highest mark, lowest mark, number of students passing,    i need help in adding the c++ code for the average mark, highest mark, lowest mark, number of students passing, using array. 01
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 to communicate with different web applications - JSP-Servlet
how to communicate with different web applications  how to communicate with different web applications which are under same or different servers? ex... to second/jsp/show.jsp without using query string like that can u guide me how
* How to communicate with different web applications ? ? ? ? - JSP-Servlet
* How to communicate with different web applications ? ? ? ?  Deepak ji thanks for fast reply still i have some doubt about this How to communicate with different web applications which are under same or different servers? ex
*** How to communicate with different web applications ? ? ? ? - JSP-Servlet
*** How to communicate with different web applications ? ? ? ?  Deepak ji thanks for fast reply still i have some doubt about this How to communicate with different web applications which are under same or different servers? ex
How to communicate with different web applications ? ? ? ? - JSP-Servlet
How to communicate with different web applications ? ? ? ?  Deepak ji thanks for fast reply still i have some doubt about this How to communicate with different web applications which are under same or different servers? ex
how to add the calendar to the dynamic rows in html or jsp page - JSP-Servlet
how to add the calendar to the dynamic rows in html or jsp page   Hi Sir, i have 3 columns in my jsp page date of payment,amount recieved,no and i have 2 button in my jsp page ADD and delete button. when i click on add
How to communicate with different web applications ? ? ? ? - JSP-Servlet
How to communicate with different web applications ? ? ? ?  How to communicate with different web applications which are under same or different... to second/jsp/show.jsp without using query string like that can u guide me how
How to add dynamically rows into database ?Need help pls
How to add dynamically rows into database ?Need help pls  Hi everyone, I really have a problem of insert multiple rows into the database.Now i can only insert one row into the database.How can insert the rest of the row
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);   
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 
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 read a rows which have a values in a excel file using apache poi - JSP-Servlet
How to read a rows which have a values in a excel file using apache poi  Dear sir, How to read excel file in that only a rows which are having some values using apache poi...please help me sir. Thanks and Regards
How to include Two multi rows, one with a file Upload and another with normal fields, have problem while including both
How to include Two multi rows, one with a file Upload and another with normal fields, have problem while including both  How to include Two multi rows, one with a file Upload and another with normal fields, have problem while
How to import data from sql server table into an excel file by creating the rows dynamically in the excel according to the dataabase??
How to import data from sql server table into an excel file by creating the rows dynamically in the excel according to the dataabase??  How to import... 6 columns but the rows keep on increasing in the table time to time. So how
How to read text file to two different name array
How to read text file to two different name array   I have those numbers:12,4,9,5 numbers:19,12,1,1 how to put it in two different name array in text file to java
How to read text file to two different name array
How to read text file to two different name array   I have those numbers:12,4,9,5 numbers:19,12,1,1 how to put it in two different name array in text file to java
 

 

 

DMCA.com