how to mark different colors for diffent rows

Ads

 
 

Share on Google+Share on Google+

mohammed afsar
how to mark different colors for diffent rows
1 Answer(s)      7 years ago
Posted in : Java Beginners
Ads
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

Ads









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
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 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 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 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
Tutorials   
Java Spring Hibernate Struts Training What type of programming language is Java? How do beginners learn about Java? ClassNotFoundException HttpRequestInterceptor java.lang.noclassdeffounderror: org/apache/http/httprequest noclassdeffounderror: org/apache/http/client/methods/httpurirequest java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient How do I resolve this Java Class not found exception? httpclient java.lang.NoClassDefFoundError Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017

Ads

 
Advertisement null

Ads