Resizing a table's columns

Posted on 2003-02-20
Medium Priority
Last Modified: 2010-08-05
I have a problem with JTable.
There exists a GUI framework based on Swing and I have the phenomenon that MOST of the tables have columns with an identical width but a few do have (as demanded).

I use this statement to define the widths of the table's columns:
myTable.setPreferredColumnWidth(new int[] {12, 10, 10, 10,170, 10, 50, 130, 10, 70, 10});

In the very next statement I display the set widths on the console this way:
int c = myTable.getColumnModel().getColumnCount();
System.err.println("Columns to be initialized: " + c);
for(int i=0; i<c; i++)
  int w = myTable().getColumnModel().getColumn(i).getPreferredWidth();
  System.err.print("[" + w + "] ");
}//end for i

ALL COLUMN WIDTHS ARE SET TO 75 (which seems to be a default width).

How can I prevent from such a behaviour?

Using this:

do nothing.

Hope anybody can help me very soon.

Thank you in advance,

Berlin, Germany
Question by:dirku
  • 4
  • 3
  • 2
  • +1
LVL 35

Expert Comment

ID: 7986444
 Can you try using:

  TableColumn column = myTable.getColumnModel().getColumn(0);

  ... and so on, and let us know what happens?

Author Comment

ID: 8016207
Hi, girionis.

This has no effect.
LVL 35

Expert Comment

ID: 8016251
 Weird... that should do. Can you post a sample compilable example?
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 8017774
No, that's not possible. Unfortunately.

There is GUI framework implemented which encapsulates Swing. Thus, I had to send you tons of Java-classes, ...
besides it would be hard to undestand the way dataSets are used to initialize everything in this application (it's all horrible designed and implemented by the company).

I guess, this framework is responsible for this weird behaviour. When running the C/S-application within JBuilder everything is fine. Starting the app without the IDE the mentioned "table behaviour" appears.
LVL 35

Expert Comment

ID: 8017827
 Sorry then I do not have any more suggestions... I ran out of ideas. When I want to resize the columns I always use the  column.setPreferredWidth() method since this will reflect the changes to the table as well.

> Starting the app without the IDE the mentioned "table behaviour" appears.

  It shouldn't though. Are you sure you are using a VM that complies to the Java standards?

Expert Comment

ID: 8071024
I think I know what the problem is: does your code set the minimum width of the columns anywhere? The default behaviour of setPreferredWidth is to compare the value being passed with the current minimum and maximum widths; the call will set the preferred with to be *within* the limits specified by the minimum and maximum widths. So one way to get around your problem could be to set the minimum widths to smaller values (perhaps even zero), and to then set the preferred widths.
i.e., something like:

//Set the minimum widths first
myTable.setMinimumWidth(new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});

//Then set the preferred column widths
myTable.setPreferredColumnWidth(new int[] {12, 10, 10, 10,170, 10, 50, 130, 10, 70, 10});

Hope this helps :-)

Author Comment

ID: 8078086
Hi, seriuos_sam.

I have already solved the problem and I thought I'd had placed a comment here. :-(

I simply put the statements in setPreferredWidth within a SwingUtilities.invokeLater... and it works finde now.

Expert Comment

ID: 8078165
That's cool, dirk - great to hear that your problem is solved :-)
LVL 35

Expert Comment

ID: 8078497
 dirku can you please ask a moderator to PAQ the question and rfund the points back to you?

  Just go here: http://www.experts-exchange.com/Community_Support/ and ask a zero poitns question.

Accepted Solution

SpideyMod earned 0 total points
ID: 8083626
PAQ'd and points refunded

Community Support Moderator @Experts Exchange

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question