Link to home
Start Free TrialLog in
Avatar of dirku
dirkuFlag for Germany

asked on

Resizing a table's columns

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
Avatar of girionis
girionis
Flag of Greece image

 Can you try using:

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

  ... and so on, and let us know what happens?
Avatar of dirku

ASKER

Hi, girionis.

This has no effect.
 Weird... that should do. Can you post a sample compilable example?
Avatar of dirku

ASKER

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.
 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?
Avatar of serious_sam
serious_sam

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
Avatar of dirku

ASKER

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.
That's cool, dirk - great to hear that your problem is solved :-)
 dirku can you please ask a moderator to PAQ the question and rfund the points back to you?

  Just go here: https://www.experts-exchange.com/Community_Support/ and ask a zero poitns question.
ASKER CERTIFIED SOLUTION
Avatar of SpideyMod
SpideyMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial