Link to home
Start Free TrialLog in
Avatar of Ustas
Ustas

asked on

J2ME - Midlet Form - Horizontal Scrolling

Hello All,

Been trying to figure out if J2ME javax.microedition.lcdui.Form allows for horizontal scrolling.

When form items are added to this form, and they exceed the form area, the vertical scrollbar appears automatically. This is not the case however when an ImageItem wider than the form is placed onto it. That is, if say the form on a Nokia mobile with a screen of 125x100 takes precisely those dimensions, and an ImageItem of 200x200 is placed on it. Vertical scrolling appears automatically, but not the horizontal.

Arguements such as horizontal scrolling cannot be there for the MIDP 1.0 standard since not all mobiles have the left and right arrow keys are unfounded, since all the phones have the 0-9 digits out of which 2,8,4,6 keys can do the job as well.

I know I can implement the scrolling myself manually using Canvas, but I will then loose the ability to work with Form items, that is a requirement for me.

Therefore to simplify the whole question I need to know:

1) How to make the javax.microedition.lcdui.Form have a horizontal scrollbar, or a fully qualified reasoning why it is not possible.

2) How to do it otherwise while preserving the ability to use ImageItem and/or other form items.

Regards,
Stas
Avatar of jimmack
jimmack

1) You can't add a horizontal scrollbar to a Form.  The Form class manages it's own layout, has no facilities to modify it's behaviour in such a way and is also implementation specific (it is *possible* that some phones may allow a horizontal scrollbar, but I doubt it.  Even if it was supported, you would have no control over it).

2) You have correctly identified the only way that oversized images can be scrolled on a handset (ie. using a Canvas).  You have also correctly identified the fact that you can't add Item subclasses to a Canvas.

You need to reconsider the user interface.  What is the reason for having oversized images?  Would it make sense (within your application context) to have an option from the form to display the image?  This would allow you to display the image on a Canvas with the scrolling implementation you have suggested.  You could also have an "OK" or "Back" Command on the Canvas to return to the Form.

The only other alternative I can see at the moment would be to have the server do some dynamic image resizing.
Avatar of Ustas

ASKER

That is as far as I arrived.

Any idea on HOW to do the horizontal scrolling in a canvas ?

I need to be able to display images larger than the phone's screen. Actually up to 300 pixels in width, and possibly (phone permitting) even more.

An example of such a scrolling would be in Webviewer application (quite a popular one). That can display large images easily.

Have you seen any implementation of that anywhere on the web ? I am sure I am not the only one with such a problem, and reprogramming that defies the concept of Java that was designed to be object oriented precisely to improve the software development practices and not to reinvent the wheel each time in the first place.

Stas
There seems to be a limit on the size of posts at the moment, so I've had to break this down into two posts for you.

package com.ossltd.scroll;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class Scroller extends MIDlet implements CommandListener
{
    private Display display;
    private Command exitCommand;
    private Command viewCommand;
    private Form mainForm;
    private ScrollCanvas scrollCanvas;
   
    public Scroller()
    {
        exitCommand = new Command("Exit", Command.EXIT, 1);
        viewCommand = new Command("View", Command.SCREEN, 1);
       
        mainForm = new Form("Scroller");
        mainForm.append("Press 'View' to access the scrolling image");
        mainForm.addCommand(viewCommand);
        mainForm.addCommand(exitCommand);
        mainForm.setCommandListener(this);
       
        scrollCanvas = new ScrollCanvas();
        scrollCanvas.addCommand(exitCommand);
        scrollCanvas.setCommandListener(this);
    }
   
    public void startApp()
    {
        display = Display.getDisplay(this);
        display.setCurrent(mainForm);
    }
   
    public void pauseApp()
    {
    }
   
    public void destroyApp(boolean flag)
    {
    }
   
    public void commandAction(Command command, Displayable displayable)
    {
        if (command == viewCommand)
        {
            display.setCurrent(scrollCanvas);
        }
        else if (command == exitCommand)
        {
            destroyApp(true);
            notifyDestroyed();
        }
    }
}
ASKER CERTIFIED SOLUTION
Avatar of jimmack
jimmack

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You'll need to create your own "/images/myImg.png" and put this under the res directory for your MIDlet.
;-)
Avatar of Ustas

ASKER

Amazing!

Thanks for such a quick reply. Last message with scrollCanvas code accepted!

[off] have you been working long on j2me ? [/off]
Originally I worked with KVM on the Palm Pilot (that's probably about 5 years ago).

I've been working seriously with J2ME for about 2 years.