View unanswered posts | View active topics It is currently Mon May 01, 2017 1:34 am

Reply to topic  [ 3 posts ] 
 Issue with Miglayout panel inside another panel 
Author Message

Joined: Wed Jan 20, 2010 4:50 am
Posts: 1
Post Issue with Miglayout panel inside another panel
Basically I have 1 panel (box layout, Y axis aligned) containing 2 other panels. The first one uses miglayout while the second one works with flow layout. The result is the following, and it shows how the miglayout is painted as if it were aligned to the right:

first.gif [ 3.63 KiB | Viewed 8777 times ]

However, if I change the layout used by the first inside-panel to flow layout for example, the form looks the way I excepted:

second.gif [ 3.48 KiB | Viewed 8777 times ]

I've made a simple class to demonstrate the issue I am having. Most probably, I'm doing something wrong...

public class Form extends JPanel {
   private JFrame frame = new JFrame("Form");
   private JComponent component;
   public Form() {
      this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

   protected void setComponent() {
      this.component = new JPanel(new MigLayout("debug"));
   protected void addFormButtons() {
      JPanel panel = new JPanel(new FlowLayout());
      panel.add(new JButton("Ok"));
      panel.add(new JButton("Cancel"));
   public Component add(Component component) {
      return (this.component.add(component));
   public void add(Component component, Object constraints) {
      this.component.add(component, constraints);

   public void setVisible(boolean visibility) {
      if (visibility) {
   public static void main(String[] args) {
      Form form = new Form();
      form.add(new JLabel("label"));
      form.add(new JTextField());


Note that the second screenshot was taken by running the same piece of code but replacing:
this.component = new JPanel(new MigLayout("debug"));

this.component = new JPanel();

Thanks in advance,

Wed Jan 20, 2010 5:09 am
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: Issue with Miglayout panel inside another panel

I can't really understand your code. You override add to add to another component and doing such strange things.

If you set the background of the container that MigLayout is handling to for instance blue you can see that it is that whole container that is moved to the right. MigLayout isn't in control of its own bounds, only the bounds of the components within. Therefore this seems to be something else.


Tue Jan 26, 2010 2:42 pm
Profile WWW

Joined: Fri Jun 15, 2012 8:56 am
Posts: 1
Post Re: Issue with Miglayout panel inside another panel
This issue has another root of evil:
If you adding something to BoxLayout which is Y-oriented, you should execute the method
for each object which is in that BoxLayout. (Maybe you should execute that method for all components in all BoxLayouts of your frame)
I think MigLayout container by default has LEFT_ALIGNMENT, so its position begins from center of another component and ends on the right.
Here is an example from first book about Swing from Ivan Portyankin:

Here you can find BoxLayoutDemo2, which explains the issue:

Fri Jun 15, 2012 9:20 am
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.