Friday, May 11, 2012

How to set color in jTable Column Header using NetBeans

2 comments
All NetBeans user see How to set background color and foreground color to jTable Column Header by use of DefaultTableModel.In my preveous post we learn how to display values in jTable by using DefaultTableModel.Netbeans provides Swings Controls which minimize our coding time and helps to build GUI faster than core coding.

Discription of program :
  • Create a New jframe in NetBeans.
  • From Palette drag and drop table over the jFrame.
  • Another way to open palette in NetBeans, windows --> palette.
  • See how to program.
  • DefaultTableModel is first must be declare as global variable.
  • Define separate method for set DefaultTableModel to jTable model or you code directly without creating separate method.Defining such type of methods improve coding standards so always go for this way.
  • After defining method call that method in constructor.
  • Here we define method is settable();
  • In settable() method we use ColumnIdentifiers to set column header name. The method getTableHeader used to get column header and methods setBackground(Color.PINK) and setForeground(Color.blue) used for set background color to column header and set separate color to column header name.
  • Source Code :
  • See how to call settable() methode in constructor.
package jdefaulttabledemo;
import java.awt.Color;
import javax.swing.table.DefaultTableModel;

public class jTableHeader extends javax.swing.JFrame {

    DefaultTableModel dm;
    public jTableHeader() {
        initComponents();
        settable();
        
    }
  • Source Code for settable() method.
 private void settable() {
       String s[][]={{"Arun","www.java4projects.blogspot.in"},{"Digvijay","www.tutorialdata.com"}};
       String s1[]=new String []{"Blogger Name","Blogger website"};
       dm = new DefaultTableModel(s, s1);
       dm.setColumnIdentifiers(s1);
        jTable1.getTableHeader().setBackground(Color.PINK);
        jTable1.getTableHeader().setForeground(Color.BLUE);
        jTable1.setModel(dm);
    }
  • Out Put :

2 Responses so far

  1. webseos says:

    A good tutorial on Netbeans JTable to set Header row coloured. You can add tabs by code too, Thanks Share Market Teachers in Kolkata on Stock, F&O, Options Trading Courses.

Leave a Reply