How to select a dialog box

Source: Internet
Author: User

This article briefly introduces how to implement the dialog box Selection Function in j23. you can use the dialog function in win32API to implement the dialog box and block the waiting for the return value, and then execute different processing based on the return value, I believe this article will surely help you gain some benefits.

Select a dialog box for the j2-based implementation

One of the difficulties in developing and using a cell phone on such a small screen is frequent screen switching. Although the UI part of midp2.0 is rich, these UI parts are Based on Event Callback. This is insufficient for handling a large number of basic Q & A interactions.

This article implements a dialog box blocking the current thread. In short, you can use the dialog function in win32API to implement the dialog box and block the waiting for the return value, then perform different processing based on the returned value.

What are the doubts?

First, let's review the midpUI event processing mechanism. There are two elements:

1) First, the UI is maintained by a system thread. That is to say, the essentials for handling system events cannot be blocked.

2) event processing uses a callback mechanism. First, the UI component uses methods such as setCommandListener to register a callback interface for itself. The callback essentials are implemented by the user). When the corresponding event is triggered, the callback essentials of the registered interface are called.

The following is a code snippet that implements the CommandListener class:

 
 
  1. Formf=newForm("Helloworld");  
  2. f.addCommand(exit);  
  3. f.setCommandListener(this); 

It can be imagined that the Event Callback-based processing method cannot handle a large number of basic Q & A interactions. You have to implement one class after another for every dialog box that only asks whether or not to continue, or process a complex callback function. If you select the latter, the code snippets that process different logic events in one if-else will be annoying to you.

Good Practice

At this time, we will miss the processing method of the dialog box functions in win32Api:

 
 
  1. intchoose=Dialog(title,type……);  
  2. if(choose==OK){……}  
  3. elseif(choose==Cancel){……} 

This will block the current thread, wait for the returned value, and then execute the Processing Based on the returned value. The reason for cool is that in a logically complete task, you can complete all the logic in One callback essentials at a time, instead of jumping between different classes to ask basic YES/NO questions.

How to implement it in MIDP

The first question we encountered was that we had to block the current thread and wait for the returned value. That is to say, this dialog box cannot be directly run in the UI callback, such as in commandAction. The solution is to put all event processing in a Thread class for processing. This is an extra burden, but cannot be prevented ). Fortunately, this workload is not large, and it is not difficult to implement communication between two objects.

The second question is how to block the current thread. We offer the Java thread's three-pronged wait ()/policyall (). We can specify a semaphore Initial Value false). When it is false, the current thread is blocked. After receiving the user notification, the semaphore is changed to true and the thread is awakened.


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.