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



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

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


mohammed afsar
how to mark different colors for diffent rows
1 Answer(s)      5 years and 4 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
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
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 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
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
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
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 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
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 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
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
 

 

 

DMCA.com