How to resize the column in jtable using java tutorial

0
64

How to resize the column in jtable using java tutorial

This tutorial is all about How to resize the column in jtable using java tutorial. In this tutorial you will learn how to set the width of column in jtable using java that fit to your data in order to look better and to display the data properly in table. This tutorial uses JTable Components and Netbeans IDE. Please follow all the steps below to complete this tutorial.

How to resize the column in jtable using java tutorial Steps

 

First Create your project by clicking file at the top of your project and then click new project, you can name your project whatever you want. And then remove the default column by right clicking the table and the click table contents. Navigate to the column tab and then click all the default column and just delete by click the delete button and then close. After that copy and paste the code below

 

try{

String sql = “SELECT company_id as ‘ID’,company_name as ‘Company Name’,email as ‘Email’,contact_no as ‘Contact’,address as ‘Address’ FROM company”;

pst = conn.prepareStatement(sql);

rs = pst.executeQuery();

while(rs.next()){

table.setModel(DbUtils.resultSetToTableModel(rs));

}

table.getColumnModel().getColumn(0).setPreferredWidth(40);

table.getColumnModel().getColumn(1).setPreferredWidth(120);

 

table.getColumnModel().getColumn(2).setPreferredWidth(150);

table.getColumnModel().getColumn(3).setPreferredWidth(100);

 

table.getColumnModel().getColumn(4).setMinWidth(150);

}catch(Exception e){

JOptionPane.showMessageDialog(null, e);

}

[ /java]

You can resize the column by accessing its index.

table.getColumnModel().getColumn(3).setPreferredWidth(100);

table is the instance of your table , and the 3 is the index of the column

Note: column index always start at 0.

 

Run your project and see if it works.

 

 

For questions or any other concerns or thesis/capstone creation with documentation, you can contact me through the following:

E-Mail: [email protected]

Facebook: facebook.com/RyyanSalem

Contact No.: +639098911050

Ryan A. Salem

BSIT Graduate, soon to be MIT.

Java Developer / System Developer

Related topic(s) that you make like:

1.) Create Login Form in Java
2.) Medical Store Management System using Java Netbeans

 

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here