Solved

Back and forth between forms

Posted on 2009-05-17
1
1,774 Views
Last Modified: 2013-11-24
I'm totally novice to J2ME, but have some experience with programming in other langs.
I've developed a simple application that reads four user inputs (main form) and show the result of some calculations to a new form (result form). Everything works as expected.  When i hit the back command button in the "result form" I'm back to the main form. the problem is when I enter new values, i get the same old result no matter what the entered data!. I tried to call a method to clear the result form but didn't work. Is there some magical trick I'm not aware of that's causing this problem ??
I'm using netbeans as an IDE.
I'm attaching the code, it is large but hope it helps
tried to provide more points but couldn't. it wouldn't allow
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.alatebaa.calc;
 
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.lang.String;
import java.lang.Double;
 
/**
 * @author Mohamed
 */
public class VisualMIDlet extends MIDlet implements CommandListener {
 
    private boolean midletPaused = false;
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">
    private Form Main;
    private TextField tf_qn;
    private TextField tf_vol;
    private TextField tf_dose;
    private TextField tf_wt;
    private TextField tf_dropsPerMl;
    private Form Result;
    private StringItem si_mlPerH;
    private StringItem si_dropsPerMin;
    private Command okCommand;
    private Command okCommand1;
    //</editor-fold>
    private double wt;
    private double dose;
    private double qn;
    private double vol;
    private double dropsPerMl;
    private double mlPerH;
    private double dropsPerMin;
 
    /**
     * The VisualMIDlet constructor.
     */
    public VisualMIDlet() {
    }
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">
    //</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {
        // write pre-initialize user code here
 
