Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
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. 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:

                         

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

Current Comments

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

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2007. All rights reserved.