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 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" Watch "JTable" New topic
Author

JTable

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
is there a way i could set/fix the column widths for a Jtable when it displays....
is there anyway to make it uneditable....
Glen Tanner
Ranch Hand

Joined: Apr 16, 1999
Posts: 147

I found the easiest way to make a JTable non-editable is to extend the DefaultTableModel like this:
<CODE>
package util;
import javax.swing.*;
import javax.swing.table.*;
// No edit table model
public class NoEditTableModel extends DefaultTableModel
{
public NoEditTableModel(java.lang.Object[] colNames, int numRows)
{
super(colNames, numRows);
}
public NoEditTableModel(java.util.Vector data, java.util.Vector columnNames)
{
super(data, columnNames);
}
public NoEditTableModel(Object[][] data, Object[] columnNames) {
super(data, columnNames);
}
public boolean isCellEditable(int row, int col)
{
return false;
}
}
</CODE>
*****
To set column sizes, set them in the TableColumnModel:
<CODE>
TableColumnModel colModel = yourTbl.getColumnModel();
// set width of columns in table
colModel.getColumn(0).setPreferredWidth(100);
colModel.getColumn(1).setPreferredWidth(100);
colModel.getColumn(2).setPreferredWidth(150);
colModel.getColumn(3).setPreferredWidth(100);
colModel.getColumn(4).setPreferredWidth(175);
colModel.getColumn(5).setPreferredWidth(200);
</CODE>
Hope this will help!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Yeah, This is the best way to do so!!!
Steve Tharp
Greenhorn

Joined: Mar 19, 2001
Posts: 10
I tried this but I keep getting an ArrayIndexOutOfBoundsException
Any idea why?
ron erwin
Greenhorn

Joined: Mar 26, 2001
Posts: 2
Okay I like the example listed above two points,
1. can we default the text font size
2. can we set the Column class at this same place?
-- and also set the ColumnName?
I know we can create the getColumnName getColumnClass methods... it would seem more strightforward to just set them here....
Thanks!
Ron Erwin
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JTable