Redirect to the url in j2me - java-me

I want to redirect to the given url:
http://www.silvertouch.mobi/mynino/ninoparent/paypalPayment.php?
by clicking the button in the canvas page. How can it be possible to write platformRequest(url) in other canvas file?

Have you tried passing the midlet reference to the canvas and then calling the midlet processRequest(Url) method in pointerPressed?
public class MyCanvas extends Canvas
{
private MIDLet midlet;
public MyCanvas(MIDlet midlet)
{
this.midlet = midlet;
}
...
public pointerPressed(int x, int y)
{
midlet.processRequest(myUrl);
}
}

platformRequest() is on the MIDlet class. Either pass the class to your canvas, or do something like this:
public class MyMIDlet extends MIDlet {
public static MIDlet m;
protected void startApp() throws MIDletStateChangeException {
MyMIDlet.m = this;
... other initialisation stuff
}
}
Then platformRequest() can be called anywhere via MyMIDlet.m.platformRequest().

Related

JavaFX access ui-elements from Controller(Singleton)

I have a javafx design in the file javafx.fxml where the root element has the following attribute
fx:controller="de.roth.jsona.javafx.ViewManagerFX"
This controller class has a singleton machanism and is binded with some ui-elements.
public class ViewManagerFX {
private static ViewManagerFX instance = new ViewManagerFX();
#FXML
private Slider volumeSlider;
#FXML
private Label volumeLabel;
public IntegerProperty volumeValue = new SimpleIntegerProperty();
#FXML
private TabPane musicTabs;
public List<StringProperty> tabNames = new ArrayList<StringProperty>();
public static ViewManagerFX getInstance() {
return (instance);
}
public void initialize() {
// Volume
volumeSlider.valueProperty().bindBidirectional(volumeValue);
volumeLabel.textProperty().bindBidirectional(volumeValue, new Format() {
#Override
public StringBuffer format(Object obj, StringBuffer toAppendTo,
FieldPosition pos) {
toAppendTo.append(obj);
toAppendTo.append("%");
return toAppendTo;
}
#Override
public Object parseObject(String source, ParsePosition pos) {
return null; // no need to be implemented
}
});
volumeValue.set(Config.getInstance().VOLUME);
}
public void addMusicFolderTab(final String t, final ArrayList<MusicListItem> items) {
Platform.runLater(new Runnable() {
#Override
public void run() {
Tab m = new Tab("Test Tab");
musicTabs.getTabs().add(0, m);
}
});
}
}
The method addMusicFolderTab is called from a thread that is used to scan files and directories.
In the initialize method I can access the ui-elements but in the method addMusicFolderTab, that is called from the filescanner-thread, the variable musicTabs is null. Here is the exception:
java.lang.NullPointerException
at de.roth.jsona.javafx.ViewManagerFX$3.run(ViewManagerFX.java:110)
I have no clue, why I can't access the TabPane from outside the initialize method.
Aside from the many questionable patterns used here, the problem is that your ViewManagerFX singleton (besides not being a singleton) never has its instance set.
When using FXML, the Controller is created and loaded dynamically by Reflection from the FXMLoader.
What happens is that by calling ViewManagerFX.getInstance(), you access the a different controller than the one created by the FXMLoader. The instance you access is the one created here:
private static ViewManagerFX instance = new ViewManagerFX();
The quickest way to solve the issue is to set the instance in the initialize() since it's called by the FXMLoader on the instance created by the FXMLoader.
public void initialize() {
instance = this;
// Volume
...
}

Using a Command to switch to a Displayable in a class that extends MIDlet

