aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes to add Scrollbar to Frame in AWT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "to add Scrollbar to Frame in AWT" Watch "to add Scrollbar to Frame in AWT" New topic
Author

to add Scrollbar to Frame in AWT

Jatin sachdev
Ranch Hand

Joined: Jan 21, 2010
Posts: 57
I want a scrollbar in my awt frame.
In my frame i have used paint(graphics g) option
when the painting goes beyond the specified size it is not displaying.I want a scrollbar to see that.

for e.g.

size(300,300)---frame

and we have rectangle starting at x=400 and y=400

to see this rectangle i need a scrollbar
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Why are you using AWT, and not Swing?

Anyway, you can do this by adding a ScrollPane to your frame. Then, add a Canvas to your ScrollPane, to which you draw whatever it is you want to draw.

You should call setPreferredSize() on the Canvas as the data you want to draw requires it.
Jatin sachdev
Ranch Hand

Joined: Jan 21, 2010
Posts: 57
Its not working.....

i have wasted my 6 hours on this please help...

Add.java


Graph.java


When i run this Graph.java Scroll does not work
Please help me in this.

Since paint in Canvas can go beyond the set size of JFrame i want scrolling

Now this has seriously become a Headache.
Also suggest a good book for swing to learn.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Stephan mentioned ScrollPane, not JScrollPane. The former is an AWT component, the second a Swing component. You shouldn't mix AWT and Swing components.

But I repeat his question: Why are you using AWT, and not Swing? Replace the Canvas with a JPanel in which you override paintComponent. Just make sure to call super.paintComponent(g) as the first statement.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Check out the Swing tutorials:
http://download.oracle.com/javase/tutorial/uiswing/
http://download.oracle.com/javase/tutorial/ui/features/index.html

Anyway, regarding your problem. First of all, don't mix Swing and AWT components with eachother. Either use one or the other. If you're going to use Swing, extend a JPanel instead of a Canvas.
Also, when you use Swing, make sure to override paintComponent(Graphics), instead of paint(Graphics), or you will run into a lot of trouble. [edit] Rob mentioned all of this [/edit]

Now, you base your painting on some data, right? You can't paint if you don't know what to paint. Usually, you draw stuff depending on some fields you would have in your Add class. Now, this means that whenever you update your fields when the painting method has to draw something else, you should also be able to determine how big the painting is going to be. Then you update the preferred size of your component based on this information, and you revalidate the scroll pane.

Here are two examples, one for AWT, one for Swing:
Jatin sachdev
Ranch Hand

Joined: Jan 21, 2010
Posts: 57
Thanks a ton

Its now working....................yeahhhhhhhhhhhhhhhh.....
 
Don't get me started about those stupid light bulbs.
 
subject: to add Scrollbar to Frame in AWT