Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How can I resize a JTable's column width?

Reply
Thread Tools

How can I resize a JTable's column width?

 
 
gajo
Guest
Posts: n/a
 
      06-25-2004
Hi,
I've created a JTable and filled it out with some data, but by default the
table's header is filled out by values like A,B,C,... So I've created a new
JTableHeader which is wrapped around a DefaultTableColumnModel object that
I've created and to which I have added the neccessary columns and set their
size and text.
After I start the application, the header of the table looks good, but the
table columns do not follow the header's column's width. That is, the width
of the header's columns are not equals to the table's.
I tried to make the table's column model the same as the header's, by doing
table.setColumnModel(myColumns); but instead of getting what I wanted I've
got an empty table (the values I entered did not show up), and all the
column widths were equal and set to some default value. I could tollerate
even that, but why don't the cell values show up? I tried revalidating,
repainting and doLayout(), but nothing works.

So what should I do to customize the table's column width to be equal to
that of the headers?
And btw. how can I add a scrollbar to the table? Wrapping a ScrollPane
around it doesn't seem to work!

Here's some code for further explanation, just in case:
// I create the table
myTable = new JTable(numRows, 3);

// I create the column model
TableColumn firstCol = new TableColumn();
firstCol.setHeaderValue("Red");
firstCol.setWidth(50);
TableColumn secondCol = new TableColumn();
secondCol.setHeaderValue("Blue");
secondCol.setWidth(80);
TableColumn thirdCol = new TableColumn();
thirdCol.setHeaderValue("Brown");
thirdCol.setWidth(40);

DefaultTableColumnModel myCols= new DefaultTableColumnModel();
myCols.addColumn(firstCol);
myCols.addColumn(secondCol);
myCols.addColumn(thirdCol);

// I set the table's header
myTable.setTableHeader(new JTableHeader(myCols));

// if I do this the cell values won't show
// myTable.setColumnModel(myCols);

// I add the data to the cells
for (int i = 0; i<numRows; i++) {
myTable.setValueAt("bear", i, 0);
myTable.setValueAt("fox", i, 0);
myTable.setValueAt("pheasant", i, 0);
}
 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      06-25-2004
"gajo" <> wrote in message
news: om...
> Hi,
> I've created a JTable and filled it out with some data, but by default the
> table's header is filled out by values like A,B,C,... So I've created a

new
> JTableHeader which is wrapped around a DefaultTableColumnModel object that
> I've created and to which I have added the neccessary columns and set

their
> size and text.


The usual simple approach is to sublclass AbstractTableModel and override
getComumnName(int index);

However, the same model object should be used for both header and table.

--
Mike W


 
Reply With Quote
 
 
 
 
Pacific Guy
Guest
Posts: n/a
 
      06-25-2004
(gajo) wrote in message news:<. com>...
> Hi,
> I've created a JTable and filled it out with some data, but by default the
> table's header is filled out by values like A,B,C,... So I've created a new
> JTableHeader which is wrapped around a DefaultTableColumnModel object that
> I've created and to which I have added the neccessary columns and set their
> size and text.
> After I start the application, the header of the table looks good, but the
> table columns do not follow the header's column's width. That is, the width
> of the header's columns are not equals to the table's.
> I tried to make the table's column model the same as the header's, by doing
> table.setColumnModel(myColumns); but instead of getting what I wanted I've
> got an empty table (the values I entered did not show up), and all the
> column widths were equal and set to some default value. I could tollerate
> even that, but why don't the cell values show up? I tried revalidating,
> repainting and doLayout(), but nothing works.
>
> So what should I do to customize the table's column width to be equal to
> that of the headers?
> And btw. how can I add a scrollbar to the table? Wrapping a ScrollPane
> around it doesn't seem to work!
>


I'm not sure what you're using as the datamodel for your table, but if
you're using a custom object as opposed to a simple array, implement
it's public String getColumnName(int idx){} method to return the
correct column name. If you do that the JTable will output the correct
column header names and your column header widths will stay synched up
with the widths of the table columns.

If you are using simple arrays, follow the constructor example at the
top of this tutorial to display column headers with simple data from
arrays:

http://java.sun.com/docs/books/tutor...nts/table.html


> Here's some code for further explanation, just in case:
> // I create the table
> myTable = new JTable(numRows, 3);
>
> // I create the column model
> TableColumn firstCol = new TableColumn();
> firstCol.setHeaderValue("Red");
> firstCol.setWidth(50);
> TableColumn secondCol = new TableColumn();
> secondCol.setHeaderValue("Blue");
> secondCol.setWidth(80);
> TableColumn thirdCol = new TableColumn();
> thirdCol.setHeaderValue("Brown");
> thirdCol.setWidth(40);
>
> DefaultTableColumnModel myCols= new DefaultTableColumnModel();
> myCols.addColumn(firstCol);
> myCols.addColumn(secondCol);
> myCols.addColumn(thirdCol);
>
> // I set the table's header
> myTable.setTableHeader(new JTableHeader(myCols));
>
> // if I do this the cell values won't show
> // myTable.setColumnModel(myCols);
>
> // I add the data to the cells
> for (int i = 0; i<numRows; i++) {
> myTable.setValueAt("bear", i, 0);
> myTable.setValueAt("fox", i, 0);
> myTable.setValueAt("pheasant", i, 0);
> }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid Column Resize Dragon ASP .Net 0 05-08-2007 03:45 PM
css column misalign until FF/IE window resize warrenjamestaylor@gmail.com HTML 0 02-04-2006 09:56 PM
How to resize all images sizes and coordinates of the images on resize browser rams.kakara@gmail.com ASP General 2 02-13-2005 09:03 AM
Resize table column Yama ASP .Net 2 11-09-2004 06:18 PM
Column won't resize in datagrid VB Programmer ASP .Net 3 09-28-2004 03:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57