I'm working in java me. I'm trying to switch between visual designs using ok Commands and back Commands. I have a form displayable which I named formA in my main class A.java and a formB in another class B.java . I used an ok Command in formA which on selection, is supposed to take the user to formB.
I created a reference to B.java in my main class A.java constructor
B b;
// A.java constructor
public A() {
b = new B(this);
}
now I could call the getFormB method from my commandAction in formA. Then I added a backCommand which is supposed to take me back to formA in A.java and I tried creating a reference in B.java same way I did in A.java but I get a SecurityException MIDletManager ERROR at runtime. I was adviced to add an A attribute to my B class and receive the instance as a constructor parameter so I can call the getFormA() method to switch to formA in A.java
A a;
B(A a) {
this.a = a;
}
in command action I did ds on the backCommand:
switchDisplayable ( null , a.getFormA());
This compiled, but at runtime on hitting the BACK key from formB I get java/lang/NullPointerException.
Can anyone tell me why this happended and how to fix it please. All I'm trying to acheive is the backCommand to take the user back to formA from formB
If your A class extends Form or your A class is Displayable, then in the Back command, you can just tell switchDisplayable(null, a).
If your A class is not a Form, then make sure your A class has the following methods:
public Form getFormA() {
return ...; // return the `Form` here so you will not get NullPointerException
}
UPDATE:
If you're using NetBeans, you can open Flow tab and drag backCommand from formB to formA. NetBeans will generate the required code for you.
If you code by hand, then it will looks like the following:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class ExampleMidlet extends MIDlet {
private Display display;
private Form formA;
private Form formB;
private Command formA_next;
private Command formB_back;
public void startApp() {
if (display==null) {
display = Display.getDisplay(this);
formA = new Form("Form A");
formA_next = new Command("Next", Command.SCREEN, 0);
formA.addCommand(formA_next);
formA.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c==formA_next) {
display.setCurrent(formB);
}
}
});
formB = new Form("Form B");
formB_back = new Command("Back", Command.BACK, 0);
formB.addCommand(formB_back);
formB.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c==formB_back) {
display.setCurrent(formA);
}
}
});
}
display.setCurrent(formA);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
I don't know how you code your Form, but it seems that a is null. Maybe you can show me the full code. Passing this in constructor is generally not recommended. By the way, you still need a 'main' class that extends MIDlet right? Then there will be 3 classes, such as:
ExampleMiddlet.java (this is where you put your MIDlet lifecycle, such as startApp(), pauseApp(), etc):
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class ExampleMidlet extends MIDlet {
private Display display;
private Form formA, formB;
public void startApp() {
if (display==null) {
display = Display.getDisplay(this);
formA = new FormA(this);
formB = new FormB(this);
}
display.setCurrent(formA);
}
public Form getFormA() {
return formA;
}
public Form getFormB() {
return formB;
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
FormA.java (this is where you put the content of your Form):
import javax.microedition.lcdui.*;
public class FormA extends Form {
private Command cmdNext;
public FormA(final ExampleMidlet midlet) {
super("Form A");
append("This is form A.");
cmdNext = new Command("Next", Command.SCREEN, 0);
addCommand(cmdNext);
setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
Display.getDisplay(midlet).setCurrent(midlet.getFormB());
}
});
}
}
FormB.java (this is where you put the content of your Form):
import javax.microedition.lcdui.*;
public class FormB extends Form {
private Command cmdBack;
public FormB(final ExampleMidlet midlet) {
super("Form B");
append("This is form B.");
cmdBack = new Command("Back", Command.SCREEN, 0);
addCommand(cmdBack);
setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
Display.getDisplay(midlet).setCurrent(midlet.getFormA());
}
});
}
}

Canvas switching in J2ME

