Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Problem in setting column width..

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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() {
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...

 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moses, could you please use code tags next time?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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(0).setPreferredWidth(width);
table.getColumnModel().getColumn(1).setPreferredWidth(width * 15);
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic