Desktop Java 8 – part 5– How in the simple way to colour JTable component in frame?

Another topic about Java desktop application. In it You find answer how make colour table component in Java JFrame.

Our last code JTable component in frame is:

package doraprojects;

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class SimpleTable 
{   
public static final int WIDTH_WINDOW = 600;
public static final int HEIGHT_WINDOW = 500;
    public static void main(String[] args) 
    {   
    EventQueue.invokeLater(new Runnable(){
     public void run()
     {    
        JFrame f = new JFrame("My window");
        f.setSize(WIDTH_WINDOW, HEIGHT_WINDOW); 
        f.getContentPane().setBackground(Color.white);
           String[] columns = { "ID","Name","product","price in $" };  
           Object[][] data = { {1234,"John","pencil",10},
                               {4382,"Mirian","pen",11}, 
                               {2345,"Katy","chair",253}, 
                               {6334,"Agnes","table",456}, 
                               {1177,"Stanley","flower",25} };    
         JTable table = new JTable(data, columns);
         table.getColumnModel().getColumn(0).setPreferredWidth(80);
         table.getColumnModel().getColumn(1).setPreferredWidth(100);
         table.getColumnModel().getColumn(2).setPreferredWidth(150);
         table.getColumnModel().getColumn(3).setPreferredWidth(100);
         
         table.setAutoCreateRowSorter(true);
         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
         JScrollPane scroll = new JScrollPane(table);
         
         f.add(scroll);
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         f.setVisible(true);
        
      }
        }); 
   }
}

After launch this application You see:

First we colour all cell in table. To do it we use setBackground function and call it for table object. This function get only one parameter: Color object.

...
         JTable table = new JTable(data, columns);
         table.setBackground(new Color(195,232,255));
         table.getColumnModel().getColumn(0).setPreferredWidth(80);
         table.getColumnModel().getColumn(1).setPreferredWidth(100);
         table.getColumnModel().getColumn(2).setPreferredWidth(150);
         table.getColumnModel().getColumn(3).setPreferredWidth(100);
         
         table.setAutoCreateRowSorter(true);
         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
         JScrollPane scroll = new JScrollPane(table);
         
         f.setLayout(new FlowLayout(FlowLayout.LEFT));
         f.add(scroll);
...

The table with blue color will be display:

And what to do that to colour header in this table? You need call setBackgroud function with Color objects as parameter, but for once to JTableHeader object. This object is getting through getTableHeader method.

...
         JTable table = new JTable(data, columns);
         table.setBackground(new Color(195,232,255));
         table.getColumnModel().getColumn(0).setPreferredWidth(80);
         table.getColumnModel().getColumn(1).setPreferredWidth(100);
         table.getColumnModel().getColumn(2).setPreferredWidth(150);
         table.getColumnModel().getColumn(3).setPreferredWidth(100);
table.getTableHeader().setBackground(new Color(100,195,255));
table.setAutoCreateRowSorter(true);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scroll = new JScrollPane(table);

f.setLayout(new FlowLayout(FlowLayout.LEFT));
f.add(scroll);
...

Result:

Around data in table is seen gray color. Change it You may through get ViewPort form JScrollPane object and call for it setBackground method with Color object.

...
         JTable table = new JTable(data, columns);
         table.setBackground(new Color(195,232,255));
         table.getColumnModel().getColumn(0).setPreferredWidth(80);
         table.getColumnModel().getColumn(1).setPreferredWidth(100);
         table.getColumnModel().getColumn(2).setPreferredWidth(150);
         table.getColumnModel().getColumn(3).setPreferredWidth(100);
         table.getTableHeader().setBackground(new Color(100,195,255));
         table.setAutoCreateRowSorter(true);
         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scroll = new JScrollPane(table);
scroll.getViewport().setBackground(new Color(3,123,225));
f.setLayout(new FlowLayout(FlowLayout.LEFT));
f.add(scroll);
...

Effect launching application:
q4