LayoutManager and LayoutManager2

View previous topic View next topic Go down

Java LayoutManager and LayoutManager2

Post by BIT0102-Mohaimin on Mon Feb 21, 2011 1:33 pm

If you have worked with java swing/awt, you are possibly familial with the interface LayoutManager. At least this method defined by class Container:
Code:
public void setLayout(LayoutManager mgr);

So what is that LayoutManager2!
This is another interface that extends LayoutManager. That means, LayoutManager2 is also a LayoutManager.
What is the difference?
All layout managers divides the Container in some small parts. When we add something, it is occupies one of those parts.
If the LayoutManager you are using implements only LayoutManager, there is no way you can specify which particular part it should occupy, it has its own order to add Components. On the other hand, if you are using a LayoutManager2, you can set additional constraints that will specify a location where to add.

We do not directly add components using the layout manager, rather we use the add() method. There are several versions of this method. I am mentioning two.
Code:
public Component add(Component comp);
public void add(Component comp, Object constraints);
The second one is used to pass a constraint.

FlowLayout and GridLayout implemets LayoutManager.

BorderLayout, CardLayout, GridBagLayout, BoxLayout, GroupLayout, OverlayLayout, SpringLayout implemets LayoutManager2. I have used the first three a lot, often the 4th one. First two uses String as Constraint, GridBagLayout uses a custom Constrain of its own, called GridBagConstraints.

Now answer to a question I had since a long time. Many of you may have the same one. "Why I cannot specify index of a grid in a GridLayout, why it adds components sequentially?" Answer is above, it does not implement LayoutManager2, so cannot impose constraints.

This topic is a consequence of this topic posted by BIT0122-Amit. He owes to be mentioned
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Java Re: LayoutManager and LayoutManager2

Post by BIT0122-Amit on Mon Feb 21, 2011 1:53 pm

You are becoming a Pro Dude Very Happy Please keep it up.

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Java Re: LayoutManager and LayoutManager2

Post by BIT0220-Iftekhar on Mon Feb 21, 2011 4:23 pm

BIT0102-Mohaimin wrote:
Now answer to a question I had since a long time. Many of you may have the same one. "Why I cannot specify index of a grid in a GridLayout, why it adds components sequentially?" Answer is above, it does not implement LayoutManager2, so cannot impose constraints.

This question arose in my mind too. Swing LayoutManager is one of the topics I want to study in depth. So good to know why GridLayout don't permit to add components at a particular grid index. Smile
rep++ Mohaimin vai
avatar
BIT0220-Iftekhar
Administrator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 434
Points : 630

View user profile http://iftekharunplugged.blogspot.com

Back to top Go down

Java Re: LayoutManager and LayoutManager2

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum