Setting the Margin Between Cells in a JTable

In this section, you will learn how to set the margin (Gap) between cells in a JTable component.

Setting the Margin Between Cells in a JTable

In this section, you will learn how to set the margin (Gap) between cells in a JTable component.

Setting the Margin Between Cells in a JTable

Setting the Margin Between Cells in a JTable

     

In this section, you will learn how to set the margin (Gap) between cells in a JTable component. Here we are providing you an example with code that arranges the column margin (Horizontal space) and row margin (Vertical space). Whenever you increase the column margin, you will get the cell widths  increased automatically. Unlike column margin, row margin does not increase automatically, whenever row margin has been increased. 

Description of program:

This program helps you for how to set the margin in JTable. For this first of all, you will create a JTable containing the data and columns with column header. The column header have the column name. Then you will have to set the internal gaps (space) between the cells through the setIntercellSpacing() method. Now apply the SetRowHeight() method for setting the height of rows through using the setRowHeight() method. The entire process has completed now, you will get the set margin between cells in a JTable.

Description of code:

setIntercellSpacing(Dimension dim):
This is the method sets the row margin and column margin. It takes 'Dimension' type data.

    dim: It specifies the new height and width between cells.

getRowHeight():
This method returns the height of a row in pixels.

setRowHeight(int row, int row_height):
This is the method to set the height of a row. It takes two integer types arguments:

    row: This is the row that have to be changed.
  row_height: It defines the  height of  a row in pixels.

Here is the code of program:

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class CellGap{
  JTable table;
  public static void main(String[] args) {
  new CellGap();
  }

  public CellGap(){
  JFrame frame = new JFrame("Setting the margin in JTable Component!");
  JPanel panel = new JPanel();
  String data[][] {{"Vinod","Computer","3"},
   {
"Rahul","History","2"},
   {
"Manoj","Biology","4"},
   {
"Sanjay","PSD","5"}};
  String col [] {"Name","Course","Year"};
  DefaultTableModel model = new DefaultTableModel(data,col);
  table = new JTable(model);
  Dimension dim = new Dimension(20,1);
  table.setIntercellSpacing(new Dimension(dim));
  SetRowHight(table);
  table.setColumnSelectionAllowed(true);
  JTableHeader header = table.getTableHeader();
  header.setBackground(Color.yellow);
  JScrollPane pane = new JScrollPane(table);
  panel.add(pane);
  frame.add(panel);
  frame.setSize(500,150);
  frame.setUndecorated(true);
  frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
  }
  public void SetRowHight(JTable table){
  int height = table.getRowHeight();
  table.setRowHeight(height+10);
  }
}

Download this example.

Output of program: