• 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

JTable

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a way i could set/fix the column widths for a Jtable when it displays....
is there anyway to make it uneditable....
 
Ranch Hand
Posts: 147
Python Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, This is the best way to do so!!!
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this but I keep getting an ArrayIndexOutOfBoundsException
Any idea why?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic