• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

to add Scrollbar to Frame in AWT

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 15729
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15729
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton

Its now working....................yeahhhhhhhhhhhhhhhh.....
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic