File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Problem in setting column width.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem in setting column width.." Watch "Problem in setting column width.." New topic
Author

Problem in setting column width..

arun singhal
Ranch Hand

Joined: Oct 25, 2009
Posts: 41
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...
Moses Francis
Greenhorn

Joined: Nov 09, 2009
Posts: 1
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() {
setTitle("Table");

//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");
column2.setPreferredWidth(100);

column3 = new TableColumn(2);
column3.setHeaderValue("Year");
column3.setPreferredWidth(120);

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

headers = new DefaultTableColumnModel();
headers.addColumn(column1);
headers.addColumn(column2);
headers.addColumn(column3);

//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);
rows.addRow(v1);
rows.addRow(v2);

table = new JTable(rows, headers);

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

setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

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

I hope this is help you..
Bye...

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2226
    
    8
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Moses, could you please use code tags next time?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
santhosh varala kumar
Greenhorn

Joined: Nov 05, 2009
Posts: 25
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(0).setPreferredWidth(width);
table.getColumnModel().getColumn(1).setPreferredWidth(width * 15);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in setting column width..