views:

1518

answers:

4

Hi all,

I am struggling ( ! ) with java GUI development. All these small things that make obvious sense just don't work. I'll explain what I am trying to do and where I am failing (this is quite frustrating):

I've got this graphics2d object which resides inside a panel. Now this graphics draws an image, it can go on and draw forever. this drawing expands horrioncally and since I do not know what size to expect I put it inside a scrollpane. Now I want to resize the JPanel in each pain iteration and set the setPreferredSize to a larger value, thus expanding the scrollpane's knob.

The problem: From some reason when I call setPreferredSize from paint it doesn't do anything. I call it on the panel. When I try calling setPreferredSize from the function that calls paint, it performs the resizing operation only once!

What could be the reason for that?

Apart from that I've got another small question, I'd love to solve: Since the graphics draw goes on and increases in size in time, how can I advance the position of scrollpanes? Gradually increasing it?

People, thank you very much. I sure need help here

+1  A: 

Sounds to me like you got a couple of things wrong.

  • Setting any size attribute (actuallly any attribute of a component at all) inside the paint method will result in possibly strange behavior, because you don't really know when and how often paint gets called

  • You normally do not call paint your self. It will get called by Swing itself, once the component becomes visible

  • If I got it right you want to have a Component, that grows with time. IMHO the correct approach would be to create a Timer. Using the Timer change the size as required. In the paint method just read this information and use it to decide what to paint.

Jens Schauder
well, I already have a timer that calls the repaint method every once in a while. I tried setting the size of the component myself and I noticed this strange thing: When the scrollbar is moved to the far end (as if I am going to scroll onwards) the JPanel suddenly resizes. Any ideas why? And how can I get the scroll pane to advance on it own?
vondip
Sounds like you need to inform the upstream components (JScrollpane) about the change. If I got it right, invalidate() is the right thing to do, but I'm not completely sure
Jens Schauder
+1  A: 

preffered size is, by its name, not mandatory... maybe pack() or revalidate() would help...

ante.sabo
wow, revalidate really did the trick :O thank you 'as'. Its like looking for a needle in a haystack. Do you know how to advance the scroll bar though?
vondip
Why are you asking mutliple question on this topic. You'be been given the anser in your other postings, yet you never bother to mark the questions as accepted. Of course you should never call setPreferredSize from the paintComponent() method. And you should not call revalidate from that method either. It you would all your questions together then everybody would know what has been suggested and we wouldn't waste time making the same suggestions. In the future post a SSCCE with your question. If you don't know what a SSCCE is then search the web.
camickr
@unknown: try scrolling element in separate thread, maybe you have scenario in which you set it somewhere while still validation process is not finished, and that process set scroller back to 0. Not sure that would help..
ante.sabo
Thank you as, I've used the following line to set the scrollbar's position:scrollPane.getViewport().setViewPosition(new java.awt.Point(0, 0));it works very well, and your revalidate proves to be my solution. Thank you
vondip
+1  A: 

setPreferredSize is one of many hints you can give to the LayoutManager which is managing the layout of your component's container if indeed a LayoutManager has been set. It is up to the given LayoutManager what it does with those hints.

I suggest you check out the book Filthy Rich Clients for an in-depth explanation of how the Swing component rendering works.

Martin OConnor
+1  A: 

You should note that set(Preferred/Minimal/Maximum)Size() are nothing more than hints to the parent component and its layoutmanager how much space the component should get. Further note that theses sizes are only interpreted when the components are layouted (upon first render, invalidate() and so on).

ZeissS