How to horizontally and vertically expand CustomItem?

Posted on 2007-08-12
Last Modified: 2013-11-24
Dear J2ME Experts,

I have written a subclass of CustomItem named MyCustomItem.
I want to add the object of this class to myForm (Form object) so that added MyCustomItem object is maximally horizontally and vertically expanded.
Of course, this MyCustomItem object would be the only item on this form.

This is the code sample that I used:

MyCustomItem myitem = new MyCustomItem();
myItem.setLayout(Item.LAYOUT_EXPAND | Item.LAYOUT_VEXPAND);
MyForm myform = new MyForm();

The problem is that getPrefContentWidth() and getPrefContentHeight() methods of MyCustomItem must be overrided, and it is obvious that J2ME LayoutManager uses this values to calculate dimensions of myitem, and ignores my requests for expanding.

Of course, now,  in the code-writting time I don't know user-screen dimensions so I can't return valid values from this methods.

I would really appreciate any help on this subject,
Thank you in advance,
Uros Vidojevic
Question by:UrosVidojevic
    LVL 15

    Expert Comment

    Screen width and height can be obtained from a Canvas object.  Once you have these values, you could pass them into your new object.
    LVL 5

    Expert Comment

    for that you will have to use Canvas class.
    and from there u can get the valuse of screen-WIDTH and screen-Height

    here is sample code for geting the valus;

    int height = this.getHeight();
    int widht = this.getWidht/();
    then pass these values to ur functons.

    hope your problem is solved now.

    Kind Regards,
    LVL 15

    Accepted Solution

    I think I've changed my mind (I've been working with Canvases a lot recently :-) ).

    The getHeight() and getWidth() methods are defined by the Displayable class.  This class is extended by the Form class as well as the Canvas class, but the values returned from a Form or a Canvas are different.  The values returned by a Form indicate the area available for Items on that Form.  This is probably more appropriate for your application.

    Assuming that the Form you are adding your custom item to is called "myForm", I suggest you add the following code above your existing code:

    int width = myForm.getWidth();
    int height = myForm.getHeight();
    MyCustomItem myitem = new MyCustomItem(width, height);
    myItem.setLayout(Item.LAYOUT_EXPAND | Item.LAYOUT_VEXPAND);

    If you don't already have it, you'll need the following at the top of that file:

    You'll then need to modify your constructor to accept the two int parameters and store those in private attributes that can then be referenced in your getPref.../getMin... methods.

    LVL 7

    Author Comment

    Thank you, I'm wondering what is the purpose of LAYOUT_EXPAND & LAYOUT_VEXPAND constants. :-)
    LVL 15

    Expert Comment

    They're used to provide some hints to the system when it draws the item.
    Have a look here:

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    MaxSpan challenge 9 50
    countClumps  challenge 10 70
    wordsCount challenge 10 49
    triangle challenge 4 38
    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now