Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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. 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:

                         

» View all related tutorials
Related Tags: c class orm form io method sed format 2d get ip translation return rotation instance transform show for transformation to

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

hello sir

I want to print my Jtable Records in Netbeans
what I use to do that ..????????

Posted by Anju on Monday, 09.15.08 @ 17:02pm | #79587

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.