How to Resize the Table Column in Java Runtime

0
29

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.

  1. import javax.swing.table.TableColumnModel;

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

  1. public final void resizeTableColumn(){
  2.  
  3. }

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.

  1. TableColumnModel tcm = jTable1.getColumnModel();
  2. tcm.getColumn(0).setPreferredWidth(80);
  3. tcm.getColumn(1).setPreferredWidth(10);
  4. 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.

  1. public ReziesTheTableColumn() {
  2. initComponents();
  3. resizeTableColumn();
  4. }

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

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:

Facebook Comments
(Visited 32 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here