JTable columns question
Hello
I have a JTable that is changed dinamicaly
If i use table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); then if i have a lot of columns, i will have a scroll . But if i have only 2 columns for example, my table is ugly, i whant that the table to have scrols when i have alot of columns and if i have few columns i whant them to fill my table.
table.setAutoResizeMode(JTable.ALLCOLUMNS); or any other property is not good.
Can anyone help me on this ?
[484 byte] By [
JKodera] at [2007-11-26 22:15:54]

# 3
Maybe this will help:
import java.awt.*;
import javax.swing.*;
public class TableHorizontal extends JFrame
{
public TableHorizontal()
{
JTable table = new JTable(5, 10)
{
public boolean getScrollableTracksViewportWidth()
{
return getPreferredSize().width < getParent().getWidth();
}
};
table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
table.setAutoscrolls(false);
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
table.getColumnModel().getColumn(0).setPreferredWidth(10);
table.getColumnModel().getColumn(1).setPreferredWidth(20);
table.getColumnModel().getColumn(2).setPreferredWidth(30);
table.getColumnModel().getColumn(3).setPreferredWidth(40);
table.getColumnModel().getColumn(4).setPreferredWidth(50);
}
public static void main(String[] args)
{
TableHorizontal frame = new TableHorizontal();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}