dirku
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.setPreferredColumn Width(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().g etColumnCo unt();
System.err.println("Column s to be initialized: " + c);
for(int i=0; i<c; i++)
{
int w = myTable().getColumnModel() .getColumn (i).getPre ferredWidt h();
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.AUT O_RESIZE_O FF);
do nothing.
Hope anybody can help me very soon.
Thank you in advance,
Dirk
Berlin, Germany
dirk.ulrich@gmx.de
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.setPreferredColumn
In the very next statement I display the set widths on the console this way:
int c = myTable.getColumnModel().g
System.err.println("Column
for(int i=0; i<c; i++)
{
int w = myTable().getColumnModel()
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(
or
myTable.setAutoResizeMode(
do nothing.
Hope anybody can help me very soon.
Thank you in advance,
Dirk
Berlin, Germany
dirk.ulrich@gmx.de
ASKER
Hi, girionis.
This has no effect.
This has no effect.
Weird... that should do. Can you post a sample compilable example?
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.
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?
> 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?
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(ne w int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
//Then set the preferred column widths
myTable.setPreferredColumn Width(new int[] {12, 10, 10, 10,170, 10, 50, 130, 10, 70, 10});
Hope this helps :-)
Sam
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(ne
//Then set the preferred column widths
myTable.setPreferredColumn
Hope this helps :-)
Sam
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.
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
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.
Just go here: https://www.experts-exchange.com/Community_Support/ and ask a zero poitns question.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
TableColumn column = myTable.getColumnModel().g
column.setPreferredWidth(1
... and so on, and let us know what happens?