Results 1 to 4 of 4
Like Tree2Likes
  • 2 Post By Fubarable

Thread: Layout problem

  1. #1
    bmL
    bmL is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Layout problem

    I'm new to java so I would like to say Hello.

    Right now im strugling to catch up on layouts in java. But now I'm having realy strange problem. I have decalred 2 test classes:
    Java Code:
    public class TestApplet extends Applet {
        
        @Override
        public void init() {
            setSize(500, 500);
            setBackground(Color.yellow);
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            add(new JButton("Button 1"));
            add(new TestJPanel());
            add(new JButton("Button 1"));
        }
    }
    Java Code:
    public class TestJPanel extends JPanel {
    
        @Override
        public void addNotify() {
            Dimension d = new Dimension(500, 100);
            setSize(d);
            setPreferredSize(d);
            setBackground(Color.red);
            super.addNotify();
        }
    }
    There is snapshot of what i get: http://oi49.tinypic.com/2ufbtyq.jpg
    And what I've expected: http://oi45.tinypic.com/2n0lzj7.jpg (modified in paint)
    Basicly what happends:
    -the TestJpanel inherits height and width from it's parent (TestApplet).
    TestApplet changes it's size to 500+25+25 (500 original height = height of testApplet; 25 = height of buttons)

    Why it happends? Why does the testJPanel inherit deminsions from applet?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Layout problem

    The layout is what sets the sizes. Some layouts fully respect the preferredSize of a component, some don't. BoxLayout will expand some components beyond their preferredSize if necessary as long as it doesn't exceed their maximumSize. Consider setting the maximumSize of your red JPanel, and then adding some glue to the bottom of your BoxLayout-using container.

    As an aside: since this is a Swing GUI, don't use an Applet object, but rather a JApplet.
    DarrylBurke and bmL like this.

  3. #3
    bmL
    bmL is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: Layout problem

    Thank You very much, maximumSize solves the problem :)
    So the setSize is used when I set up layout to null. And on other hand setPreferedSize and maximumSize is considered by layouts.?

    Again thanks and I hope I'll quicky catch up and I'll be able to help others :)

    cu :)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Layout problem

    Quote Originally Posted by bmL View Post
    setPreferedSize and maximumSize is considered by layouts.?
    And/or the minimumSize. For example, when a GridBagLayout can't accommodate a component at its preferredSize, it shrinks it down directly to its minimumSize (actually, it does that independently on the x and y axes, so a component may end up being displayed at its minimum width and preferred height).
    I hope I'll quicky catch up
    Practice, practice, practice.

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

Similar Threads

  1. Layout Problem
    By kris5228 in forum AWT / Swing
    Replies: 6
    Last Post: 01-09-2012, 09:38 PM
  2. Layout problem
    By miller.bartek in forum AWT / Swing
    Replies: 3
    Last Post: 01-11-2011, 07:50 PM
  3. problem in managing layout
    By namreen in forum NetBeans
    Replies: 10
    Last Post: 09-04-2010, 09:41 AM
  4. java on mac - gui layout problem
    By dalo in forum AWT / Swing
    Replies: 0
    Last Post: 06-19-2010, 10:15 AM
  5. [SOLVED] Layout Problem
    By casid in forum New To Java
    Replies: 4
    Last Post: 06-02-2009, 11:17 PM

Posting Permissions

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