This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFrame" Watch "JFrame" New topic
Author

JFrame

Ben Buchli
Ranch Hand

Joined: Mar 26, 2004
Posts: 83
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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

  • 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.


  • [Jess in Action][AskingGoodQuestions]
    Ben Buchli
    Ranch Hand

    Joined: Mar 26, 2004
    Posts: 83
    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 ]
    Eddie Vanda
    Ranch Hand

    Joined: Mar 18, 2003
    Posts: 281
    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:


    The nice thing about Standards is that there are so many to choose from!
    Ben Buchli
    Ranch Hand

    Joined: Mar 26, 2004
    Posts: 83
    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
    Craig Wood
    Ranch Hand

    Joined: Jan 14, 2004
    Posts: 1535
    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

    Joined: Mar 26, 2004
    Posts: 83
    Thanks a lot Craig,
    seems to work...
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: JFrame
     
    Similar Threads
    JPanel and JFrame
    java swings paint
    set picture as background of JFRAME and JPanel
    How to size jsplitpane?
    Difference between JFrame and JPanel