View unanswered posts | View active topics It is currently Wed Apr 26, 2017 10:29 pm



Reply to topic  [ 4 posts ] 
 Width % does not work if it used inside a container 
Author Message

Joined: Tue Dec 18, 2012 4:19 pm
Posts: 3
Post Width % does not work if it used inside a container
Hello,
I would like to use percentage for "width". I am not sure if the code is correct or is this a bug but with 100% just do not want to work. When the gui appears it is good but if I resize the frame the content consumes bigger place than it should. With 90% it is a bit better but still not perfect.
I would like a container which always grabs the horizontal space and I want to put an another container "width=100%" into the horizontally stretched container.
I use the 4.0 version, The example uses Swing but the same behavior with with SWT as well.
Thank you, Gabor
Code:
import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;

import net.miginfocom.layout.CC;
import net.miginfocom.swing.MigLayout;

public class MigLayoutTestForImitatedTableSwing extends JFrame
{
    MigLayoutTestForImitatedTableSwing()
    {
        JPanel rootPanel = new JPanel();
        rootPanel.setLayout(new BorderLayout());
        JScrollPane scrollPane = new JScrollPane(rootPanel);
        scrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(scrollPane);
        add(panel);
        buildContent(rootPanel);
    }

    static void buildContent(JPanel parent)
    {
        JPanel compRealForm = new JPanel();
        parent.add(compRealForm);

        MigLayout layout = new MigLayout();
        layout.setColumnConstraints("grow");
        compRealForm.setLayout(layout);
        // ------------------------------------------------------

        // Table holder, I want this container to be stretched horizontally
        JPanel tableHolder = new JPanel();
        MigLayout layoutTH = new MigLayout("","[grow]","[grow]");
        tableHolder.setLayout(layoutTH);
        tableHolder.setBackground(new Color(0,0,200));

        // grabHorizontally (this container alone works good)
        CC thCC = new CC();
        thCC.growX();
        compRealForm.add(tableHolder,thCC);

        // real table (This does not work with 100%)
        if (true)
        {
            // I need this solution
           
            JPanel tableComp = new JPanel();
            MigLayout layoutT = new MigLayout("","[grow]","[grow]");
            // MigLayout layoutT = new MigLayout();
            tableComp.setLayout(layoutT);
            tableComp.setBackground(new Color(0,200,0));
            CC tCC = new CC();
            tCC.width("100%");
            tableHolder.add(tableComp,tCC);

            // add more content into the tableComp container
            // ....
        }
        else
        {
            // Try with a label to see if the Composite has caused the problem
            // or not, but this is also does not work.
            JLabel label = new JLabel("100%");
            label.setOpaque(true);
            label.setBackground(new Color(0,200,0));
            CC labelCC = new CC();
            labelCC.width("100%");
            tableHolder.add(label,labelCC);
        }
    }

    public static void main(String[] args)
    {
        MigLayoutTestForImitatedTableSwing test = new MigLayoutTestForImitatedTableSwing();
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test.setSize(500,400);
        test.setVisible(true);
    }
}


Tue Dec 18, 2012 4:52 pm
Profile
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: Width % does not work if it used inside a container
Width is not good when it relies on a parent that can change. It will always be one step behind.

try "growx,pushx" instead and see if that works.

Cheers,
Mikael


Wed Dec 26, 2012 3:31 pm
Profile WWW

Joined: Tue Dec 18, 2012 4:19 pm
Posts: 3
Post Re: Width % does not work if it used inside a container
Am I right that the "growx,pushx" work if there are at least two widgets in the same cell?
I did two things:
1. removed the "[grow]" column from the MigLayout paramer and added "gap 0" (to avoid gap around the extra widget).
2. added a label just to have two widgets in the same cell and it works fine this way.
Is this the right way? Do I really have to add an extra widget? I tried with SWT and it is the same.

Code:
 MigLayout layoutTH = new MigLayout("gapx 0","","[grow]");
        tableHolder.setLayout(layoutTH);
        tableHolder.setBackground(new Color(0,0,200));

        // grabHorizontally (this container alone works good)
        CC thCC = new CC();
        thCC.growX();
        compRealForm.add(tableHolder,thCC);

        // real table (This does not work with 100%)
        if (true)
        {
            float percentage = 90;
           
            JPanel tableComp = new JPanel();
            MigLayout layoutT = new MigLayout("","[grow]","[grow]");
            tableComp.setLayout(layoutT);
            tableComp.setBackground(new Color(0,200,0));
            CC tCC = new CC();
            // this is not good
            // tCC.width("100%");

            // percentage is used this way
            tCC.growX(percentage);
            tCC.pushX(percentage);
            tableHolder.add(tableComp,tCC);

            // added an extra widget. The real widget (JPanel) will share the
            // horizontal space with this "invisible" widget.
            JLabel label = new JLabel();         
            CC lcc = new CC();
            lcc.growX(100 - percentage);
            lcc.pushX(100 - percentage);
            tableHolder.add(label,lcc);

            // add more content into the tableComp container
            // ....
        }


Wed Jan 02, 2013 4:38 pm
Profile

Joined: Tue Dec 18, 2012 4:19 pm
Posts: 3
Post Re: Width % does not work if it used inside a container
Actually I wanted to have the enclosed layout. When we can set the width in percentage and we can also use the horizontal alignment. Do you know how to achieve this?
Thank you, Gabor


Attachments:
File comment: expected layout
widhtPercentageWithAlignX.png
widhtPercentageWithAlignX.png [ 11.06 KiB | Viewed 29966 times ]
Thu Jan 03, 2013 11:18 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 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.