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
Who is Participating?
jimmackConnect With a Mentor Commented:
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.

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,
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:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.