How to Resize the Table Column in Java Runtime

This tutorial is all about “How to Resize the Table Column in Java Runtime” will teach you on how you can create program equipped with a Java table that automatically resize the table column when the program is running.

Resizing the table column and row is not difficult to a user because user can directly resize it by dragging the table column and row. But in some cases, there are programs that the Java table will automatically resize the column when the program is running. This feature will help the table to avoid missing information. When you run the program, the Java table will automatically resize depending on the settings you setup on the codes.

How to Resize the Table Column in Java Runtime Steps

Create or add a new form inside your Java source package. In my case, I named my form using “ResizeTheTableColumn”.

Design your new created form just look like the image below. The form uses a Java Table element located from your Netbeans Palettes.

Insert the following codes above your main class. The code is use to access the required library needed in this program.

[java]import javax.swing.table.TableColumnModel;[/java]

Create a new method inside your class. In my case, I use “resizeTableColumn” method name.

public final void resizeTableColumn(){

}

Insert the following codes below inside your new created method. The codes also use the table model located in your table libraries. The “.getColumn(0)” statement served as the index of your column and “.setPreferredWidth(80)” statement served as the size of the column.

TableColumnModel tcm = jTable1.getColumnModel();
tcm.getColumn(0).setPreferredWidth(80);
tcm.getColumn(1).setPreferredWidth(10);
tcm.getColumn(2).setPreferredWidth(10);

 

Copy the method name you created and paste or insert it into the public method inside your main class.  Just insert it after the “initComponents”. The “initComponents” will access the constructor that draws all the elements inside your form.

public ReziesTheTableColumn() {
initComponents();
resizeTableColumn();
}

 

Run your program and the output should look like the image below.

About How to Resize the Table Column In Java

Project Name: How to Resize the Table Column
Language/s Used: JAVA
Database: None
Type: Desktop Application
Developer: IT SOURCECODE
Updates: 0
How to Resize the Table Column In Java– Project Information

If you have comments and suggestion about Resizing the Table Column in Java, feel free to contact us. Remember that this program can only resize the table column and not the row.

Related Articles You May Like:

Leave a Comment