Trouble dynamically resizing JTable
Hi all,
I am having trouble resizing JTable whenever the JFrame containing it is resized.
The frame is using BoxLayout ( Y_AXIS ) and I already added a componentResized event to the frame:
frame.addComponentListener(new ComponentAdapter()
{
publicvoid componentResized ( ComponentEvent e )
{
int someStrangeModifier = 20;
int newHeight= frame.getPreferredSize().height - someStrangeModifier;
int newWidth= frame.getPreferredSize().width - someStrangeModifier;
table.setPreferredSize(new Dimension( newHeight, newWidth ) );
tabulka.invalidate();
tabulka.validate();
tabulka.repaint();
}
} );
Now the strange thing is, that it sometimes work and sometimes it does not. I checked it, and all I found out is, that this event is allways called, preferredSize is set right however GUI does nothing. It stays the way it was.
I also already tried many methods like revalidate() repaint() paint() etc. not only on table but also on frame.
I even tried setVisible(false); setVisible(true); ( yeah, I know that was silly, but I was really desperate )
What I really can't understand is why sometimes it does resize and sometimes it does not.
So please, if somebody knew what to do, it would be much appretiated.
[1665 byte] By [
Babantika] at [2007-11-27 5:35:48]

# 1
I think that BoxLayout respects the minimum and maximum sizes of the component. So maybe these need to be reset along with the preferred size.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.
# 2
> I think that BoxLayout respects the minimum and
> maximum sizes of the component. So maybe these need
> to be reset along with the preferred size.
Sadly that didn't help. But thanks anyway.
I made a slight modification to the code. I used JScrollPane and put that JTable in it.
When I set AutoResizeMode( JTable.AUTO_RESIZE_OFF ); and kept using the componentResized() method, I had the same problems. While ScrollPane adapted to the frame, table sometimes did and sometimes did not.
However, when I left the autoResizeMode to default, interesting thing happened. Whenever I resized the window in both directions at once, table adapted to the new size. When I resized it just vertically, usually nothing happened.
I take it that whenever JTable's auto_resize happens, it resizes height of the table as well, but otherwise it is back to sometimes yes sometimes no.
New code: public class TableInFrame extends JFrame
{
private JScrollPane scrollPane;
private JTable table;
private DefaultTableModel tblModel;
public TableInFrame()
{
this.setTitle("JTable in JScrollPane in JFrame");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tblModel = new DefaultTableModel();
table= new JTable( tblModel );
tblModel.addColumn("Column 1");
tblModel.addColumn("Column 2");
tblModel.addRow( new String[]{"foo","bar"} );
table.setTableHeader(null);
// table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
scrollPane = new JScrollPane( table, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
this.add( scrollPane );
}
addComponentListener( new ComponentAdapter()
{
public void componentResized ( ComponentEvent e )
{
int newHeight = scrollPane.getPreferredSize().height;
int newWidth = scrollPane.getPreferredSize().width - 18; // -18px for vertical slidebar
table.setPreferredSize( new Dimension( newHeight, newWidth ) );
table.invalidate();
table.validate();
table.repaint();
}
} );
}
So now I am happy that it at least works partially, but I'm more and more curious why does it behave like this.
# 3
That is not a SSCCE, so I can't help you.
# 4
Argh, of course, my fault. Here it is corrected, and including imports and main.
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;
public class TableInFrame extends JFrame
{
private JScrollPane scrollPane;
private JTable table;
private DefaultTableModel tblModel;
public TableInFrame()
{
this.setTitle("JTable in JScrollPane in JFrame");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tblModel = new DefaultTableModel();
table= new JTable( tblModel );
tblModel.addColumn("Column 1");
tblModel.addColumn("Column 2");
tblModel.addRow( new String[]{"foo","bar"} );
table.setTableHeader(null);
// table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
scrollPane = new JScrollPane( table, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
this.add( scrollPane );
addComponentListener( new ComponentAdapter()
{
public void componentResized ( ComponentEvent e )
{
int newHeight = scrollPane.getSize().height;
int newWidth = scrollPane.getSize().width - 18; // -18px for vertical slidebar
table.setPreferredSize( new Dimension( newHeight, newWidth ) );
// table.setMinimumSize( new Dimension( newHeight, newWidth ) );
// table.setMaximumSize( new Dimension( newHeight, newWidth ) );
table.invalidate();
table.validate();
table.repaint();
}
} );
this.pack();
this.setVisible(true);
}
public static void main(String[] args)
{
new TableInFrame();
}
}
Thanks for your interest.
