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:

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