Changing component margin doesn't work

30 views
Skip to first unread message

arsen...@gmail.com

unread,
Jul 22, 2014, 12:33:11 AM7/22/14
to codenameone...@googlegroups.com
Hello,

I want to change height of component by button click, I have two buttons for it ("up" and "down"), and three "positions" of component. FULLSCREEN, when top margin = 0. MIDDLE - when top margin = (display height / 2), and BOTTOM, when top margin = (display height - 100). Starting "position" is BOTTOM

What problem did I met - that when you extend from BOTTOM to MIDDLE - it extends, but then, if you click any of two buttons - graphically nothing changes, but Log prints, that margin of component changes. And it changes correctly. Also Logs from actionListeners are printing everything as it works correctly.

It doesn't changes only graphically.

Why it doesn't change the UI? The component is one of two presented in parent container, which is Layer Layout

Here's actionListeners:

        upButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {

                // if component in BOTTOM position - expand to MIDDLE
                if (findContainer(f).getStyle().getMargin(
                        Component.TOP) == display.getDisplayHeight() - 100) {
                    Log.p("Up action if", Log.ERROR);
                    setDrawerSize(f, (display.getDisplayHeight() / 2));

                // else, expand to FULLSCREEN
                } else {
                    Log.p("Up action else", Log.ERROR);
                    setDrawerSize(f, display.getDisplayHeight());
                }
            }
        });

        downButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {

                // if component in FULLSCREEN position - reduce to MIDDLE
                if (findTourMapDrawerContainer(f).getStyle().getMargin(
                        Component.TOP) == display.getDisplayHeight()) {
                    Log.p("Down action if", Log.ERROR);
                    setDrawerSize(f, (display.getDisplayHeight() / 2));

                // else, reduce to BOTTOM position
                } else {
                    Log.p("Down action else", Log.ERROR);
                    setDrawerSize(f, 100);
                }
            }
        });

Here's setDrawerSize() method:


    private void setDrawerSize(Form f, int drawerSize) {

        Container container = findContainer(f);
        container.getStyle().setMargin(Component.TOP, (display.getDisplayHeight() - drawerSize));

        container.setScrollableY(false);
        if (container.getStyle().getMargin(Component.TOP) < (display.getDisplayHeight() - 100)) {
            container.setScrollableY(true);
        }
        parentContainer.repaint();
    }

If you are experiencing an issue please mention the full platform your issue applies to:
IDE: Eclipse
Ubuntu 12.04
Real devices + simulators

Chen Fishbein

unread,
Jul 22, 2014, 1:34:47 AM7/22/14
to codenameone...@googlegroups.com, arsen...@gmail.com
You need to trigger re-layout, try to call revalidate() on your parent Container

arsen...@gmail.com

unread,
Jul 22, 2014, 1:44:11 AM7/22/14
to codenameone...@googlegroups.com, arsen...@gmail.com
That's strange... If I call repaint() on parent - it acts as I described in my question.
If I call revalidate() alone, or with repaint() after it - it works, except it doesn't update screen when I change position from BOTTOM to MIDDLE when I click "Up".
After that, when I click "Up" once more - it expands to FULLSCREEN.

"Down" button works as it should.

What problem could it be?

Shai Almog

unread,
Jul 22, 2014, 9:12:52 AM7/22/14
to codenameone...@googlegroups.com, arsen...@gmail.com
You are using setWidth/height/x/y etc. which you shouldn't. The layout manager (which is invoked on revalidate) is placing the components in the right place.
You should position components only thru the layout managers http://www.codenameone.com/how-do-i---positioning-components-using-layout-managers.html

arsen...@gmail.com

unread,
Jul 22, 2014, 2:05:44 PM7/22/14
to codenameone...@googlegroups.com, arsen...@gmail.com
Hello,

Instead of setiny height and width - I'm using setting margins, which is working on code. I mean, in first case, when you click twice "up" button, component remains in MMIDDLE state only on the screen, and if you get it's margin - it will return 0, as it should return, when component is in FULLSCREEN state. It doesn't redraw from MIDDLE to any other, but when I add call of parent's revalidate() - it doesn't show, that component in MIDDLE state, after I click once "up" button, and after second "up" click - it "jumps" directly to FULLSCREEN state.

The problem is only in showing element on the screen. All margin properties are correct after each "up"/"down" button press

Shai Almog

unread,
Jul 23, 2014, 2:02:14 AM7/23/14
to codenameone...@googlegroups.com, arsen...@gmail.com
Hi,
margin varies between selected/unselected/pressed states.

arsen...@gmail.com

unread,
Jul 23, 2014, 2:22:04 AM7/23/14
to codenameone...@googlegroups.com, arsen...@gmail.com
Thank you!
Thank you very much!
That's was my problem. Component has changed its state, and that's why it hasn't worked properly.
Reply all
Reply to author
Forward
0 new messages