Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2003

Tip: Looking for answers? Try searching our database.

JTable in a JScrollPane (but not the usual problem :-)

Thread view: 
Joseph Knapka - 23 Aug 2003 07:29 GMT
Hi folks,

First post to this NG, so please be nice :-) The O'Reilly
"Swing" book, the API docs, and various FAQs haven't helped
me with this issue; perhaps someone here can.

I understand that in order to make my JTable behave properly
inside a JScrollPane, I have to set the auto-resize mode
to AUTO_RESIZE_OFF. That works fine, wrt column resizing
by the user. It does, however, cause a problem: when the
JTable is first displayed, all of the columns are at their
preferred widths, so there's a lot of empty space in the
JScrollPane in many cases, and the user is *forced* to
manually resize a bunch of columns just to get a readable
presentation.

The behavior I'd like to see is that when first displayed,
the JTable distributes the columns evenly across the
visible width of the JScrollPane. Then, when the user
subsequently resizes the columns, I want that to also
work as expected (showing the scrollbars when things get
too wide, etc). I tried initially adding the JTable
to the JScrollPane with auto-column-resizing enabled,
then subsequently disabling it, but that didn't
work (it acted as if autoresize was just disabled).

Is there a simple way to get the behavior I want? Is
there some well-known (to others) documentation that I
should have looked at before posting this? Pointers
to either would be greatly appreciated.

Many thanks,

-- Joe

Signature

stdDisclaimer :: Lawsuit -> ()
stdDisclaimer = "These are my opinions etc., not TransCore's"
Joseph Knapka / Systems engineer / TransCore ITS Inc.
joseph.knapka@transcore.com / (915)549-5098

Christian Kaufhold - 23 Aug 2003 12:14 GMT
Hello!

> I understand that in order to make my JTable behave properly
> inside a JScrollPane, I have to set the auto-resize mode
[quoted text clipped - 15 lines]
> then subsequently disabling it, but that didn't
> work (it acted as if autoresize was just disabled).

I am not sure about the notion of "first displayed".

Try this in JTable subclass:

           private boolean firstDisplayed = true;

           public boolean getScrollableTracksViewportWidth()
           {
               return firstDisplayed && getAutoResizeMode() == AUTO_RESIZE_OFF
                   ? getPreferredSize().width < getParent().getWidth()
                   : super.getScrollableTracksViewportWidth();
           }

           public void doLayout()
           {
               super.doLayout();

               if (firstDisplayed && getWidth() > 0)
               {
                   firstDisplayed = false;

                   for (Enumeration i = getColumnModel().getColumns(); i.hasMoreElements();)
                   {
                       TableColumn c = (TableColumn)i.nextElement();

                       c.setPreferredWidth(c.getWidth());
                   }

                   revalidate();
               }
           }

getScrollableXXX makes the table initially become as wide as the viewport.
doLayout then updates the once-calculated preferred widths for that size.

I don't like it.

Maybe the real problem is that the JTable initially shows up at an impro-
per size (450 pixels wide) unrelated to its contents.
-> setPreferredScrollableViewportSize.

Christian


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.