Displaying Alert with Yes, No Command - java-me

At the J2me application I used an alert with yes, no command. If user clicks the yes command Form Screen will be displayed and if clicks the no command TextBox screen will be displayed. But the code does not work. For two command only textbox screen will be displayed.
This is my code:
public Login(){
yes=new Command("Yes",Command.OK,1);
no=new Command("No",Command.CANCEL,1);
alert=new Alert("","Save The Changes?",null,AlertType.CONFIRMATION);
alert.setTimeout(Alert.FOREVER);
alert.addCommand(yes);
alert.addCommand(no);
textbox.setCommandListener(this);
alert.setCommanListener(this);
}
public void commandAction(Command command, Displayable displayable) {
if(displayable==textbox)
{
if(command==exit)
{
switchDisplayable(null,alert);
}
}
else if(displayable==alert)
{
if(command==no)
{
switchDisplayable(alert,getForm());
}
else if(command==yes)
{
switchDisplayable(alert,getTextbox());
}
}
}
Where is my fault?

Your main fault here is I think not using appropriate logging in your MIDlet. Other than that, there are no evident mistakes in the code snippet you posted.
It is most likely that the error is caused by something going wrong in your getForm() method code, but since there is no logging, you have to also check other possibilities like eg that command listener or no command object, or alert object has been somehow changed somewhere else in your code.
With logging like shown in example below, you could simply run your midlet in emulator and check console messages to find out whether expected code has been executed or not:
public void commandAction(Command command, Displayable displayable) {
Log.log("command: [" + command.getCommandLabel()
+ "] at screen: [" + displayable.getTitle() + "]");
if(displayable==textbox)
{
Log.log("in textbox");
if(command==exit)
{
Log.log("handle exit command");
switchDisplayable(null,alert);
}
}
else if(displayable==alert)
{
Log.log("in alert");
if(command==no)
{
Log.log("handle no command");
switchDisplayable(alert,getForm());
}
else if(command==yes)
{
Log.log("handle yes command");
switchDisplayable(alert,getTextbox());
}
}
}
//...
public class Log {
// utility class to keep logging code in one place
public static void log (String message) {
System.out.println(message);
// when debugging at real device, S.o.p above can be refactored
// - based on ideas like one used here (with Form.append):
// http://stackoverflow.com/questions/10649974
// - Another option would be to write log to RMS
// and use dedicated MIDlet to read it from there
// - If MIDlet has network connection, an option is
// to pass log messages over the network. Etc etc...
}
}

Related

C++ CLI Invoke issues

I have a MainForm class (as you'd expect, it is a form) that has a text box on it. I also have another class called 'Application_Server' That does a load of other stuff (not just form-background related, quite a lot of network based stuff etc.).
The Application_Server class runs in it's own thread, but needs to be able to update the controls on the form, for this question, we will stick with just the textbox.
The problem is that even though I am executing the command to set the text of the textBox control via 'Invoke' I am still getting the following exception during runtime:
Additional information: Cross-thread operation not valid: Control
'DebugTextBox' accessed from a thread other than the thread it was
created on.
What could be causing this? I am definitely invoking a delegate within MainForm.
Here are the relevant code segments (cut down for readability):
MainForm.h:
public ref class MainForm : public System::Windows::Forms::Form {
delegate void del_updateDebugText(String^ msg);
del_updateDebugText^ updateDebugText = gcnew del_updateDebugText(this, &MainForm::postDebugMessage);
private: void postDebugMessage(String^ message);
};
MainForm.cpp:
void EagleEye_Server::MainForm::postDebugMessage(String^ message)
{
Monitor::Enter(DebugTextBox);
if (this->DebugTextBox->InvokeRequired)
{
this->Invoke(updateDebugText, gcnew array<Object^> { message });
}
else
{
this->DebugTextBox->AppendText(message);
}
Monitor::Exit(DebugTextBox);
}
And finally, the code calling it:
void ServerAppManager::postDebugMessage(System::String^ message)
{
mainFormHandle->updateDebugText(message);
}
void ServerAppManager::applicationStep()
{
postDebugMessage("Starting\n");
// This is Run in seperate thread in MainForm.cpp
while (s_appState == ApplicationState::RUN)
{
postDebugMessage("Testing\n");
}
}
Thanks!
From background worker called bwSearch we do the call as following from the DoWork event handler:
private: System::Void bwSearch_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {
//... logic
UpdateTxtOutput("Some message");
//... more logic
}
I have a RitchTextBox called txtOutput, also the windows form control containing this code is called frmMain, the UpdateTxtOutput is defined in three parts as follows:
delegate void UpdateTxtOutputDelegate(String^ text);
void UpdateTxtOutput(String^ text)
{
UpdateTxtOutputDelegate^ action = gcnew UpdateTxtOutputDelegate(this, &frmMain::Worker);
this->BeginInvoke(action, text);
}
void Worker(String^ text)
{
txtOutput->AppendText("\t" + text + "\n");
}
I managed to get it working by simplifying the method within the 'MainForm' class to:
void EagleEye_Server::MainForm::postDebugMessage(String^ message)
{
Monitor::Enter(DebugTextBox);
DebugTextBox->AppendText(message);
Monitor::Exit(DebugTextBox);
}
And then moving the 'Invoke' call to the method calling the delegate, not pretty but it works for now. I think the issue may have been caused by the form getting stuck inside an Invoke loop. I say this as I noticed that the form would lock up and stop responding after it hit the recursive Invoke statement.

JavaFX: Problems to use setOnCloseRequest within a Controller

I want to use setOnCloseRequest after my stage has been started but i get almost a NullPointerException although i set the right controller.
I click on a menuitem to open the stage and after the menu is open i want to use the close button. I think the method tries to access until the controller has been initialized.Actually i want to say: Please controller you are just allowed to do something after you are initialized.
THis is my code of the controller.
if (KundenDatenController.getInstance() != null) {
((Stage) (KundenDatenController.getInstance().kundenControllerPane.
getScene().getWindow())).setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent t) {
t.consume();
if (generalControler.controlEmptyTextField(pflichtfelder)) {
((Stage) (kundenControllerPane.getScene().getWindow())).close();
} else if (!generalControler.controlEmptyTextField(pflichtfelder)) {
FXOptionPane.showConfirmDialog((Stage) (kundenControllerPane.getScene().getWindow()),
"Sollen die Eingaben wirklich verworfen werden?",
"Programm schliessen");
if (status.equals("Laden")) {
KundenUebersichtController.getInstance().setStatus("Aufnehmen");
}
}
}
});

In J2ME, is that possible to operate an Alert dialog box with Yes & NO command?

I have created an Alert dialog box in my J2ME app to alert user when user press exit button to terminate an app and ask user confirmation to exit from app with yes and no command.
When user press Yes button app will terminate and when user press No button app will return to its Main form. To do this I developed a code from scratch which are as follows:
public class CustomAlert extends MIDlet implements CommandListener
{
Alert ExitAlrt;
Display d;
Command MainListSelect, Exit, YesCmdAlrt, NoCmdAlrt;
List MainList;
public CustomAlert()
{
d = Display.getDisplay(this);
//Initialization of commands
MainListSelect = new Command("Select", Command.SCREEN, 1);
Exit = new Command("Exit", Command.STOP, 2);
//Initialization of lists
MainList = new List("Menu", List.IMPLICIT);
//Adding command to lists
MainList.addCommand(MainListSelect);
MainList.addCommand(Exit);
MainList.setCommandListener(this);
//Appending the content of lists
MainList.append("Settings",null);
}
protected void startApp()
{
MainList.setSelectedIndex(0, true);
d.setCurrent(MainList);
}
protected void pauseApp() { }
protected void destroyApp(boolean unconditional){}
//This method handle commands which operate list that is Select & Exit
public void commandAction(Command cmd,Displayable dispable)
{
if(cmd == MainListSelect)
{
int slctindx = MainList.getSelectedIndex();
if(slctindx == 0)
{}
else if(slctindx == 1)
{}
}
if(cmd == Exit)
{
ExitAlrt = new Alert("Application Alert","Are you sure you want to exit?",null, AlertType.WARNING);
YesCmdAlrt = new Command("Yes", Command.EXIT,1);
ExitAlrt.addCommand(YesCmdAlrt);
NoCmdAlrt = new Command("No", Command.SCREEN,2);
ExitAlrt.addCommand(NoCmdAlrt);
d.setCurrent(ExitAlrt);
}
}
//This Code handle Commands present on Alert dialog box.
public void commandAction(Command cmd) /
{
ExitAlrt.setCommandListener(this);
if(cmd == NoCmdAlrt)
{
d.setCurrent(MainList);
}
else if(cmd == YesCmdAlrt)
{
destroyApp(true);
notifyDestroyed();
}
}
}
In above code problem is when I click on Exit button, Alert box appears and when I press Yes button to terminate an app it again redirect to me on Main List of an app. I did lot of placements in code but problem remain constant.
What is solution for this?
ExitAlert in the posted code snippet lacks a command listener because you didn't invoke setcommandListener for it. As a result, instead of expected exit, default command action happens which is to simply dismiss the alert, as explained in API javadocs:
If the user invokes a Command and the default listener is present, the default listener ignores the Command and implements the automatic-advance behavior.
Note you might think that ExitAlrt.setCommandListener(this) inside commandAction(Command cmd) method does the trick for you but this is not so, because this method is not invoked in between creation of the ExitAlrt instance and displaying it.
To get the desired behavior, implement and set an appropriate command listener for ExitAlrt prior to invoking setCurrent.
// ...
if(cmd == Exit)
{
System.out.println("Exit command invoked"); // log message for debugging
Alert ExitAlrt = new Alert("Application Alert",
"Are you sure you want to exit?", null, AlertType.WARNING);
ExitAlrt.addCommand(new Command("Yes", Command.EXIT, 1));
ExitAlrt.addCommand(new Command("No", Command.SCREEN, 2));
// --> set command listener for ExitAlrt prior to invoking setCurrent
ExitAlrt.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
System.out.println("command: [" + c.getCommandLabel()
+ "] at screen: [" + d.getTitle() + "]"); // for debugging
if (c.getCommandType() != Command.EXIT) {
System.out.println("Exit cancelled"); // for debugging
d.setCurrent(MainList);
return;
}
System.out.println("Exit confirmed"); // for debugging
destroyApp(true);
notifyDestroyed();
}
});
d.setCurrent(ExitAlrt);
}
// ...
For simplicity, above code snippet uses System.out.println for logging. If needed, refer to another SO question for an explanation and example of how this could be done in a more practical way.

