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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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?
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more


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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

770 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