(J)ScrollPane trouble : I want to have scroll bars while the size of image get too large

Hello every one
I just wrote a program to excercise use of some GUI components and Graphics context...
here is the SSCCE :

and the result :

Although there is no problem with horizontal scrollbar(if i reduce the width of window)
but for vertical one if i reduce the height of window,scroll bar does'nt appear untill the height becomes as small as just a little of image remains,and then appears,that is it cant find out the height of panel "canvas" and sets its default size to a small size(I think)

by adding this line in constructor the result will be :

as you see this is somehow better,but still has problem : i want to have the image fully , in this picture the scale is set to 160%

can anyone offer a solvation

thank you for any help

Esmaeil Ashrafi

I'm really tired of being engaged with stuff other than Java and programming

That is not a SSCCE, please reread the information found on the SSCCE link.

General comments:

a) Read the section from the Swing tutorial on "Custom Painting". You should NOT be using a Canvas in a Swing application. Use a JPanel and override the paintComponent() method.

b) Scroll bars appear automatically when the preferred size of the panel is greater than the size of the scroll pane. If you are doing free hand drawing then you need to continually recalculate the preferred size as the shapes move outside the bounds of the panel. How you do this is up to you since we don't know how your actual application works.

Esmaeil Ashrafi
Ranch Hand

Joined: Feb 22, 2010
Posts: 73

posted Feb 23, 2010 09:57:22

0

Rob Camick wrote:

here is the SSCCE :

That is not a SSCCE, please reread the information found on the SSCCE link.

General comments:

a) Read the section from the Swing tutorial on "Custom Painting". You should NOT be using a Canvas in a Swing application. Use a JPanel and override the paintComponent() method.

b) Scroll bars appear automatically when the preferred size of the panel is greater than the size of the scroll pane. If you are doing free hand drawing then you need to continually recalculate the preferred size as the shapes move outside the bounds of the panel. How you do this is up to you since we don't know how your actual application works.

I will appreciate if you link the SSCCE(i just learned this term here ),and what should i call that segment of code ?

@ a)
Please read the comments in constructor in front of calss "Canvas" i wrote,this is not that exists in awt,just has the same name !

@ b)
Actually i want to set the preffered size of panel "canvas" to some value that if the dimensions of drawing image got bigger than it,scroll bars act on the panel "canvas" !
I know,the ScollPane is currently set to wrap the panel "main",but i tried to add scrollpnae to "canvas" :

but it affected more aweful : even the canvas of drawing image not shown,and/or ( by having or not having the line , the panel "controlPanel" not shown

if you care to find out exactly what is the problem,i tried to attach the source codes,but this system doesnt accept .zip and .rar files,i dont know,waht does it accept !?

thank you

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2261

8

posted Feb 23, 2010 10:22:03

0

I will appreciate if you link the SSCCE

The link was given in the last posting where you first heard the term. Or you can search the web.

i wrote,this is not that exists in awt,just has the same name !

Well, use a proper name and don't use class names that already exist in the JDK. It helps reduce confusion

Actually i want to set the preffered size of panel "canvas" to some value that if the dimensions of drawing image got bigger than it,scroll bars act on the panel "canvas"

I know. I said this is your responsibility. We can't give you the code. Your program uses a MouseListener or a JButton or something to add the images to your canvas. So you need to change this code to automatically update the preferred size of the drawing canvas when the images you add extend beyond the current dimension of the canvas.

Esmaeil Ashrafi
Ranch Hand

Joined: Feb 22, 2010
Posts: 73

posted Feb 23, 2010 13:51:02

0

well i made the changes ( actually add another method in my frame class to calculate the desired dimension relevant to size of the image is being drawing and return this new dimension to pass through setPrefferedSize() method of my canvas panel ,and i'm sure it calculates the proper size,because,i add a "System.out.println(newCalculatedPrefferedSize)" to that method to track its behaviour )

Also i advanced one step forward by understanding what you told

But,still a bit of problem : i placed "setPrefferesSize()" method of my canvas and the call to that method ( calculates and returns my desired dimension for canvas panel ) in the actionPerformed() method,so when i press the only button registered with ActionListener,it does everything expected ( set needed variables,call the new added method,then "canvas.setPrefferedSize()",and finally calls "canvas.repaint()" ),but scrollbars doesnt appear until i resize the windoow ( although just a bit )

I tried something but didnt succeed

would you please help me to next step

Thanks

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2261

8

posted Feb 23, 2010 14:08:23

0

I already gave you your "next" step. You still haven't posted a SSCCE.

a) create a frame
b) add a scrollpane containing the panel to the frame in the center
c) add a button to the south
d) add an Actionlistener to the button. Every time you click the button you increase the preferred size of the panel by 100 and then invoke panel.revalidate(). the scrollbars will change every time.

So now that you've proven the simple concept works you figure out what your real program is doing differently.

Nah, delete all this as most of your code is unrelated to the question at hand, and also your code will compile for no-one (no one has all the classes).

I just wanted to avoid posting too many lines of code and bother you

Then take my advice: delete your code above, read the link on SSCCE, and post a real SSCCE, else you risk wasting a volunteer's time, and that's not good.

Esmaeil Ashrafi
Ranch Hand

Joined: Feb 22, 2010
Posts: 73

posted Feb 23, 2010 14:53:43

0

just please read the end of my previous post "Dear Volunteer"

by the way i think the segment of ocdes i put in first post,was the SSCCE,why?because it covers the segments related to question,dont you think!?all codes related to the problem was in "constructor" and then in "actionPerformed()" after you helped

and also your code will compile for no-one (no one has all the classes).

and i tried to attach the package as an archive type of zip or rar and the system avoided

by the way i think the segment of ocdes i put in first post,was the SSCCE,why?because it covers the segments related to question,dont you think!?all codes related to the problem was in "constructor" and then in "actionPerformed()" after you helped

Nope, not even close. A "real" SSCCE is a program that will compile and run as-is on our machines and will thus allow us to see and experience your problem first hand. That code snippet is small, which is good, but isn't a self-contained program, so it isn't even close to being an SSCCE.

by the way,thank you very much
Esmaeil

Very welcome!

subject: (J)ScrollPane trouble : I want to have scroll bars while the size of image get too large