Home Java Example Java Swing Setting the Height and Width of Rows and columns in JTable
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Setting the Height and Width of Rows and columns in JTable

Advertisement
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:

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Ask Questions?    Discuss: Setting the Height and Width of Rows and columns in JTable  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Dibyojyoti
April 3, 2012
Dynamically adjust width of a JTable

Hi, I am creating a JTable dynamically using TableModel and adding the columns dynamically to that table and after that adding the table in jScrollpane. I am able to set the width of the colums also using setPreferredWidth() but the whole width of the table is comming same. please let me know how can I set the whole width of the table dynamically
Andre jonn
August 1, 2012
Db query

How can i query a db table to output entries starting with a particular alphabet.ex.i have this app that i developed using GUI.i created a dialog which enables the user to search for an entry by inputting the name into a txtfld n hitting the "search" button.but the thing is what if the user wants to only display only names starting from an alphabet like say "y" so he types only "y" in the textfld.all i get is an error.i wrote this line in ma code: String str = "SELECT * FROM table_name WHERE column_name LIKE column_name = ? + ''%"; all i get are errors even when i changed it.please help me out here all i need is that line of code that outputs all the data that starts with the alphabet inputted by the user in the txtfld. Please can u send it to my mail listed above.i'd appreciate it
DMCA.com