JTable Column Resizing problem

Hey all.

I have a JTable inside a JScrollPane inside a JDialog. It's a simple table (three labels and two check boxes) that lets the user hide, show or disable any JComponent on the screen. However, I want the user to be able to resize the columns. I have the table's auto resize mode set to AUTO_RESIZE_SUBSEQUENT_COLUMNS. However, when the user resizes a column, the subsequent columns don't resize, they just keep going to the right past the edge of the view. That is, until the user releases the mouse and then clicks anywhere on the table, at which point all the columns snap right back to the way they were to begin with. I am extending AbstractTableModel to provide a specific model for the table, but am not changing any of the resize or paint methods. Any ideas?

For the record, here is the code for the table model (slightly cleaned up):

class MyTableModelextends AbstractTableModel

{

List objects;

private List visible;

private List enabled;

public JASTableModel(List l)

{

super();

objects = l;

visible =new ArrayList();

enabled =new ArrayList();

for (int i = 0; i < l.size(); i++)

{

Boolean isVisible =new Boolean(false);

Boolean isEnabled =new Boolean(false);

try{

JComponent component = (JComponent)l.get(i);

if (component.isVisible())

isVisible =new Boolean(true);

if (component.isEnabled())

isEnabled =new Boolean(true);

}

catch(ClassCastException e){}//leave unchecked

visible.add(isVisible);

enabled.add(isEnabled);

}

}

publicint getColumnCount(){

return 5;

}

publicint getRowCount(){

return objects.size();

}

public Class getColumnClass(int c){

return getValueAt(0, c).getClass();

}

public Object getValueAt(int row,int col)

{

Object o = objects.get(row);

if (col == 0)

{

//return object id...

}

elseif (col == 1)

{

//return string label...

}

elseif (col == 2)

return visible.get(row);

elseif (col == 3)

return enabled.get(row);

else//col == 4

{

//return string details...

}

}

publicboolean isCellEditable(int row,int col)

{

if (col == 2 || col == 3)returntrue;

returnfalse;

}

publicvoid setValueAt(Object value,int row,int col){

// get the array for the proper row

if (col == 2)

visible.set(row, value);

elseif (col == 3)

enabled.set(row, value);

return;

}

public JComponent getComponent(int i)

{

Object o = objects.get(i);

if (oinstanceof JComponent)return (JComponent)o;

returnnull;

}

}

[6062 byte] By [Cidolfas2a] at [2007-11-27 7:05:34]
# 1
This isn't really to do with your TableModel, but is more likely to be related to your JTable itself.Can you post the code that constructs and places the JTable on a panel/frame etc?For instance, have you put the JTable on a JScrollPane?
moschopsa at 2007-7-12 18:56:48 > top of Java-index,Desktop,Core GUI APIs...
# 2

Sure. Thanks!

public class ObjectHideDialog extends JDialog

{

public ObjectHideDialog()

{

setModal(true);

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

Container contentPane = getContentPane();

contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

table = new JTable(new MyTableModel());

table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);

TableColumnModel model = table.getColumnModel();

TableColumn col = model.getColumn(0);

col.setHeaderValue("Object ID");

col.setResizable(true);

col = model.getColumn(1);

col.setResizable(true);

col.setHeaderValue("Type");

col = model.getColumn(2);

col.setResizable(true);

col.setHeaderValue("Show?");

col = model.getColumn(3);

col.setResizable(true);

col.setHeaderValue("Enabled?");

col = model.getColumn(4);

col.setResizable(true);

col.setHeaderValue("Details");

scrollPane.setPreferredSize(new Dimension(300, 600));

contentPane.add(scrollPane);

contentPane.add(Box.createVerticalStrut(10));

JButton btnOK = new JButton("OK");

btnOK.addActionListener(this);

JButton btnCancel = new JButton("Cancel");

btnCancel.addActionListener(this);

Box hbox = Box.createHorizontalBox();

hbox.add(btnOK);

hbox.add(btnCancel);

contentPane.add(hbox);

setSize(new Dimension(600, 700));

}

Cidolfas2a at 2007-7-12 18:56:48 > top of Java-index,Desktop,Core GUI APIs...