?
Solved

Resizing a table's columns

Posted on 2003-02-20
10
Medium Priority
?
249 Views
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
System.err.println();

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

How can I prevent from such a behaviour?

Using this:
myTable.setAutoResizeMode(-1);
or
myTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

do nothing.

Hope anybody can help me very soon.

Thank you in advance,

Dirk
Berlin, Germany
dirk.ulrich@gmx.de
0
Comment
Question by:dirku
[X]
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
10 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 7986444
 Can you try using:

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

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

Author Comment

by:dirku
ID: 8016207
Hi, girionis.

This has no effect.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8016251
 Weird... that should do. Can you post a sample compilable example?
0
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

by:dirku
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.
0
 
LVL 35

Expert Comment

by:girionis
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?
0
 

Expert Comment

by:serious_sam
ID: 8071024
Dirk,
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 :-)
Sam
0
 

Author Comment

by:dirku
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.
0
 

Expert Comment

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

Expert Comment

by:girionis
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.
0
 

Accepted Solution

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

SpideyMod
Community Support Moderator @Experts Exchange
0

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