I have a class which extends canvas.I created one more canvas class. But I couldn't switch between them.
Is it possible to switching between canvases in J2ME?
import ...
public class MyMIDlet extends MIDlet{
...
final Canvas1 c1;
final Canvas2 c2;
...
public MyMIDlet(){
c1 = new Canvas1(this);
c2 = new Canvas2(this);
}
...
}
import ...
public class Canvas1 extends Canvas implements CommandListener{
MyMIDlet myMidlet;
Display disp;
Command switchDisp;
...
/**
*constructor
*/
public Canvas1(MyMIDlet myMidlet){
this.MyMIDlet = myMidlet;
disp = myMidlet.getDisplay();
switchDisp = new Command("switch", Command.SCREEN, 0);
this.addCommand(switchDisp);
this.setCommandListener(this);
}
...
public void paint(Graphics g){
g.setColor(255,255,255);
g.drawString("canvas1", 0, 0, 0);
}
...
public void commandAction(Command cmd, Displayable displayable){
disp.setCurrent(myMidlet.c2);
}
}
import ...
public class Canvas2 extends Canvas implements CommandListener{
MyMIDlet myMidlet;
Display disp;
Command switchDisp;
...
/**
*constructor
*/
public Canvas1(MyMIDlet myMidlet){
this.MyMIDlet = myMidlet;
disp = myMidlet.getDisplay();
switchDisp = new Command("switch", Command.SCREEN, 0);
this.addCommand(switchDisp);
this.setCommandListener(this);
}
...
public void paint(Graphics g){
g.setColor(255,255,255);
g.drawString("canvas2", 0, 0, 0);
}
...
public void commandAction(Command cmd, Displayable displayable){
disp.setCurrent(myMidlet.c1);
}
}
Displayable object is an object that has the capability of being placed on the display. A Displayable class implements Displayable interface.
The Display class is the display manager that is instantiated for each active MIDlet and provides methods to retrieve information about the device's display capabilities. A canvas is made visible by calling the Display.setCurrent() method.
A canvas implements the Displayable interface.
A Displayable class is a UI element that can be shown on the device's screen while the Display class abstracts the display functions of an actual device's screen and makes them available to you. It provides methods to show or change the current UI element that you want displayed. Thus, a MIDlet shows a Displayable UI element on a Display using the setCurrent(Displayable element) method of the Display class.

How to detect key pressed event in LWUIT form?

I have written simple j2me program with LWUIT package.I have added one Form in my MIDLET class file. Suppose,user press a key then I want to show another Form.But I couldn't be able to capture key event in my LWUIT Form.
This is my code snippt
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
public class MultipleForm extends MIDlet implements ActionListener{
private Form mFirstForm, mSecondForm;
public void startApp()
{
if (mFirstForm == null)
{
Display.init(this);
mFirstForm = new Form("First Form");
Button button = new Button("Switch");
button.addActionListener(this);
mFirstForm.addComponent(button);
mSecondForm = new Form("Second Form");
Button button2 = new Button("Switch");
button2.addActionListener(this);
mSecondForm.addComponent(button2);
mFirstForm.show();
}
}
protected void keyPressed(int key)
{
System.out.println("Key Pressed");
if(key==52)
{
Form current = Display.getInstance().getCurrent();
if (current == mFirstForm)
{
mSecondForm.show();
}
else if(current==mSecondForm)
{
mFirstForm.show();
}
}
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
}
To capture the event key in a LWUIT Form you need to use Form.addGameKeyListener(here the key, here actionListener)
The keys are mapped using Canvas like Canvas.FIRE for example.
Try to do that.

In lWUIT, How to call main MIDlet class by click on back command?

My question is how to call main MIDlet class by clicking on back command?
Suppose MainMIDlet.java this class extends Form and implements ActionListener and Aboutus.java this class extends also include Form with implements ActionListener. In this class I had not created object of form. So in this class how to call MainMIDlet class when click on Commmand back button?
Pass the MainMIDlet form instance when you call the Aboutus.java.
For example,
MainMIDlet.java
public class MainMIDlet extends MIDlet implements ActionListener {
Form form = new form();
...
...
public void actionPerformed(ActionEvent ae)
{
Command cmd = ae.getCommand();
String cmdname= cmd.getCommandName();
if (cmdname.equals("Aboutus"))
{
Aboutus aboutus = new Aboutus(form); // pass the current form
aboutus.show();
}
}
}
Aboutus.java
public class Aboutus extends Form implements ActionListener {
Form mainform;
public Aboutus(Form form) {
this.mainform = form;
...
...
Command backCommand = new Command("Back",null,1);
this.setBackCommand(backCommand);
}
...
...
public void actionPerformed(ActionEvent ae)
{
Command cmd = ae.getCommand();
String cmdname= cmd.getCommandName();
if (cmdname.equals("Back"))
{
mainform.showBack(); // show the Main Midlet form here
}
}
}

Resources