Results 1 to 13 of 13
  1. #1
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default How to autoscroll when a graphic is moving...

    Ok, I have a basic JFrame with a JPanel, JPanel being my container for drawing. My JFrame is set to 800, 600 but my JPanelis bigger then this, as i have no components on my JPanel I want my JFrame to auto scroll to my picture...how could this work?

    Thanks!

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: How to autoscroll when a graphic is moving...

    Why CardLayout?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How to autoscroll when a graphic is moving...

    That wasn't compulsory... just because it maximises the panel within the frame without having to set margins etc, and he only needed to show one panel at a time. In fact i'm not sure why I even added that, it would have been my personal preference in this case though.

  5. #5
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default Re: How to autoscroll when a graphic is moving...

    Sorry, i never replied my problem is that when i put the JPanel in something it doesnt work my JPanel is extending a class.

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How to autoscroll when a graphic is moving...

    Post your code as well as your debugger/compiler output, "it doesn't work" isn't enough information to work with here!

  7. #7
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default Re: How to autoscroll when a graphic is moving...

    Class1 is the frame, class2 is the panel with graphics and i removed package and imports so its clean:

    public class class1 extends JFrame{

    public class1(){
    JFrame GameFrame = new JFrame("title");
    GameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE);
    GameFrame.add(new class2());
    GameFrame.setSize(800, 600);
    GameFrame.setVisible(true);
    GameFrame.setBackground(Color.pink);
    GameFrame.setResizable(false);
    GameFrame.setLocationRelativeTo(null);
    }

    public static void main(String[] args){
    class1 c1 = new class1();
    }

    public class class2 extends JPanel implements ActionListener{

    public class2(){
    //my methods are here
    }
    }

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How to autoscroll when a graphic is moving...

    So your problem is just the size of the frame i'm guessing...

    add this code and tell me if the size comes correctly:
    Java Code:
    GameFrame.setPreferredSize(800, 600);
    GameFrame.setMaximumSize(800, 600);

  9. #9
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default Re: How to autoscroll when a graphic is moving...

    No the size of the frame and panel are fine, but the problem is that the panel paints outside the frame which is fine but how would i move the jframe focus i guess...not really sure...what i would call that.

  10. #10
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How to autoscroll when a graphic is moving...

    Didn't you read the links I gave you before? You can do that easily with the JScrollPane, by setting the viewport.

  11. #11
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default Re: How to autoscroll when a graphic is moving...

    Okay, but do I need this to JFrame to JPanel?

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: How to autoscroll when a graphic is moving...

    Quote Originally Posted by ozzyman View Post
    That wasn't compulsory... just because it maximises the panel within the frame without having to set margins etc, and he only needed to show one panel at a time. In fact i'm not sure why I even added that, it would have been my personal preference in this case though.
    Ah, OK. My choice for that would be BorderLayout

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: How to autoscroll when a graphic is moving...

    Quote Originally Posted by Alkor View Post
    Okay, but do I need this to JFrame to JPanel?
    No, you don't. If you're not willing to go through links posted in order to help you, why ask on a forum at all? You've been given the link to the scroll pane tutorial; no go over to Oracle and read it.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. JScrollPane issues - no autoscroll wished
    By Flo in forum AWT / Swing
    Replies: 3
    Last Post: 08-18-2011, 02:17 PM
  2. How to autoscroll a JScrollPane
    By Junky in forum AWT / Swing
    Replies: 2
    Last Post: 03-10-2011, 10:32 PM
  3. Autoscroll website
    By Eliteoomph in forum New To Java
    Replies: 2
    Last Post: 02-09-2011, 10:33 PM
  4. Chat window autoscroll lockes scrollbar at buttom
    By ZacK.YoveL in forum AWT / Swing
    Replies: 2
    Last Post: 06-26-2010, 12:19 AM
  5. dragging the row header autoscroll
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-15-2010, 06:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •