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 8287901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:09:59+00:00 2026-06-08T12:09:59+00:00

Is there any reason why in LWUIT a Button can have its own ActionListener

  • 0

Is there any reason why in LWUIT a Button can have its own ActionListener (via button.addActionListener) while a Command does not?

Is the only way to have a listener for a specific command is to add an ActionListener to a form and check the listener for which Command the event came from like below?

    public void startApp() {
    Display.init(this);
    f = new Form("Mixed Record");
    exit = new Command("Exit");
    start = new Command("Start");
    Button button = new Button("Button");

    f.addCommand(exit);
    f.addCommand(start);
    f.addCommand(delete);
    f.addComponent(button);

    f.addCommandListener(new ActionListener() {

        public void actionPerformed(ActionEvent ae) {
            if (ae.getCommand().equals(exit)) {
                //Do Exit command code
            } else if (ae.getCommand().equals(start)) {
                //Do Start command code
            }
        }
    });

    button.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent ae) {
            //Do button code
        }
    });

    f.show();
}
  • 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-06-08T12:10:00+00:00Added an answer on June 8, 2026 at 12:10 pm

    Well, I can’t tell you exactly why the people who wrote LWUIT made that decision but there are several reasons why it makes sense.

    When a Form contains several Commands, they are grouped in a menu. Every time the user expands then collapses the menu, a maximum of one Command is executed. As such, the Commands are conceptually more linked to one another than Buttons are, especially since it is not uncommon to reuse Button subclasses from one Form to another.

    There might also have been a concern about making the API of the LWUIT Form look a lot like the LCDUI Form in the MIDP specification.

    I also like that your code shows one positive consequence of the decision:

    You already have 2 unnamed inner classes (the ActionListener subclasses) in your code. If each Command had its own ActionListener, you would probably have written 3 unnamed inner classes. Developers tend to do that a lot even though, when you have spent a bit more time looking at stack traces of code that contains multiple unnamed inner classes, you will realize that it is bad practice to have more than one in each named class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any reason why this loop is getting stuck? I can't quite get
Is there any reason to not include pdb files in an installer? I have
Is there any reason to NOT have a webpage retrieve it's main content on
Is there any reason that I would be missing the java.util.HashMap package? I have
Is there any reason why this would send duplicate emails? I have a PHP
Is there any reason why I can't use preg_replace inside a static function? when
Is there any reason why I can't just use Guice for DI inside a
Is there any reason why SELECT * FROM MyTable WHERE [_Items] LIKE '*SPI*' does
Is there any reason to use one over the other? Do they have the
is there any reason this chain does not work? It does not add the

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.