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 / November 2004

Tip: Looking for answers? Try searching our database.

under what condition "setViewPosition" may not work?

Thread view: 
Kaidi - 20 Nov 2004 05:03 GMT
Hello guys,
I have a rather strange question about setViewPosition (in
JScrollPane), it seems not working sometimes under certain conditions,
but I can not find out the reason. My program is doing something like:

There is a:
 OperateVisPanelDrawBoard jPanelDrawBoard = new
OperateVisPanelDrawBoard();
 The OperateVisPanelDrawBoard is "extends JPanel", with only two
functions, one is paintComponent(Graphics g) where I draw out some
dots and lines on the JPanel, one is getPreferredSize() where it
return the preferredSize (the boundary of those dots and lines drawn).

And there is a:
 JScrollPane jScrollPaneDraw =
     new JScrollPane(jPanelDrawBoard,
                     ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                   
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
 jScrollPaneDraw.setAutoscrolls(true);

The question is in a piece of code like:
   // when user clicks the mouse, it draws out some dots and lines
and my code udpates the jPanelDrawBoard, and the scrool pane(bars)'s
view is also updated by calling the setViewPosition.
   .....
   Point nodePos=new .........;
   System.out.println("View port pos: "+nodePos.toString());    
   jScrollPaneDraw.getViewport().setViewPosition(nodePos);
   Point p2=jScrollPaneDraw.getViewport().getViewPosition();
   System.out.println("View port pos: "+p2.toString());
   System.out.println();
   ......
I suppose the two println above should return same value. But
sometimes the values are different! It seems the setViewPosition does
not work sometimes.
Some output I get may look like:

View port pos: java.awt.Point[x=658,y=576]
View port pos: java.awt.Point[x=658,y=576]

View port pos: java.awt.Point[x=1034,y=1540]
View port pos: java.awt.Point[x=1034,y=1244]

View port pos: java.awt.Point[x=2402,y=3478]
View port pos: java.awt.Point[x=2402,y=2244]

See the last two ones, the y values are different. :-(

Any idea of why this happen? Under what conditions will
setViewPosition refuse to work?
PS: this part of code is in single (main) thread.

Thanks a lot and nice weekend. :-)
John McGrath - 20 Nov 2004 06:30 GMT
> It seems the setViewPosition does not work sometimes.
> Some output I get may look like:

It works, but then it adjusts the position afterwards.  If the view is not
smaller than the viewport, then the viewport will make sure that as much
as possible of the view component is showing.

If the view component is 100 pixels tall, the viewport is 70 pixels tall,
and you set the view Y position to 40, that would leave only 60 pixels of
your view component to show in the 70 pixel viewport.  The viewport would
shift the view down by 10, so the resulting view position would be 30
instead of 40 and you would have a full 70 pixels of the view showing.

Signature

Regards,

John McGrath

Kaidi - 21 Nov 2004 22:45 GMT
> > It seems the setViewPosition does not work sometimes.
> > Some output I get may look like:
[quoted text clipped - 8 lines]
> shift the view down by 10, so the resulting view position would be 30
> instead of 40 and you would have a full 70 pixels of the view showing.

Thanks a lot.
I found out the reason: the
jScrollPaneDraw.getViewport().getViewSize() does not reflect the
correct (and always much smaller) size I want. I suppose in my
example, the jScrollPaneDraw will set its size based (mainly) on the
value from jScrollPaneDraw's getPreferredSize(), but it seems not true
(not even close to that, sometimes may differ 1000+ pixels). Any idea
of it?

Thanks.
John McGrath - 22 Nov 2004 01:13 GMT
> the jScrollPaneDraw.getViewport().getViewSize() does not reflect the
> correct (and always much smaller) size I want.

Well I guess the question is, what size do you want, and is it what the
JViewport is supposed to return?  It is pretty hard to tell based on the
short excerpt of code you provided.  If you would post a small, runnable
example of the problem, it would make it much easier to see what is
happening.

> I suppose in my example, the jScrollPaneDraw will set its size based
> (mainly) on the value from jScrollPaneDraw's getPreferredSize(), but
> it seems not true (not even close to that, sometimes may differ 1000+
> pixels). Any idea of it?

The jScrollPaneDraw component (this generally applies to all components)
does not set its own size.  That is normally done by the container that
contains the component, which in turn usually delegates that task to its
Layoutmanager.  The LayoutManager may take into account the preferred size
of the child component, but that is not guaranteed.  Some LayoutManagers
will always respect the child's preferred size and some will ignore it
completely.

Signature

Regards,

John McGrath



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.