• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Problem in setting column width..  RSS feed

Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to set width of each column...
Means I want to change width of each column..
In this given code width of each column is same...
But I want each column of different width...
as Like 1st column is of width 50,2ed is of 100,3rd is of 120....
Please make change in this given code ...
haw can i change width of each column

Please get me out of this problem...
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Friends.. Good Day....

Your program is good that has no problem. But you can use TableColumn and DefaultTableColumnModel instead of directly add column to table. It has more methods to change column's model, width, fontcolor etc.,

Here is Sample Program...

import javax.swing.*;
import javax.swing.table.*;

TableColumn and DefaultTableColumnModel both are present in
javax.swing.tree package

public class DemoTable extends JFrame {

JTable table;
TableColumn column1, column2, column3;
DefaultTableColumnModel headers;
DefaultTableModel rows;

public DemoTable() {

//create and define columns
column1 = new TableColumn(0);
column1.setHeaderValue("First Name"); // set column name
column1.setPreferredWidth(50); //set column width

column2 = new TableColumn(1);
column2.setHeaderValue("Last Name");

column3 = new TableColumn(2);

// A DefaultTableColumnModel object is created and the three
// TableColumn objects are added to it.

headers = new DefaultTableColumnModel();

//Create two vector objects and load them with the data
//to be placed on each row of the table

Object[] v1 = {"Lisa","Reid",new Integer(1990) };
Object[] v2 = {"Cherry","Spada",new Integer(1989) };

// A DefaultTableModel object is created initally with 0 rows and 3 columns.
// two rows are added containing the data int the object array.

rows = new DefaultTableModel(0,3);

table = new JTable(rows, headers);

getContentPane().add(new JScrollPane(table));


public static void main(String args[]) {
new DemoTable();

I hope this is help you..

Posts: 3040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" which will show you how to set the column size among other things.
Posts: 21742
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moses, could you please use code tags next time?
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Prime wrote:Moses, could you please use code tags next time?

Basically their are 2 ways to do it:

1. One usage tablecolumn model directly as mentioned in earlier comment.

2. You can set the column size explicitly by setWidth method
here is the sample:
Dimension dim = table.getPreferredSize();
int width = dim.width / 4;
table.getColumnModel().getColumn(1).setPreferredWidth(width * 15);
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!