View unanswered posts | View active topics It is currently Wed Mar 01, 2017 11:55 pm



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:

Attachment:
first.gif
first.gif [ 3.63 KiB | Viewed 8614 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:

Attachment:
second.gif
second.gif [ 3.48 KiB | Viewed 8614 times ]


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

Code:
public class Form extends JPanel {
   private JFrame frame = new JFrame("Form");
   private JComponent component;
   
   public Form() {
      super();
      this.frame.add(this);
      this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
      this.setComponent();
      super.add(this.component);
      this.addFormButtons();
   }

   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"));
      super.add(panel);
   }
   
   @Override
   public Component add(Component component) {
      return (this.component.add(component));
   }
   
   @Override
   public void add(Component component, Object constraints) {
      this.component.add(component, constraints);
   }

   @Override
   public void setVisible(boolean visibility) {
      if (visibility) {
         super.setVisible(visibility);
         this.frame.pack();
      }
      this.frame.setVisible(visibility);
   }
   
   public static void main(String[] args) {
      Form form = new Form();
      form.add(new JLabel("label"));
      form.add(new JTextField());
      form.setVisible(true);
   }

}


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


with:
Code:
this.component = new JPanel();


Thanks in advance,
Juan


Wed Jan 20, 2010 5:09 am
Profile
Site Admin

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

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.

Cheers,
Mikael


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
obj.setAlignmentX(Component.LEFT_ALIGNMENT);
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:
Image

Here you can find BoxLayoutDemo2, which explains the issue:
http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html


Fri Jun 15, 2012 9:20 am
Profile
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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.