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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Setting the Height and Width of Rows and columns in JTable 
 

In this java tutorial you will learn how to set the height and width of rows and columns in JTable.

 

Setting the Height and Width of Rows and columns in JTable

                         

In this java tutorial you will learn how to set the height and width of rows and columns in JTable. Earlier, you have read about the JTable that contains the data in rows and columns format so, each and every rows and columns have some specific height and width for containing the data. JTable provides some specified height and width for the table. Whenever  you want to store the larger data in it,  you will have to increase it, after that you can store the data in it that will look clearly. 

Description of program:

In this program we are going to set the height and width of rows and columns in JTable. This table has multiple data but some data are longer for this we must require for setting its height and width by using the setPreferredWidth() and setRowHeight(). The setPerferredWidth method helps us for setting the column width and setRowHeight method is used to set the rows height. These methods are used to set the height and width of  all rows and columns but if you want to set the specific rows and columns then you must  maintain its index of rows and column. See the example:-  

Code of program:

TableColumn:
This is the class that represents all descriptions of a column in a JTable like: width, maximum and minimum width, resizable.  It provides the multiple slots to a renderer and an editor that is used for displaying and editing the values in the column.

getColumnModel():
Above method contains all columns information of a specified table and returns the TableColumnModel object.

getColumn(int col_index):
This method takes integer type value (index of column of a table) that will match on those table's column index and returns the TableColumn object.

setPreferredWidth(int col_width):
This is the method that set the column width according to specification. It takes integer type value. Whenever,  you entered the value minimum or maximum of  its limiting value, it will adjust to an appropriate limiting value.

setRowHeight(int row_height):
This method is used for setting the height in pixels of all rows of a table. It takes integer type value to set each rows of  the table depending upon it.

    row_height: It denotes rows height of a table.

setRowHeight(int row_index, int row_height):
This  method sets the row's height of specific row that takes two arguments e.g:

    row_index: It is an integer value that represents the index of rows that works for modifying the row.
    row_height: It is an integer value that represents the height of rows that works  for modifying the height in the row.

Here is the code of program:

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

public class setRowHightAndColBidth{
  public static void main(String[] args) {
    new setRowHightAndColBidth();
  }
  public setRowHightAndColBidth(){
    JFrame frame = new JFrame("Creating JTable Component Example!");
    JPanel panel = new JPanel();
    String data[][] {{"vinod","BCA","A"},{"Raju","MCA","b"},
 {
"Ranjan","MBA","c"},{"Rinku","BCA","d"},{"Rama anuj pathak tiwari","PGDCA","e"}};
    String col[] {"Name","Course","Grade"};
    JTable table = new JTable(data,col);
    //first column width
    TableColumn column = table.getColumnModel().getColumn(0);
    column.setPreferredWidth(150);
    //set all rows height 
    table.setRowHeight(20);
    //set specific row height
    table.setRowHeight(2,50);
    //Select single cell
    table.setCellSelectionEnabled(true);
    panel.add(table,BorderLayout.CENTER);
    frame.add(panel);
    frame.setSize(300,200);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Download this example.

Output of program:

                         

» View all related tutorials
Related Tags: c ide class methods method get icon height vi width change using int id ai create js define show paint

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 
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.