[an error occurred while processing this directive]

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.

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"},
    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));
    JTableHeader header = table.getTableHeader();
    JScrollPane pane = new JScrollPane(table);
  public void SetRowHight(JTable table){
    int height = table.getRowHeight();

Download this example.

Output of program:


[an error occurred while processing this directive]