org.eclipse.swt.browser.Browser does not open in Eclipse RAP application

Wonder if somebody can help me with this. I am trying to open an embedded browser in an Eclipse RAP applications. All examples I have seen look something like:
link.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent event) {
try {
Browser b = new Browser(parent, SWT.NONE);
b.setText("<html><body>This is Unicode HTML content from memory</body></html>");
} catch (SWTError e) {
// Error handling here
}
}
});
That doesn't do anything (visually) though. When I replace the Browser with ExternalBrowser like so:
link.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent event) {
try {
int browserStyle = ExternalBrowser.LOCATION_BAR;
ExternalBrowser.open( "myPage", "http://www.stackoverflow.com", browserStyle );
} catch (SWTError e) {
// Error handling here
}
}
});
It works. Although not exactly as desired.
I am using Eclipse RCP 1.4.2 on OS X 10.8.2.
Any insight is highly appreciated.
When you create a new widget, you have to trigger a re-layout to make it visible. Depending on your layout, it may be sufficient to call parent.layout(). If the parent is also contained in a layout and shrunken to its preferred size, you will have to call layout() on its parent. If unsure, layout the top-level shell.

disabling sound in a single application in blackberry

I just did a dictionary application in blackberry along with a speech to text conversion support .Everything is working fine. Now i wanted to disable the sound when the user needs So how can i do it programmatically .Please help me
Try this
use the flag value as reference
if flag value is true then user click on item then it will play the sound
else sound wont play and display one dialog that Do you want enable sound with two options yes or no
if user click on yes then make flag value as true and item.setText("Voice Disable"); otherwise no action means no changes in flag
in your list item click listener write condition as following
if(flag==true)
{
write your logic to play
}
sample code is
public class app extends UiApplication{
public static void main(String[] args) {
new app().enterEventDispatcher();
}
public app() {
pushScreen(new SampleScreen());
}
}
class SampleScreen extends MainScreen
{
static boolean flag=true;
MenuItem item=null;
public SampleScreen() {
// use the flag value as reference
// if flag value is true then user click on item then it will play the sound
// else sound wont play and display one dialog that Do you want enable sound with two options yes or no
// if user click on yes then make flag value as true and item.setText("Voice Disable"); otherwise no action means no changes in flag
// in your list item click listner write condition as following
// if(flag==true)
// {
// write your logic to play
// }
// you already implement
item=new MenuItem("Voice Disable",0,100) {
public void run() {
if(flag)
{
flag=false;
item.setText("Voice Enable");
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("Voice Disable succesfully");
}
});
}else{
flag=true;
item.setText("Voice Disable");
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("Voice Enable succesfully");
}
});
}
}
};
addMenuItem(item);
}
}

Resources