• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

to add Scrollbar to Frame in AWT

 
Jatin sachdev
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5889
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephan van Hulst
Bartender
Pie
Posts: 5889
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton

Its now working....................yeahhhhhhhhhhhhhhhh.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic