View unanswered posts | View active topics It is currently Thu Mar 30, 2017 3:38 am



Reply to topic  [ 12 posts ] 
 Add feature: MiGLayout to behave like Wrap Layout? 
Author Message

Joined: Tue Apr 19, 2011 6:48 pm
Posts: 8
Post Add feature: MiGLayout to behave like Wrap Layout?
By reading around on this forum, it seems as if the MiGLayout team has no intention of adding support for flowing layouts. Although, it seems very much possible to implement this function in a good way, judging from how well Wrap Layout handles it. What is the real problem here? Wouldn't this be a good addition to MiGLayout? If MiGLayout claims that "You will never have to switch to another layout manager ever again!", and that it is "MiGLayout - The Java Layout Manager"; all possible use cases should be covered imho. Today, I am actually in need of a seperate layout manager than MiGLayout, namely Wrap Layout.

I really like MiGLayout (thanks!), and would love to see such functionality be added to it, so that it would truly become the Java Layout Mangager.

Also see:
http://stackoverflow.com/questions/5715 ... rap-layout


Tue Apr 19, 2011 7:08 pm
Profile
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: Add feature: MiGLayout to behave like Wrap Layout?
The problem is that Swing doesn't support "what will be your height given this width" and vice versa. That's why a flowing layout can't be done in a good way.

Take for instance FlowLayout which doesn't report the correct preferred height for a layout. Try for instance to add a FlowLayout to a top panel in BorderLayout.

Cheers,
Mikael


Tue Apr 19, 2011 7:13 pm
Profile WWW

Joined: Tue Apr 19, 2011 6:48 pm
Posts: 8
Post Re: Add feature: MiGLayout to behave like Wrap Layout?
Try resizing the frame when running the following code.
Everything seems just dandy. Even though I am placing the
flowing layout in the north section of a BorderLayout.

Code:
import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MiGTest extends JFrame{
   
   public static void main(String[] args) {
        new MiGTest().setVisible(true);
    }

   private JPanel jPanel;
   private JButton jButton;
   
   public MiGTest(){
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      //setLayout(new MigLayout("debug, fillx", "[fill]"));
      setLayout(new BorderLayout());
      initComponents();
      addComponents();
      pack();
   }
   
    private void addComponents() {      
       add(jPanel, BorderLayout.NORTH);{
           for (int i = 0; i < 10; i++) {
                jPanel.add(new JButton("" + i));
            }
       }
   }

   private void initComponents() {
      jPanel = new JPanel(new WrapLayout());
      jButton = new JButton("Test");   
   }
}


Also note how it works just as fine when switching comments on the lines

Code:
      setLayout(new BorderLayout());
      //setLayout(new MigLayout("debug, fillx", "[fill]"));


, that is, when using MiGLayout instead of BorderLayout.

The Wrap Layout class:

http://www.camick.com/java/source/WrapLayout.java

Is there an issue here that I am missing?

Thanks


Tue Apr 19, 2011 8:02 pm
Profile
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: Add feature: MiGLayout to behave like Wrap Layout?
Using the parent size works for simple layouts that just have a frame and some content. As soon as you have layouts in layout that "fight" for the space in the frame this doesn't work since the size of the parent is not set and will depend on the preferred size of the children. Thus, using the current size of the parent will create a circular dependency.

It will work for a lot of layouts though.

Cheers,
Mikael


Tue Apr 19, 2011 9:50 pm
Profile WWW

Joined: Tue Apr 19, 2011 6:48 pm
Posts: 8
Post Re: Add feature: MiGLayout to behave like Wrap Layout?
Ok, thanks for the clarification =)


Wed Apr 20, 2011 7:00 pm
Profile

Joined: Mon Aug 23, 2010 6:24 am
Posts: 54
Post Re: Add feature: MiGLayout to behave like Wrap Layout?
MiG Support wrote:
The problem is that Swing doesn't support "what will be your height given this width" and vice versa. That's why a flowing layout can't be done in a good way.

Take for instance FlowLayout which doesn't report the correct preferred height for a layout. Try for instance to add a FlowLayout to a top panel in BorderLayout.

Cheers,
Mikael


Hi Mikael,

I was under the impression that layouts are a recursive process where Swing determines the size of the parent (starting with JFrame) and then asks the Layout Manager to lay out the children. Once you know the bounds of the children you repeat the process for their descendants. If that is indeed the case, what prevents flowing layout from working? I get the feeling I am misunderstanding something about the above explanation...

Thanks,
Gili


