Setting the width of each column in a JTable
Hi All,
I am just a newbie in java comming from VB. I wanna know if it is possible to set the width of each column in a JTable and how to do that.
Got any sample program that will answer my question? I sorry if I am asking for a sample code since I dont have enough time to find for a sample. I find it hard to understand by just reading and not experimenting on the program.
Please help me with this one.
Thank you and Godbless,
Barako
# 1
Following code may help you:
JTable table = new JTable( new MyTableModel() );
TableColumnModel columns = table.getColumnModel();
TableColumn clm = columns.getColumn(0);
clm.setPreferredWidth(220);
clm = columns.getColumn(1);
clm.setPreferredWidth(40);
clm = columns.getColumn(2);
clm.setPreferredWidth(60);
clm = columns.getColumn(3);
clm.setPreferredWidth(65);
-
public class MyTableModel extends DefaultTableModel{
String[] columnNames = { "Name", "Rol", "Add", "Tel-no" };
publicMyTableModel ()
{
for(int i=0;i<columnNames.length;i++)
addColumn(columnNames);
}
public void removeAllRows()
{
Integer itemCount = getRowCount();
for(int l = 0;l >< itemCount;l++)
this.removeRow(0);
}
public void removeAt(int row)
{
this.removeRow(row);
}
public boolean isCellEditable(int rowIndex, int columnIndex)
{
return false; //for all columns not to be edited
}
}//end of class
# 2
> I am just a newbie in java comming from VB.
Why do you think I continually point you to the Swing tutorial when I answer your questions? Thats because the information for basic questions like these is contained in the tutorial.
> I find it hard to understand by just reading and not experimenting on the program.
Thats again why I point you to the tutorial. You can download the demo code an play around with it.
You've already been pointed to the tutorial on using tables in many of your previous postings. So take the time to actually read the tutorial and quit wasting our time with questions like this that are fully explained in the tutorial.