File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTable column width problem.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable column width problem.." Watch "JTable column width problem.." New topic
Author

JTable column width problem..

Antony Patric
Greenhorn

Joined: Apr 24, 2008
Posts: 7
Hi all,

I have a JTable with a few columns and I need to set specific widths for each of the columns.I did that using setMaxWidth,setMinWidth and setPreferredWidth.

Actually the specific portion id invoked when a button is clicked.The first time the code works fine and the columns are displayed in the correct widths.But when I click the button again the widths are changed and all the columns take equal spacing and occupy the space of the scrollpane.

If anybody knows the problem please help me...

I am posting the portion of code below..

public void displayhistory() {
TableColumn column=null;
colshist=new Vector();
rowshist=new Vector();
String colnames[]={"Prescription ID","Medicine ID","Prescription Date","Prescribed By","Dose","Frequency","Duration","Medication Time","Notes"};
int colwidths[]={105,75,100,150,80,85,90,85,200};
TableColumnModel colmodel=null;
for(int i=0;i<colnames.length;i++) {
colshist.addElement(colnames[i]);
}

if(flag2==0)
{
tabhistmodel= new DefaultTableModel();
flag2++;
}

tabhistmodel.setDataVector(rowshist,colshist);
populaterowshist();
tablehistory=new JTable(tabhistmodel);
int height=tablehistory.getRowHeight();
tablehistory.setRowHeight(height+5);


colmodel=tablehistory.getColumnModel();
for(int i=0;i<colmodel.getColumnCount();i++) {

System.out.println("inside column width");
column=colmodel.getColumn(i);

column.setMaxWidth(colwidths[i]);
column.setMinWidth(colwidths[i]);
column.setPreferredWidth(colwidths[i]);
System.out.println(column.getMaxWidth()+" maxwidth");
System.out.println(column.getMinWidth()+" minwidth");

}


tablehistory.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);

tablehistory.getTableHeader().setReorderingAllowed(false);
tablehistory.getTableHeader().setResizingAllowed(false);

jspanehist = new JScrollPane(tablehistory);
}



Regards,
Antony
Ted Smyth
Ranch Hand

Joined: May 28, 2008
Posts: 73
Originally posted by Antony Patric:
Hi all,

I have a JTable with a few columns and I need to set specific widths for each of the columns.I did that using setMaxWidth,setMinWidth and setPreferredWidth.

Actually the specific portion id invoked when a button is clicked.The first time the code works fine and the columns are displayed in the correct widths.But when I click the button again the widths are changed and all the columns take equal spacing and occupy the space of the scrollpane.

If anybody knows the problem please help me...

I am posting the portion of code below..

public void displayhistory() {
TableColumn column=null;
colshist=new Vector();
rowshist=new Vector();
String colnames[]={"Prescription ID","Medicine ID","Prescription Date","Prescribed By","Dose","Frequency","Duration","Medication Time","Notes"};
int colwidths[]={105,75,100,150,80,85,90,85,200};
TableColumnModel colmodel=null;
for(int i=0;i<colnames.length;i++) {
colshist.addElement(colnames[i]);
}

if(flag2==0)
{
tabhistmodel= new DefaultTableModel();
flag2++;
}

tabhistmodel.setDataVector(rowshist,colshist);
populaterowshist();
tablehistory=new JTable(tabhistmodel);
int height=tablehistory.getRowHeight();
tablehistory.setRowHeight(height+5);


colmodel=tablehistory.getColumnModel();
for(int i=0;i<colmodel.getColumnCount();i++) {

System.out.println("inside column width");
column=colmodel.getColumn(i);

column.setMaxWidth(colwidths[i]);
column.setMinWidth(colwidths[i]);
column.setPreferredWidth(colwidths[i]);
System.out.println(column.getMaxWidth()+" maxwidth");
System.out.println(column.getMinWidth()+" minwidth");

}


tablehistory.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);

tablehistory.getTableHeader().setReorderingAllowed(false);
tablehistory.getTableHeader().setResizingAllowed(false);

jspanehist = new JScrollPane(tablehistory);
}



Regards,
Antony


Please use code tags!

It seems to me that this line is causing the issue:



If you want to the table to never change width, and make use of the scrollpane to manage all the viewing, then set autoResizeMode to JTable.AUTO_RESIZE_OFF.

I typically like to set JTable column sizes relative to a "primary" column, rather than setting all sizes specifically (since component sizes, fonts, etc, can vary by platform / Look & Feel). For example: I might have the 3rd column set to twice the width of the 2nd, and the first column is a fixed width of say 60 pixels.


|60 pixels---|Regular Column---|Long Column----------------|


For that use JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS (which is in fact the default).

[ July 14, 2008: Message edited by: Ted Smyth ]
[ July 14, 2008: Message edited by: Ted Smyth ]

Edward Smith
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable column width problem..