I get trouble when I created one form and one midlet and I want through an alert for user when user enters detail, but I got trouble it can't change screen to show alert
nothing can be easier with the code provided in rev 4 of your question
// ... your AddReview class
public AddReview(String title, MainFoodie mf) {
super(title);
this.mf = mf; // without that you'll get NPE in changeSreen
// ...
}
//...
private void changeScreen(Alert a, Form f){
Display.getDisplay(mf).setCurrent(a, f);
}
public void commandAction(Command c, Displayable d) {
if(c.getLabel().equals("Save")){
// how to show alert here:
changeScreen(cfmAlert, this)
}
Related
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.
I am making an application using LWUIT.
There is a form
There is a list embedded on the form.
The list has 5 elements.
Initially, when I first load the app, if I choose the 1st element, 2nd gets chosen; when I choose the second the 3rd gets chose and and so on (Weird!)
I am not able to click any button on the screen either
next what I do is, shift to a different from using arrow keys (of the keyboard... I am running the app on a simulator btw)
Then I come back to the first form and now everything works as expected(no weird behaviour).
What could be the issue?
I am using Sun Java Micro Edition SDK 3.0 (default touch screen for testing)
My code is:
List dummy = new List();
dummy.addItem("wewerwer");
dummy.addItem("wewerdswer");
dummy.addItem("wewqweerwer");
dummy.addItem("dscxwewerwer");
dummy.addItem("jhgwewerwer");
mainListForm.setLayout(new BorderLayout());
mainListForm.addComponent(BorderLayout.CENTER,dummy);
mainListForm.show();
What could possible be going wrong here?
UPDATE 1
I think there is a bug here. I have attached the complete code below along with the screen shot
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
public class Demo extends MIDlet implements ActionListener {
private Form mForm;
List abc;
public void startApp() {
Display.init(this);
try {
Resources r = Resources.open("/Test.res");
UIManager.getInstance().setThemeProps(r.getTheme(
r.getThemeResourceNames()[0])
);
} catch (Exception e){
System.out.println(e.toString());
}
if (mForm == null) {
Button click = new Button("Press me!");
click.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("I have been pressed");
}
});
abc = new List();
abc.addItem("Str1");
abc.addItem("Str2");
abc.addItem("Str3");
abc.addItem("Str4");
abc.addItem("Str5");
abc.addItem("Str6");
Form f = new Form("Hello, LWUIT!");
abc.addActionListener(this);
f.addComponent(abc);
Command exitCommand = new Command("Exit");
f.addCommand(exitCommand);
f.addCommandListener(this);
f.addComponent(click);
f.show();
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void actionPerformed(ActionEvent ae) {
System.out.println(abc.getSelectedIndex());
}
}
So now when I click on 'Str1' of the list Str2 gets selected and so on.
IDE: Netbeans
Emulator: Default Touch screen phone
On the action event set the list to active again after the event by invoking setHandlesInput(true)
OK....so this is how you resolve it.
After the form is displayed remove the list from the form and again add it to the form and then repaint the form.
Earlier Code
1) form.addComponenet(BorderLayout.center,list);
2) form.show();
Word Around for the problem
1)form.addComponenet(BorderLayout.center,list);
2)form.show();
3)form.setScrollable(false);
I know its kind of strange, but this way the list index selection works smooth for touch screen phones.
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);
}
}
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...
}
}
I am using Java Micro Edition and I am trying to create a simple login form with a record store. When the user enters the details I'd like to check them against the ones stored and then move onto another screen like a welcome area.
I have a feeling it has something to do with the form element and switching between it but I can't seem to get anywhere with google
try this
form = new Form("login");
form.addCommand(getExitCommand());
form.addCommand(getOkCommand());
form.setCommandListener(this);
public void commandAction(Command command, Displayable displayable) {
if (displayable == form) {
if (command == exitCommand) {
exitMIDlet();
} else if (command == okCommand) {
display.setCurrent(getWelcomeForm());
}
} else if (displayable == form1) {
if (command == backCommand) {
// do something else
}
}
}
display is something that should be created in the constructor and also above it i.e.
public class YourMidlet extends MIDlet implements CommandListener {
private Display display;
private Form form1;
private Form form2;
public YourMidlet
{
display = Display.getDisplay(this);
form 1 = new Form("hello form this is form 1");
form 2 = new Form("hello form 2");
display.setCurrent(form1);
}
}
you then do:
display.setCurrent(form2);
to switch to form 2