posted 23 years ago
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!