Link to home
Start Free TrialLog in
Avatar of bigjim2000
bigjim2000

asked on

Graphics Object & Panel Sizing

Hey Everyone,

I need to draw on a panel  that is larger than its display area, and have the ability to scroll the panel and view everything that I have drawn.

Java is not my preferred language, but I must use it for this case.  If someone would write me a short example, perhaps make a scrolled panel that is 2x the size of the applet, and simply draw a square on it to demonstrate the ability to scroll about the whole image, that would be wonderful.

Thank you in advance,

-Eric
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

We can't produce code to order, but are here to help you with code you have already produced yourself. These are the rules of this site
Just use

new JScrollPane( yourPanel );
- You give you applet/GUI app a BorderLayout.
- You place
                 new JScrollPane( yourPanel );
   in the center of it
- yourPanel is the JPanel containing more then the display area can show
Avatar of bigjim2000
bigjim2000

ASKER

Pardon me.  I didn't know I couldn't ask for code.  Here is what I have so far.  Keep in mind, the panel will be resizing to fit whatever contents I need to display, and here is the code that I am using:

this.pnl.setSize( new Dimension( preferredWidth, this.pnl.getHeight()));
this.pnl.setPreferredSize( new Dimension( preferredWidth, this.pnl.getHeight()));
this.pnl.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
this.pnl.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
this.pnl.revalidate();

With this code, the panel resizes successfully, but the scroll bars do not work.

Thanks again,

-Eric
It's the panel whose size you should set and the scrollpane whose bars you should set
if you implement Scrollable in your JPanel, then you can control how big the steps are for the scrollbars in the parent JScrollpane :-)
I'm sorry again.  this.pnl refers to a JScrollPane  (force of habit... I declare all panel-like objects with a "pnl" prefix)

-Eric
you want to setSize and PreferredSize on the panel, and the rest on the scrollpane
TimYates,

Right now I have several panels on my Applet as part of a GUI.  WOuld I need to encapsulate the JScrolledPane inside its own JPanel?

-Eric
>>you want to setSize and PreferredSize on the panel, and the rest on the scrollpane

You could have a base panel, yes
LOL - wrong quote sorry!

>>WOuld I need to encapsulate the JScrolledPane inside its own JPanel?

You could have a base panel, yes, on which you placed other scroll panes
OK,  I'll try resizing a JPanel, and not the JScrolledPane.

I'll let you know how that works.

Thanks guys (and gals, as the case may be)

-Eric
OK, so I put the JScrolledPane inside a JPanel, then resized my JPanel instead.  2 things happened:

1)  The scroll bars still do not function (they are there, but do not do anything when clicked on)

2)  The JScrolledPanel does not appear to be resizing unless I implicitly call setBounds on it (Even though it is BorderLayout.CENTER, and I resize the parent)

-Eric
If someone could just tell me how to get the scrollbars working, I'd really appreciate it.  The panel resizes right, and the drawing is corrent, I just can't scroll.

Thanks,

-Eric
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Set the preferred size of your panel and add it to your scroll pane

panel.setPreferredSize(new Dimension(w, h));
JScrollPane pane = new JScrollPane(panel);

then add your scroll pane to your component hierarchy.
Thank you CEHJ,

That example helped me get my code working.

-Eric
8-)