JTable and JScrollPane

Karamat
Karamat used Ask the Experts™
on
Hi,
   I am having a bit of a problem using JScrollPane. following is my code

...
CreateColumns();
CreateData();

mainTable = new JTable(dataValues, columnNames );          
scrollPane      = new JScrollPane(mainTable);
scrollPane.setHorizontalScrollBarPolicy(
                    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
The problem I have is that all even though I get the Horizontal Scroll Bar, but my columns are not autosized. Meing it tries to fit the columns in teh table and thus squeezes all of them. The Left and Right arrows on the Horizontal bar are there, but they dont do a thing. If I resize my columns it doesnt allow me to scroll the horizontal bar.
Any sugeestions?
Thanks
Adil
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
have u used

mainTable.setPreferredScrollableViewportSize(new Dimension(height, width));

replace height and width with numbers to your preference

this will help set the height and width of the scrollpane

if that doesn't work try
mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS)

hth
vemul
Commented:
Your table columns are being auto-resized to fit within the viewport. Call the following to turn auto-resize off.

mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

Author

Commented:
Thanks Yougsing,
   That works.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial