How to force width / height of a dialog

33 views
Skip to first unread message

howud...@gmail.com

unread,
Sep 22, 2016, 1:28:03 PM9/22/16
to CodenameOne Discussions
I override calcPreferredSize to force a dialog to be larger (for a large textarea).  However it still shows up as a small dialog packed to the minimum 

package com.howudodat.sdcpmobile.ui;


import com.codename1.ui.Button;
import com.codename1.ui.Dialog;
import com.codename1.ui.FontImage;
import com.codename1.ui.TextArea;
import com.codename1.ui.geom.Dimension;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.spinner.Picker;
import com.codename1.ui.table.TableLayout;



public class DlgClientHistory extends Dialog {
 
public boolean Cancelled = false;
 
protected Button btnApply = new Button(FontImage.createMaterial(FontImage.MATERIAL_DONE, UIManager.getInstance().getComponentStyle("Command"), 5));
 
protected Button btnClose = new Button(FontImage.createMaterial(FontImage.MATERIAL_CLEAR, UIManager.getInstance().getComponentStyle("Command"), 5));
 
 
protected Picker cmbDate = new Picker();
 
protected TextArea taNotes = new TextArea();
 
protected TableLayout layout = new TableLayout(4,3);
 
 
protected FrmClient parent = null;


 
public DlgClientHistory(FrmClient frm) {
 parent
= frm;
        initManualComponents
();
   
}
   
   
protected void initManualComponents() {
//     taNotes.setPreferredSize(new Dimension(800, 150));
   
     
this.setLayout(layout);
   
this.setUIID("Form");
   
this.setDialogUIID("Form");
       
this.getStyle().setBgTransparency(255);
       
this.getDialogStyle().setBgTransparency(255);


     
Container cnt2 = new Container();
     cnt2
.add(btnApply);
     cnt2
.add(btnClose);
    btnApply
.addActionListener(evt -> onApply());
     btnClose
.addActionListener(evt -> onClose());
     add
(layout.createConstraint().horizontalSpan(3).horizontalAlign(Component.RIGHT), cnt2);
     add
(layout.createConstraint().horizontalSpan(1).horizontalAlign(Component.RIGHT), new Label("Date:"));
     add
(layout.createConstraint().horizontalSpan(1), cmbDate);
     add
(layout.createConstraint().horizontalSpan(1), new Label());
     add
(layout.createConstraint().horizontalSpan(3), taNotes);
   
}
   

 
@Override
 
protected Dimension calcPreferredSize() {
 
// TODO Auto-generated method stub
 
return new Dimension(800, 500);
 
}
     


}


Shai Almog

unread,
Sep 23, 2016, 2:03:12 AM9/23/16
to CodenameOne Discussions, howud...@gmail.com
Use the show() method of a dialog that accepts 4 ints to determine the exact distance from the edge of the screen.

howud...@gmail.com

unread,
Sep 23, 2016, 11:05:40 AM9/23/16
to CodenameOne Discussions, howud...@gmail.com
yes, I know I can do that and it is a workaround, and not a very good on at that.
  • Why isn't the layout manager respecting the preferred sizes?
  • When I walk through your code, I see that the width / height of the components are being calculated (or at least it seems), but preferred size is never changing for the container.  then in showPackedImpl(), you use preferred size instead of what appears to be a calculated size (width / height)
  • In the documentation for setPreferredsize() you state: override calcPreferredSize(), yet this value is not being respected, neither for the textarea that I want larger, nor for the container.  
  • Additionally using showAt with 4 ints stretches the textarea horizontally but not vertically.

Finally, while this BUG is easy to test when you want a single field larger, it is similarly present whenever you layout a dialog with tablelayout and textfields.  The programmer has to manually hard code the dialog size.   

Shai Almog

unread,
Sep 24, 2016, 1:57:36 AM9/24/16
to CodenameOne Discussions, howud...@gmail.com
It is. You are setting the preferred size to the wrong component.

howud...@gmail.com

unread,
Sep 24, 2016, 10:42:04 AM9/24/16
to CodenameOne Discussions, howud...@gmail.com
This is so frustrating...
Which component does the preferred size need to be set on?  Here is a VERY SIMPLE test case.  Look at it, use it, test it, then please explain what's wrong.  It respects the preferred height but not width.

package com.mycompany.myapp;


