Results 1 to 4 of 4
Thread: Layout problem
- 06-23-2012, 07:53 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 4
- Rep Power
- 0
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")); } }There is snapshot of what i get: http://oi49.tinypic.com/2ufbtyq.jpgJava 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(); } }
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?
-
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.
- 06-23-2012, 10:34 PM #3
Member
- Join Date
- Jun 2012
- Posts
- 4
- Rep Power
- 0
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 :)
- 06-24-2012, 04:24 AM #4
Re: Layout problem
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).
Practice, practice, practice.I hope I'll quicky catch up
dbbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Layout Problem
By kris5228 in forum AWT / SwingReplies: 6Last Post: 01-09-2012, 09:38 PM -
Layout problem
By miller.bartek in forum AWT / SwingReplies: 3Last Post: 01-11-2011, 07:50 PM -
problem in managing layout
By namreen in forum NetBeansReplies: 10Last Post: 09-04-2010, 09:41 AM -
java on mac - gui layout problem
By dalo in forum AWT / SwingReplies: 0Last Post: 06-19-2010, 10:15 AM -
[SOLVED] Layout Problem
By casid in forum New To JavaReplies: 4Last Post: 06-02-2009, 11:17 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks