This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX: Set JTable columns display in different sizes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX: Set JTable columns display in different sizes" Watch "NX: Set JTable columns display in different sizes" New topic
Author

NX: Set JTable columns display in different sizes

Zhi Gang
Ranch Hand

Joined: May 03, 2003
Posts: 30
hi there my JTable is using my own RecordTableModel which is extending the AbstractTableModel. I am trying to re-arrange the column size, so that when it display will be a different length.
ie. Name, Location, size
Name column will be in size 14
Name column will be in size 10
size column will be in size 6
but i am not sure how should i do it, and where .. please let me know if u have any idea..
thanks
Billy Tsai
Ranch Hand

Joined: May 23, 2003
Posts: 1297
I want to know how to do it too? because all the coloumns of my table are the same size , anyone know how?


BEA 8.1 Certified Administrator, IBM Certified Solution Developer For XML 1.1 and Related Technologies, SCJP, SCWCD, SCBCD, SCDJWS, SCJD, SCEA,
Oracle Certified Master Java EE 5 Enterprise Architect
SrinivasMA Srini
Greenhorn

Joined: Feb 21, 2003
Posts: 28
Hi,
If you design your table model like this, your problem hopefully be solved.
public class DataTableModel extends AbstractTableModel { }
Make a trail.
Cheers
Srinivas
SCJP,SCJD
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
Originally posted by SrinivasMA Srini:
Hi,
If you design your table model like this, your problem hopefully be solved.
public class DataTableModel extends AbstractTableModel { }
Make a trail.
Cheers
Srinivas
SCJP,SCJD


actually the AbstractTableModel has nothing to do with the view, well not in this regard anyway.
the change needs to happen on the JTable, heres some sample code that will help you build the loop you need.


Column Widths
It is very easy to change the column widths on a JTable. First, you must create a new TableColumn object and set it to null. Next, you must specify which column whose size you would like to change. Last, you must use the method setPreferredWidth(int) method to change the size. If you would like to change all the column sizes, you might want to run a simple for loop to do this task.
Add the following code after: table.setShowVerticalLines(false);
TableColumn column = null;
column = table.getColumnModel().getColumn(1);
column.setPreferredWidth(200);
This code will change the width of the second column. Compile and run the application to see this change take place.

NB table.setShowVerticalLines(false); is not required, it will work with or without the lines
Billy Tsai
Ranch Hand

Joined: May 23, 2003
Posts: 1297
how about the codes to prevent coloumns being moved , like you can drag the name coloumn over to the location coloumn and the location coloumn becomes the first coloumn in the table and name is the second etc..
I dont want the coloumns able to be dragged by the mouse, I want to disable dragging for the coloumns, any idea how to do that?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Billy
Why do you want to try and disable this? It is a very user friendly feature.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Billy Tsai
Ranch Hand

Joined: May 23, 2003
Posts: 1297
because I just dont feel good looking at the coloumns being moved and also I cant allow the coloumns being changed to a different position because I use use mouseListen to get the name of the currently clicked selected coloumn, and if the name coloumn is being moved to another position instead of being the first coloumn then the information I get could be the location , size etc... but I want the name
if there is a away to disable dragging the coloumns please tell me thanks
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
Originally posted by Billy Tsai:
because I just dont feel good looking at the coloumns being moved and also I cant allow the coloumns being changed to a different position because I use use mouseListen to get the name of the currently clicked selected coloumn, and if the name coloumn is being moved to another position instead of being the first coloumn then the information I get could be the location , size etc... but I want the name
if there is a away to disable dragging the coloumns please tell me thanks


cant answer that question, but seriously consider Andrews statement about it being user friendly, it really is something users expect, and why use MouseListener to know the column clicked when the JTable can tell you that and lots more?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Billy
From the API documentation:
By default, columns may be rearranged in the JTable so that the view's columns appear in a different order to the columns in the model. This does not affect the implementation of the model at all: when the columns are reordered, the JTable maintains the new order of the columns internally and converts its column indices before querying the model.

This should mean that you do not have to worry so much about an input map, or column reordering. Just use the standard methods to get where the user has clicked and what that column name is.
Let's say you have a table like:
And the user rearranges it like:
Then the user clicks in the marked cell:
When you get the event, you should be told that the user clicked in row 4, and when you ask for the heading for column 4 you should get 'D'.
However to answer your question, take a look at the getTableHeader() method of the JTable, and then at the setReorderingAllowed() method of TableHeaders.
Regards, Andrew
Billy Tsai
Ranch Hand

Joined: May 23, 2003
Posts: 1297
Thank you
S. Ganapathy
Ranch Hand

Joined: Mar 26, 2003
Posts: 194
Hi,
In JTable, we can drag columns. Say I dragged column 2 and dropped in column 5, and all columns will get adjusted accordingly. If I want to avoid this, are there any ways to do?
Please advice me.
Regards,
Ganapathy.
S. Ganapathy
Ranch Hand

Joined: Mar 26, 2003
Posts: 194
Thanks, I got it.
S. Ganapathy
Ranch Hand

Joined: Mar 26, 2003
Posts: 194
Hi,
I have a problem while displaying data in JTable. I want to display rate column right justified. Rate contains rate and currency symbol. Is there any way to set the alignment for any particular column, or is there any work around?
Please help me.
Regards,
Ganapathy.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Ganapathy

In JTable, we can drag columns. .... If I want to avoid this, are there any ways to do?
Thanks, I got it.

Hopefully you also got that I think this is a really bad idea. To recap why:
  • users are used to being able to rearrange columns to suit their way of looking at data
  • the JTable already hides the movement of columns from you, so your coding should be the same whether you allow the columns to be dragged or not.


  • I have a problem while displaying data in JTable. I want to display rate column right justified. Rate contains rate and currency symbol. Is there any way to set the alignment for any particular column, or is there any work around?

    If you make your own TableModel you can easily override the getColumnClass() method and have it return an Integer for the given column. It doesnt matter that the data in that column is not a pure Integer - it is just used to determine how to layout the data.
    If you have not yet gone through the Sun tutorial on JTables then I strongly recommend you do so. Not just for the assignment, but also for your own knowledge.
    Regards, Andrew
    S. Ganapathy
    Ranch Hand

    Joined: Mar 26, 2003
    Posts: 194
    Hi Andrew,
    Hopefully you also got that I think this is a really bad idea. To recap why:
    users are used to being able to rearrange columns to suit their way of looking at data
    the JTable already hides the movement of columns from you, so your coding should be the same whether you allow the columns to be dragged or not.

    I do aggree with you. But there is a problem in doing so. If the first column displays integer values, and the second column represents string values, and if you drag column one and place in second, there is some risk involved in it.
    If you read first column value(assuming integer), and does any processing, you actually reading string inplace of integer.
    I am facing this problem. In Contractors, I am displaying record number as well. While user double clicks on any row, it first reads record number from first column, and gets the latest value of that record number. While doing so, it raises NumberFormatException. For this reason, I disabled reordering of columns.
    If you make your own TableModel you can easily override the getColumnClass() method and have it return an Integer for the given column. It doesnt matter that the data in that column is not a pure Integer - it is just used to determine how to layout the data.

    I did the same. But if developer wish to set size for columns, TableColumn.getHeaderRenderer() will return null. This was problem in 1.3.x, and still there is problem in 1.4.x it seems. I tried on 1.4.1, and still it returns null. I left the default implementation(all columns have equal widths).
    Comments are welcome.
    Regards,
    Ganapathy.
    Billy Tsai
    Ranch Hand

    Joined: May 23, 2003
    Posts: 1297
    the answer to disable dragging or moving a coloumn is b4 my thank you post ,
    take a look at the getTableHeader() method of the JTable, and then at the setReorderingAllowed() method of TableHeaders in the API .......
    I did the same thing as u did when user click a record it displays record number clicked
    Ta Ri Ki Sun
    Ranch Hand

    Joined: Mar 26, 2002
    Posts: 442

    I did the same. But if developer wish to set size for columns, TableColumn.getHeaderRenderer() will return null. This was problem in 1.3.x, and still there is problem in 1.4.x it seems. I tried on 1.4.1, and still it returns null. I left the default implementation(all columns have equal widths).
    Comments are welcome.

    I dont use TableColumn.getHeaderRenderer() at all and I set size for my columns, I need to find a better way because right now I take the columns max length and multiply by 3, and the size is absolutely perfect, because if its too small it will be re-adjusted, very little work on my side, anyway, I also return Integer.class for both Room.SIZE as well as Room.RATE, and Object.class for all else, and I'm able to shift columns about and maintain the size without problems.
    what I do have a problem with tho, well its not a problem yet because I haven't had time to look at it for more than a minute, but I need to keep the users previously selected column order even after searching, are you guys doing this?
     
     
    subject: NX: Set JTable columns display in different sizes
     
    Similar Threads
    jtable
    JTable
    JTable
    JTable issues
    JTable Column Size