        // write post-initialize user code here
    }
    //</editor-fold>
 
    public double getMlPerH() {
        return this.mlPerH;
    }
 
    public double getDropsPerMin() {
        return this.dropsPerMin;
    }
 
    public void calculate() {
        try {
            double mcg = 1000;
            double min = 60;
 
            this.wt = Double.parseDouble(this.tf_wt.getString());
            this.dose = Double.parseDouble(this.tf_dose.getString());
            this.dropsPerMl = Double.parseDouble(this.tf_dropsPerMl.getString());
            this.qn = Double.parseDouble(this.tf_qn.getString());
            this.vol = Double.parseDouble(this.tf_vol.getString());
 
            double conc = this.qn / this.vol;
 
            double calculatedDose = (this.dose * this.wt * min) / mcg;
//(double) (int)((this.resultMlPerHour+0.005)*100.0)/100.0;
            this.mlPerH = (double) (int) (((calculatedDose / conc) + 0.005) * 100.0) / 100.0;
 
            this.dropsPerMin = (double) (int) ((((this.mlPerH / min) * this.dropsPerMl) + 0.005) * 100.0) / 100.0;
 
 
 
 
        } catch (Exception koko) {
            this.exitMIDlet();
        }
 
    }
 
    public void update() {
        this.si_mlPerH.setText(Double.toString(this.mlPerH)+"\r");
        this.si_dropsPerMin.setText(Double.toString(this.dropsPerMin)+"\r");
    }
    
    public void clearResultForm() {
    this.si_dropsPerMin.setText(" ");
    this.si_mlPerH.setText(" ");
    }
    
    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {
        // write pre-action user code here
        switchDisplayable(null, getMain());
    // write post-action user code here
    }
    //</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {
        // write pre-action user code here
 
        // write post-action user code here
    }
    //</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
        // write pre-switch user code here
        Display display = getDisplay();
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }
    // write post-switch user code here
    }
    //</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {
        // write pre-action user code here
        if (displayable == Main) {
            if (command == okCommand) {
                // write pre-action user code here
                //this.calculate();
 
 
                switchDisplayable(null, getResult());
            // write post-action user code here
            }
        } else if (displayable == Result) {
            if (command == okCommand1) {
                // write pre-action user code here
                //this.clearResultForm();
                switchDisplayable(null, getMain());
            // write post-action user code here
            }
        }
    // write post-action user code here
    }
    //</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Main ">
    /**
     * Returns an initiliazed instance of Main component.
     * @return the initialized component instance
     */
    public Form getMain() {
        if (Main == null) {
            // write pre-init user code here
            Main = new Form("Main", new Item[] { getTf_qn(), getTf_vol(), getTf_dose(), getTf_wt(), getTf_dropsPerMl() });
            Main.addCommand(getOkCommand());
            Main.setCommandListener(this);
        // write post-init user code here
        }
        return Main;
    }
    //</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Result ">
    /**
     * Returns an initiliazed instance of Result component.
     * @return the initialized component instance
     */
    public Form getResult() {
        if (Result == null) {
            // write pre-init user code here
            Result = new Form("Result", new Item[] { getSi_mlPerH(), getSi_dropsPerMin() });
            Result.addCommand(getOkCommand1());
            Result.setCommandListener(this);
            // write post-init user code here
            this.clearResultForm();
            this.calculate();
            this.update();
        }
        return Result;
    }
    //</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {
            // write pre-init user code here
            okCommand = new Command("Calc", Command.OK, 0);
        // write post-init user code here
        }
        return okCommand;
    }
    //</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand1 ">
    /**
     * Returns an initiliazed instance of okCommand1 component.
     * @return the initialized component instance
     */
    public Command getOkCommand1() {
        if (okCommand1 == null) {
            // write pre-init user code here
            okCommand1 = new Command("Back", Command.OK, 0);
        // write post-init user code here
        }
        return okCommand1;
    }
    //</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: tf_qn ">
    /**
     * Returns an initiliazed instance of tf_qn component.
     * @return the initialized component instance
     */
    public TextField getTf_qn() {
        if (tf_qn == null) {
            // write pre-init user code here
            tf_qn = new TextField("Drug Amount (mg)", "", 6, TextField.DECIMAL);
        // write post-init user code here
        }
        return tf_qn;
    }
    //</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: tf_vol ">
    /**
     * Returns an initiliazed instance of tf_vol component.
     * @return the initialized component instance
     */
    public TextField getTf_vol() {
        if (tf_vol == null) {
            // write pre-init user code here
            tf_vol = new TextField("Total volume (ml)", "", 6, TextField.DECIMAL);
        // write post-init user code here
        }
        return tf_vol;
    }
    //</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: tf_dose ">
    /**
     * Returns an initiliazed instance of tf_dose component.
     * @return the initialized component instance
     */
    public TextField getTf_dose() {
        if (tf_dose == null) {
            // write pre-init user code here
            tf_dose = new TextField("Required dose (mcg/Kg/min)", "", 6, TextField.DECIMAL);
        // write post-init user code here
        }
        return tf_dose;
    }
    //</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: tf_wt ">
    /**
     * Returns an initiliazed instance of tf_wt component.
     * @return the initialized component instance
     */
    public TextField getTf_wt() {
        if (tf_wt == null) {
            // write pre-init user code here
            tf_wt = new TextField("Weight (Kg)", "80", 4, TextField.DECIMAL);
        // write post-init user code here
        }
        return tf_wt;
    }
    //</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: tf_dropsPerMl ">
    /**
     * Returns an initiliazed instance of tf_dropsPerMl component.
     * @return the initialized component instance
     */
    public TextField getTf_dropsPerMl() {
        if (tf_dropsPerMl == null) {
            // write pre-init user code here
            tf_dropsPerMl = new TextField("Drops per ml", "15", 3, TextField.DECIMAL);
        // write post-init user code here
        }
        return tf_dropsPerMl;
    }
    //</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: si_mlPerH ">
    /**
     * Returns an initiliazed instance of si_mlPerH component.
     * @return the initialized component instance
     */
    public StringItem getSi_mlPerH() {
        if (si_mlPerH == null) {
            // write pre-init user code here
            si_mlPerH = new StringItem("Ml per hour: ", "");
        // write post-init user code here
        }
        return si_mlPerH;
    }
    //</editor-fold>
 
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: si_dropsPerMin ">
    /**
     * Returns an initiliazed instance of si_dropsPerMin component.
     * @return the initialized component instance
     */
    public StringItem getSi_dropsPerMin() {
        if (si_dropsPerMin == null) {
            // write pre-init user code here
            si_dropsPerMin = new StringItem("Drops per min: ", "");
        // write post-init user code here
        }
        return si_dropsPerMin;
    }
    //</editor-fold>
 
    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }
 
    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }
 
    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }
 
    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }
 
    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
}

Open in new window

0
Comment
Question by:dr_dedo
1 Comment
 
LVL 16

Accepted Solution

by:
dr_dedo earned 0 total points
ID: 24412824
I found the answer. the visual editor creates the result form only once, as you see in line 199, if result for is already present, it doesn't create a new one, hence, all the old stuff are shown again
i did a couple of things to work around this
1- re positioned calculate, update and clearform methods to more appropriate locations
2- changed clearForm method to this
 public void clearResultForm() {
        if (this.Result != null) {
            this.si_dropsPerMin = null;
            this.si_mlPerH = null;
            this.Result = null;
        }
    }

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
replace a word with other 1 44
servlet example issue 6 38
eclipse apache tomcat admin console 52 96
Convert from a json string array to a Java object 3 30
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question