• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

JFrame

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everybody,
I have a JFrame and added a JPanel to it, now how do I force shapes drawn to appear in this JPanel?
Thanks for the help!
Ben
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • Write a subclass of JPanel, and use that instead of JPanel itself.
  • In your subclass, override paintComponent(Graphics).
  • In the first line of your override, call super.paintComponent(Graphics).
  • Then draw your shapes.

  •  
    Ben Buchli
    Ranch Hand
    Posts: 83
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks, Ernest.
    It works...
    how would I find that out without help???
    now another question i have regarding panels. is it possible to add a ScrollPane to the Panel so that it becomes scrollable when the shapes are moved and not completely visible in the pane?
    how would i do that?
    thanks in advance
    [ May 04, 2004: Message edited by: Ben Buchli ]
     
    Ranch Hand
    Posts: 283
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Instead of adding your subclassed JPanel to the JFrame's Container instance (from getContentPane), use it as the argument to constructing a new JScrollPane and then add that JScrollPane instance to your Container instance.
    So if your JPanel subclass is MyJPanel and the instance is myJPanel, then instead of using add (myJpanel), use:
     
    Ben Buchli
    Ranch Hand
    Posts: 83
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks, Eddie, thats what I did.
    However, the problem is that when the shape gets drawn outside of the viewing window (which means it's not or only partially visible) then I want to have scroll bars available for the user to scroll.
    How do I do that?? I have the scrollbars, but somehow, they stay disabled...
    thanks,
    Ben
     
    Ranch Hand
    Posts: 1535
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The idea is to override getPreferredSize in our drawing component and to keep it apprised of the changing width and height requirements during the drawing. As we update the width and height fields we call revalidate on the drawing component which tells the scrollpane we need more space.
    Here's an example:
     
    Ben Buchli
    Ranch Hand
    Posts: 83
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks a lot Craig,
    seems to work...
     
    They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
    Free, earth friendly heat - from the CodeRanch trailboss
    https://www.kickstarter.com/projects/paulwheaton/free-heat
    reply
      Bookmark Topic Watch Topic
    • New Topic