• 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
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

NX: Set JTable columns display in different sizes

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to know how to do it too? because all the coloumns of my table are the same size , anyone know how?
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Billy
Why do you want to try and disable this? It is a very user friendly feature.
Regards, Andrew
 
Billy Tsai
Ranch Hand
Posts: 1327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 442
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I got it.
 
S. Ganapathy
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 194
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 1327
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 442
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    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?
     
    Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic