• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 746
  • Last Modified:

How to horizontally and vertically expand CustomItem?

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
  • 3
1 Solution
Screen width and height can be obtained from a Canvas object.  Once you have these values, you could pass them into your new object.
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,
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.

UrosVidojevicAuthor Commented:
Thank you, I'm wondering what is the purpose of LAYOUT_EXPAND & LAYOUT_VEXPAND constants. :-)
They're used to provide some hints to the system when it draws the item.
Have a look here: http://developers.sun.com/mobility/midp/ttips/customitem/

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now