Mon May 09, 2011 8:55 pm
Profile
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: Add feature: MiGLayout to behave like Wrap Layout?
The algorithm is depth first so that the children is asked first then the parents.


Mon May 09, 2011 9:26 pm
Profile WWW

Joined: Mon Aug 23, 2010 6:24 am
Posts: 54
Post Re: Add feature: MiGLayout to behave like Wrap Layout?
MiG Support wrote:
The algorithm is depth first so that the children is asked first then the parents.


Okay, but don't you know the parent's dimensions when laying out its children? Shouldn't you then be able to wrap the children automatically?

Gili


Mon May 09, 2011 9:53 pm
Profile
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: Add feature: MiGLayout to behave like Wrap Layout?
Nope. The layout process in the parent asks the child for its preferred size to know its size and at that point you can't say your preferred size since you don't know the width.

When you do the actual .setBounds() on the component you do, but then it's already too late.


Mon May 09, 2011 9:58 pm
Profile WWW

Joined: Mon Aug 23, 2010 6:24 am
Posts: 54
Post Re: Add feature: MiGLayout to behave like Wrap Layout?
MiG Support wrote:
Nope. The layout process in the parent asks the child for its preferred size to know its size and at that point you can't say your preferred size since you don't know the width.

When you do the actual .setBounds() on the component you do, but then it's already too late.


Ah! That clears it up. Thank you :)

Gili


Mon May 09, 2011 10:48 pm
Profile

Joined: Wed Feb 22, 2012 11:55 am
Posts: 9
Post Re: Add feature: MiGLayout to behave like Wrap Layout?
Hi,

My reply is mainly addressed to MiGMaster. Currently, I am facing the exact same problem as the one you described in your first post in this forum. I was hoping (too) that the existence of "WrapLayout" (which seems to be working well) would motivate MigLayout to eventually support flowing layouts.

First of all, could you please tell us what method did you eventually adopt? Did you eventually use WrapLayout within MigLayout?

Also, I would like to point to the following:

1. I agree with you that using WrapLayout instead of FlowLayout (as demonstrated in your MiGTest example) solves the problem "when placing the flowing layout in the north section of a BorderLayout", which was raised by Mikael (MiG Support).

2. I agree that WrapLayout works fine when used inside MiGLayout, however for it to work in your example, it is not enough to just switch the comments on the lines:

Code:
setLayout(new BorderLayout());
//setLayout(new MigLayout("debug, fillx", "[fill]"));


as you described. If you only do that, wrapping will work fine only if you're resizing the frame in very small increments. As soon as you resize it "in one big shot", you will notice how part of the content of the frame will disappear (and hence wrapping won't work properly).

For it to work properly, one way is to add the panel inside a scrollpane, i.e. in your code, instead of directly adding jPanel, add new JScrollPane(jPanel). Now, wrapping should work properly. Do you agree with me on this point?

3. So far JScrollPane solved our problem, as long as the container in its viewport had only one child. The example below puts two child panels inside the root panel contained in the scrollPane. In this case, again, unless if you're resizing the frame in very small increments, wrapping won't work properly.

Code:
public class MiGTestModified extends JFrame {

   private static final long serialVersionUID = 1L;

   public static void main(String[] args) {
      new MiGTestModified().setVisible(true);
   }

   public MiGTestModified() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(700, 400);

      JPanel rootPanel = new JPanel(new MigLayout("debug, fill", "[fill]", "[fill]"));
      JScrollPane scrollPane = new JScrollPane(rootPanel);

      JPanel panelA = new JPanel(new WrapLayout());
      for (int i = 0; i < 10; i++) {
         panelA.add(new JButton("" + i));
      }
      
      JPanel panelB = new JPanel(new WrapLayout());
      for (int i = 0; i < 10; i++) {
         panelB.add(new JButton("" + i));
      }
      
      rootPanel.add(panelA, "cell 0 0");
      rootPanel.add(panelB, "cell 0 1");
      getContentPane().add(scrollPane);
   }
}


However, if you set the layout of rootPanel to BorderLayout, and you use say BorderLayout.NORTH and BorderLayout.SOUTH for panelA and panelB, WrapLayout works normally.

Hence, it seems that mixing WrapLayout within MigLayout is not that safe!

Feedback from MiG Support is also highly appreciated.

Thank you,
Shant


Thu Feb 23, 2012 6:30 pm
Profile

Joined: Tue Jan 17, 2012 3:52 am
Posts: 1
Post Services
I am sorry, that I interfere, but you could not paint little bit more in detail.


Thu Mar 08, 2012 2:05 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 12 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.