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
Edward Smith
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |