Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 923087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:08:18+00:00 2026-05-15T19:08:18+00:00

I am trying to build a simple menu-based GUI with J2ME. The menu entries

  • 0

I am trying to build a simple menu-based GUI with J2ME. The menu entries are currently objects of classes derived from the class Button. Is there any way I can:

  1. Replace the text in the button and have an image show instead, sort of an icon?

  2. Make the text and image appear side by side on the same menu bar.

If my question is not clear, please let me know and I will edit it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:08:18+00:00Added an answer on May 15, 2026 at 7:08 pm

    You can create your own Item that looks like a button by extending the CustomItem class.

    This is a working MIDlet with a good MyButton class:

    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.CustomItem;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.lcdui.Image;
    import javax.microedition.lcdui.Item;
    import javax.microedition.lcdui.ItemStateListener;
    import javax.microedition.midlet.MIDlet;
    
    public class TestMidlet extends MIDlet implements ItemStateListener {
        class MyButton extends CustomItem {
            private Image _image = null;
            private boolean _down = false;
            private int _clicks = 0;
    
            public MyButton(Image image) {
                super("");
                _image = image;
            }
    
            // Button's image
            public void setImage(Image image) {
                _image = image;
                repaint();
            }
            public Image getImage() {
                return _image;
            }
    
            // Has the button been clicked?
            public boolean isClicked() {
                if(_clicks>0) {
                    _clicks -= 1;
                    return true;
                }
                return false;
            }
    
            // Is the button currently down?
            public boolean isDown() {
                return _down;
            }
            public void setDown(boolean down) {
                if(_down)
                    _clicks += 1;
                if(down!=_down) {
                    _down = down;
                    repaint();
                    notifyStateChanged();
                }
            }
            public void setDown() {
                setDown(true);
            }
            public void setUp() {
                setDown(false);
            }
    
            // Minimal button size = image size
            protected int getMinContentHeight() {
                return getImage().getHeight();
            }
            protected int getMinContentWidth() {
                return getImage().getWidth();
            }
            // Preferred button size = image size + borders
            protected int getPrefContentHeight(int width) {
                return getImage().getHeight()+2;
            }
            protected int getPrefContentWidth(int height) {
                return getImage().getWidth()+2;
            }
    
            // Button painting procedure
            protected void paint(Graphics g, int w, int h) {
                // Fill the button with grey color - background 
                g.setColor(192, 192, 192);
                g.fillRect(0, 0, w, h);
                // Draw the image in the center of the button
                g.drawImage(getImage(), w/2, h/2, Graphics.HCENTER|Graphics.VCENTER);
                // Draw the borders
                g.setColor(isDown()?0x000000:0xffffff);
                g.drawLine(0, 0, w, 0);
                g.drawLine(0, 0, 0, h);
                g.setColor(isDown()?0xffffff:0x000000);
                g.drawLine(0, h-1, w, h-1);
                g.drawLine(w-1, 0, w-1, h);
            }
    
            // If FIRE key is pressed, the button becomes pressed (down state)
            protected void keyPressed(int c) {
                if(getGameAction(c)==Canvas.FIRE)
                    setDown();
            }
            // When FIRE key is released, the button becomes released (up state)
            protected void keyReleased(int c) {
                if(getGameAction(c)==Canvas.FIRE)
                    setUp();
            }
            // The same for touchscreens
            protected void pointerPressed(int x, int y) {
                setDown();
            }
            protected void pointerReleased(int x, int y) {
                setUp();
            }
        }
    
        MyButton button = null;
    
        public void itemStateChanged(Item item) {
            if(item==button) {
                if(button.isClicked())
                    System.out.print("clicked, ");
                System.out.println(button.isDown()?"currently down":"currently up");
            }
        }
    
        public void startApp() {
            try {
                Form form = new Form("Example");
                button = new MyButton(Image.createImage("/icon.png"));
                form.append(button);
                form.setItemStateListener(this);
                Display.getDisplay(this).setCurrent(form);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
            notifyDestroyed();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.