import com.codename1.ui.Button;
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.Dialog;
import com.codename1.ui.FontImage;
import com.codename1.ui.Label;
import com.codename1.ui.TextArea;
import com.codename1.ui.geom.Dimension;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.table.TableLayout;


public class DlgClientFilter extends Dialog {
 
protected TableLayout layout = new TableLayout(10,3);

 
protected Button btnApply = new Button(FontImage.createMaterial(FontImage.MATERIAL_DONE, UIManager.getInstance().getComponentStyle("Command"), 5));



 
protected TextArea taNotes = new TextArea() {


 
@Override
 
protected Dimension calcPreferredSize() {
 
return new Dimension(800, 500);
 
}
 
 
};


 
public DlgClientFilter() {
        initManualComponents
();
   
}
   
   
protected void initManualComponents() {
   
this.setLayout(layout);


    taNotes
.setPreferredSize(new Dimension(800, 500));
       
   
Container content = getContentPane();
        content
.add(layout.createConstraint().widthPercentage(33), new Label(""));
        content
.add(layout.createConstraint().widthPercentage(33), new Label(""));
        content
.add(layout.createConstraint().widthPercentage(33), new Label(""));



   
Container cnt2 = new Container();
    cnt2
.add(btnApply);

    btnApply
.addActionListener(evt -> onApply());
    content
.add(layout.createConstraint().horizontalSpan(3).horizontalAlign(Component.RIGHT), cnt2);
    content
.add(layout.createConstraint().horizontalSpan(3), taNotes);


   
}
   
   
protected void onApply() {
      dispose
();
   
}
   
}

Shai Almog

unread,
Sep 25, 2016, 12:21:26 AM9/25/16
to CodenameOne Discussions, howud...@gmail.com
The preferred size of the dialog doesn't matter as it always takes up the entire screen.
What matters is the preferred size of its content.

howud...@gmail.com

unread,
Sep 25, 2016, 9:45:07 AM9/25/16
to CodenameOne Discussions, howud...@gmail.com
did you even look at my code?  It is the content that I set the preferred size on

Shai Almog

unread,
Sep 26, 2016, 12:08:13 AM9/26/16
to CodenameOne Discussions, howud...@gmail.com
Yep I looked and it isn't.

howud...@gmail.com

unread,
Sep 26, 2016, 1:07:41 AM9/26/16
to CodenameOne Discussions, howud...@gmail.com

What matters is the preferred size of its content.
No what matters is the preferred size of its contentPane 
It's clear your definition of content and mine are quite different.  

If the size of its content mattered, then setting the preferred size of one or more of the contents would cause the layout manager to appropriately calculate the size of the container in accord to the preferred size of its contents.  In fact what I have to do is hard code the size of the container.  A more appropriate response to my earlier question would be: "tablelayout does not calculate container sizes based upon the individual contents".

3 days ago, I already knew I could hard code the Dialog#show() or getContentPane()#setPreferredSize. That was an answer to my original question, but that still leaves my follow up question unanswered...why doesn't tablelayout respect the preferred size of its content and why isn't that information propagated up the container chain to the top level container?

Shai Almog

unread,
Sep 27, 2016, 1:19:20 AM9/27/16
to CodenameOne Discussions, howud...@gmail.com
I very specifically said content and not content pane. Manipulating the preferred size of the dialog content pane is problematic as Dialog IS A Form.
Table layout does calculate size based on the preferred size of its content.

howud...@gmail.com

unread,
Sep 27, 2016, 1:23:34 AM9/27/16
to CodenameOne Discussions, howud...@gmail.com
Then why doesn't this work?

protected TextArea taNotes = new TextArea() {


 
@Override
 
protected Dimension calcPreferredSize() {
 
return new Dimension(800, 500);
 
}
 
 
};

That is specifically setting the size of the content.

Neither does this work:
taNotes.setPreferredSize(new Dimension(800,500));

That is also specifically setting the preferred size of the content.

Shai Almog

unread,
Sep 28, 2016, 12:06:11 AM9/28/16
to CodenameOne Discussions, howud...@gmail.com
I'm guessing that doesn't work because you didn't place it in the center of a border layout. BorderLayout implicitly disables scrollability and sizes based on preferred size.
If you placed it in the dialog as is without changing the layout or disabling scrolling then the scrolling might impact this.
Reply all
Reply to author
Forward
0 new messages