• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

JTable column width problem..

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic