Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: JPanel padding?

  1. #1
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default JPanel padding?

    I'm currently in the process of writing a bouncing ball program with threads. The program will bounce 3 balls concurrently.

    I add a border around one instance of the Ball class, which extends JPanel and implements runnable.

    I then add ball2 to ball1, and ball3 to ball2.

    preferredSize of the panel is set in the Ball class, so no matter which instance has the border invoked on, they should all bounce off the same coordinates.

    My problem is that, ball 2 and 3 go further down than ball 1, and go up a smaller height.

    To examine the problem I added a border to each ball instance, and it gave me the following result:

    ImageShack® - Online Photo and Video Hosting

    I was wondering how i could avoid such padding of the JPanels (i assume its padding) or what the problem is, are there restrictions in JPanels that they somehow don't allow such usage?

    Any help/advice would be greatly appreciated.
    Last edited by newbie; November 26th, 2010 at 07:26 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JPanel padding?

    Might help to post a short snippet of code which clearly demonstrates the problem. Any reason you are using several JPanels and not doing custom painting?

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: JPanel padding?

        public BouncingBalls() {
            b1 = new Thread(ball1); 
            b2 = new Thread(ball2);
            b3 = new Thread(ball3);
            controlPanel();
        }
     
        private void controlPanel() {
            ball1.setBorder(BorderFactory.createLineBorder(Color.black, 5));
            ball1.add(ball2);
            ball2.add(ball3); //ballx are instances of a class Ball, which holds coordinates for checking
     
            JPanel controlPanel = new JPanel();
            controlPanel.add(ball1); //Adds ball1 onto the JPanel as a parent of all Ball instances
            add(controlPanel, BorderLayout.CENTER);
            add(boxes(), BorderLayout.SOUTH); //Adds button to South position
        }

    This is where i believe the problem to be occurring (possibly).
    Not quite sure what you mean by custom painting? could you elaberate as im not very term savvy when it comes to GUI's.

    Thanks once again.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JPanel padding?

    Java Custom Painting. What I mean by a short snippet was more along the lines of an SSCCE, because it is difficult to guess with the above information. Reproducing the problem with an example makes troubleshooting much easier.

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: JPanel padding?

    If what you said is true, that the balls are added inside each-other, then I think you have one panel inside another panel, inside another panel. I do not think this is what you want. I think you want to have to have all 3 inside the parent panel or frame or whatever, floating freely from each other.

    Also you need to figure out what LayoutManager you are using and how that will effect things. You might want to figure out how to use LayoutManager null or something like that - as the layout manager will try and force your components into some sort of placement, whereas what you want to do is place them manually. I recommend you study this topic a bit, and try to manually place 1 ball before you try and place 3.

  6. The Following User Says Thank You to TheShagg For This Useful Post:

    newbie (November 29th, 2010)

Similar Threads

  1. BAd Padding exception
    By Bverly in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 26th, 2010, 03:41 PM
  2. JPanel with ScrollBars
    By aussiemcgr in forum AWT / Java Swing
    Replies: 1
    Last Post: September 24th, 2010, 03:45 PM
  3. JButton on JPanel
    By JavaLearner in forum AWT / Java Swing
    Replies: 4
    Last Post: March 26th, 2010, 07:46 AM
  4. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM