In this post, SKOTechLearn will explain how to Change Color of jTable in Java like Column Header Color, grid Color, Rows Color, Selection Color. When I was started programming in java and using jTable, I faced problem of jTable background color foreground color of particular cells or rows and columns.
So here are the simple and easy tips to Change jTable Column Header and Rows Color in Java Netbeans Easily.
Easily Add Column Header in jTable with Grid Settings in Java
So here SKOTechLearn will described, how to Change jTable Column Header and Rows Color in Java NetBeans in simple way?
So, Let’s take a look of Following Color changing point.
First you have to import Color Class:
Now, Let's Proceed with above given point.
Column Header background with default system Color:
Column Header background with Custom Color :
So here are the simple and easy tips to Change jTable Column Header and Rows Color in Java Netbeans Easily.
Easily Add Column Header in jTable with Grid Settings in Java
So here SKOTechLearn will described, how to Change jTable Column Header and Rows Color in Java NetBeans in simple way?
So, Let’s take a look of Following Color changing point.
- jTable Column Header Background Color and Foreground or Font Color
- jTable Column Header Background Color and Foreground Color in NetBeans
- jTable Rows Background and Foreground Color
- Change Background and Foreground Color of Particular Column's Rows in jTable
- jTable Selection background and Foreground Color
- jTable Selection Background and Foreground Setting in NetBeans
First you have to import Color Class:
import java.awt.Color;
Now, Let's Proceed with above given point.
(1). jTable Column Header Background Color and Foreground or Font Color :
There are many ways to change Header’s back color. You can change it through predefined system color or you can use custom color.Column Header background with default system Color:
MyTblView.getTableHeader().setBackground(Color.MAGENTA);
Column Header background with Custom Color :
MyTblView.getTableHeader().setBackground(Color.decode("#696B9E"));
Or you can use JTableHeader class.
Enable or Show jTable Sorting Option in Java
First import this class:
import javax.swing.table.JTableHeader;
Then use this class with following way:
JTableHeader TblHeader = MyTblView.getTableHeader();
TblHeader.setBackground(Color.GREEN);
Same above code process will apply here; we can set Foreground color with system default and custom color setting.Column Header Foreground with default system Color :
MyTblView.getTableHeader().setForeground(Color.BLUE);
Column Header Foreground with Custom Color :
MyTblView.getTableHeader().setForeground(Color.decode("#FCE7FC"));
JTableHeader class for Foreground color code:
JTableHeader TblHeader = MyTbleView.getTableHeader(); TblHeader.setForeground(Color.RED);
When you run any code the output will show in following way:
(2). jTable Column Header Background Color and Foreground Color in NetBeans :
If you are using NetBeans for swing GUI project using jTable, the column header color will apply in different coding way. You have to use DefaultTableCellRenderer class in NetBeans Code.DefaultTableCellRenderer MyHeaderRender = new DefaultTableCellRenderer(); MyHeaderRender.setBackground(Color.decode("#696B9E")); MyHeaderRender.setForeground(Color.decode("#FCE7FC")); MyTblView.getTableHeader().getColumnModel().getColumn(0).setHeaderRenderer(MyHeaderRender); MyTblView.getTableHeader().getColumnModel().getColumn(1).setHeaderRenderer(MyHeaderRender); MyTblView.getTableHeader().getColumnModel().getColumn(2).setHeaderRenderer(MyHeaderRender);
After that this will show given output:
(3). jTable Rows Background and Foreground Color :
There is simple code for doing this. But remember one thing; it can change color of all rows. The simple and easy codes are as follows: MyTblView.setBackground(Color.LIGHT_GRAY);
MyTblView.setForeground(Color.BLUE);
This code will not be use for particular rows settings.This will show the following output.
(4). Change Background and Foreground Color of Particular Column's Rows in jTable :
Now, this is the very interesting point, because I have searched this point in many web-page and not find any suitable way to do this.But, SKOTechLearn will explain simple tips to change Particular Column color of jTable in Java it, write or copy given code and see what happen.
First you have to import DefaultTableCellRenderer class:
import javax.swing.table.DefaultTableCellRenderer;
After that write given code:
DefaultTableCellRenderer MyCellrendar = new DefaultTableCellRenderer(); MyCellrendar.setBackground(Color.yellow); MyCellrendar.setForeground(Color.RED); MyTblView.getColumnModel().getColumn(1).setCellRenderer(MyCellrendar);
The output will be display like:
In getColumn() method, you can put any column number inside it and this will apply on that particular column's rows.
(5). jTable Selection background and Foreground Color :
In java, there is facility to change selection color of particular rows in jtable according to requirement.You can apply one line code as described below:
MyTblView.setSelectionBackground(Color.BLACK); MyTblView.setSelectionForeground(Color.YELLOW);
(6). jTable Selection Background and Foreground Setting in NetBeans :
If you are using NetBeans, then you can simply go to “Properties” window and find “selectionBackground” and “selectionForeground” option and choose color according to your requirement.Steps are described in given following image:
So, these are the color changing process which is explained in Change jTable Column Header and Rows Color in Java NetBeans Easily.
0 comments: