HELP: setting variables for basic edit fields
Author |
Message |
DelGee
|
Posted: Tue Nov 16, 2010 8:41 pm Post subject: HELP: setting variables for basic edit fields |
|
|
So basically, i'm writing a Blackberry application for my computer sciences course. I'm the only one doing it for computer sciences at my school so i have no one to collaborate with.
So i'll show u the code and then explain the problem. this is a financial application where the user enters initial investment amount, the set interest rate, the compound period, and the length of the investments in months. im using Eclipse with a blackberry jde plug in for it.
i am using a different class for each screen.
ALSO, there is a lot of repeat code in each class (formatting the page to look good). Please help me out though.
package com.rim.samples.Finance;
import net.rim.device.api.ui.UiApplication;
public class FinanceApplication extends UiApplication
{
//VARIABLES FOR THE INVESTMENT GROWTH
public double initialInvestment;
public double interest;
public int compoundPeriod;
public int investmentLength;
public static void main(String[] args)
{
FinanceApplication firstApplication = new FinanceApplication();
firstApplication.enterEventDispatcher();
}
public FinanceApplication()
{
//OPENING SCREEN FOR INVESTMENT GROWTH
pushScreen(new Screen1());
}
}
//SCREEN 1 INPUTS *INITIAL INVESTMENT*
package com.rim.samples.Finance;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
public class Screen1 extends MainScreen implements FieldChangeListener
{
//DECLARE MENU BUTTONS
protected void makeMenu(Menu menu, int instance)
{
menu.add(_close);
}
private MenuItem _close = new MenuItem("Close", 110, 10)
{
public void run()
{
onClose();
}
};
public boolean onClose()
{
Dialog.alert("Goodbye!");
System.exit(0);
return true;
}
//SCREEN 1 METHOD
Screen1()
{
HorizontalFieldManager _fieldManagerTop = new HorizontalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
VerticalFieldManager _fieldManagerMiddle = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
HorizontalFieldManager _fieldManagerBottom = new HorizontalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
HorizontalFieldManager _fieldManagerButton = new HorizontalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
//SET TITLE: FINANCIAL APPLICATION
LabelField title = new LabelField ("Financial Application", LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
Font titleFont = this.getFont().derive(Font.BOLD | Font.ITALIC);
title.setFont(titleFont);
setTitle(title);
//SET PAGE & INFO TEXT
LabelField _page = new LabelField("Homepage ~ Initial Investment", LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
LabelField _info = new LabelField("\nThis application acts as a an investment or growth calculator. The user inputs the amount of money he or she would like to invest originally, input the interest, the compound period, the regular monthly deposit into the investment, and the time her or she will have the investment and this application will calculate how much the investment will be worth.\n\n Please enter initial amount of money you are depositing into the investment.\n$XX.xx", LabelField.USE_ALL_WIDTH);
//INPUT FIELDS TESTING!!!!
//INPUT FIELD W/ BORDER
BasicEditField _input = new BasicEditField();
XYEdges padding = new XYEdges(10, 10, 10, 10);
int color = Color.DARKGREEN;
int lineStyle = Border.STYLE_SOLID;
Border inputBorder = BorderFactory.createRoundedBorder(padding, color, lineStyle);
_input.setBorder(inputBorder);
//ADD FIELDS AND SEPARATORS
add(_fieldManagerTop);
add(new SeparatorField());
add(_fieldManagerMiddle);
add(new SeparatorField());
add(_fieldManagerBottom);
add(new SeparatorField());
add(_fieldManagerButton);
//ADD PAGE, INFO, INPUT
_fieldManagerTop.add(_page);
_fieldManagerMiddle.add(_info);
_fieldManagerBottom.add(_input);
//CREATE BUTTON TO NEXT PAGE
ButtonField pressButton = new ButtonField("NEXT");
pressButton.setChangeListener(this);
_fieldManagerButton.add(pressButton);
}
//BUTTON FIELD CHANGE: ACTIONS
public void fieldChanged(Field field, int context)
{
//TRYING TOTEST WHETHER VARIABLE WAS CHANGED
//String initialInvestmentString = (String)FinanceApplication.initialInvestment;
UiApplication.getUiApplication().popScreen(this);
UiApplication.getUiApplication().pushScreen(new Screen2());
}
}
//SCREEN 2 INPUTS *INTEREST*
package com.rim.samples.Finance;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
public class Screen2 extends MainScreen implements FieldChangeListener
{
//DECLARE MENU BUTTONS
protected void makeMenu(Menu menu, int instance)
{
menu.add(_home);
menu.add(_close);
}
private MenuItem _home = new MenuItem("Home Page", 110, 10)
{
public void run()
{
onHome();
}
};
public boolean onHome()
{
Dialog.alert("Homepage Selected");
UiApplication.getUiApplication().popScreen(this);
UiApplication.getUiApplication().pushScreen(new Screen1());
return true;
}
private MenuItem _close = new MenuItem("Close", 110, 10)
{
public void run()
{
onClose();
}
};
public boolean onClose()
{
Dialog.alert("Goodbye!");
System.exit(0);
return true;
}
//SCREEN 2 METHOD
Screen2()
{
HorizontalFieldManager _fieldManagerTop = new HorizontalFieldManager();
VerticalFieldManager _fieldManagerMiddle = new VerticalFieldManager();
HorizontalFieldManager _fieldManagerBottom = new HorizontalFieldManager();
HorizontalFieldManager _fieldManagerButton = new HorizontalFieldManager();
//SET TITLE: FINANCIAL APPLICATION
LabelField title = new LabelField ("Financial Application", LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
Font titleFont = this.getFont().derive(Font.BOLD | Font.ITALIC);
title.setFont(titleFont);
setTitle(title);
//SET PAGE & INFO TEXT
LabelField _page = new LabelField("Page Two ~ Interest", LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
LabelField _info = new LabelField("\nNow please enter the interest that will be put on this investment", LabelField.USE_ALL_WIDTH);
//INPUT FIELD W/ BORDER
BasicEditField _input = new BasicEditField();
XYEdges padding = new XYEdges(10, 10, 10, 10);
int color = Color.DARKGREEN;
int lineStyle = Border.STYLE_SOLID;
Border inputBorder = BorderFactory.createRoundedBorder(padding, color, lineStyle);
_input.setBorder(inputBorder);
//ADD FIELDS AND SEPARATORS
add(_fieldManagerTop);
add(new SeparatorField());
add(_fieldManagerMiddle);
add(new SeparatorField());
add(_fieldManagerBottom);
add(new SeparatorField());
add(_fieldManagerButton);
//ADD PAGE, INFO, INPUT
_fieldManagerTop.add(_page);
_fieldManagerMiddle.add(_info);
_fieldManagerBottom.add(_input);
//CREATE BUTTON TO NEXT PAGE
ButtonField pressButton = new ButtonField("NEXT");
pressButton.setChangeListener(this);
_fieldManagerButton.add(pressButton);
}
//BUTTON FIELD CHANGE: ACTIONS
public void fieldChanged(Field field, int context)
{
UiApplication.getUiApplication().popScreen(this);
UiApplication.getUiApplication().pushScreen(new Screen3());
}
}
//SCREEN 3 INPUTS *COMPOUND PERIOD*
package com.rim.samples.Finance;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
public class Screen3 extends MainScreen implements FieldChangeListener
{
//DECLARE MENU BUTTONS
protected void makeMenu(Menu menu, int instance)
{
menu.add(_home);
menu.add(_close);
}
private MenuItem _home = new MenuItem("Home Page", 110, 10)
{
public void run()
{
onHome();
}
};
public boolean onHome()
{
Dialog.alert("Homepage Selected");
UiApplication.getUiApplication().popScreen(this);
UiApplication.getUiApplication().pushScreen(new Screen1());
return true;
}
private MenuItem _close = new MenuItem("Close", 110, 10)
{
public void run()
{
onClose();
}
};
public boolean onClose()
{
Dialog.alert("Goodbye!");
System.exit(0);
return true;
}
//SCREEN 3 METHOD
Screen3()
{
HorizontalFieldManager _fieldManagerTop = new HorizontalFieldManager();
VerticalFieldManager _fieldManagerMiddle = new VerticalFieldManager();
HorizontalFieldManager _fieldManagerBottom = new HorizontalFieldManager();
HorizontalFieldManager _fieldManagerButton = new HorizontalFieldManager();
//SET TITLE: FINANCIAL APPLICATION
LabelField title = new LabelField ("Financial Application", LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
Font titleFont = this.getFont().derive(Font.BOLD | Font.ITALIC);
title.setFont(titleFont);
setTitle(title);
//SET PAGE & INFO TEXT
LabelField _page = new LabelField("Page Three ~ Compound Period", LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);;
LabelField _info = new LabelField("\nNow please enter the compound period.\npress spacebar to go through the choices:", LabelField.USE_ALL_WIDTH);;
//ADD FIELDS AND SEPARATORS
add(_fieldManagerTop);
add(new SeparatorField());
add(_fieldManagerMiddle);
add(new SeparatorField());
add(_fieldManagerBottom);
add(new SeparatorField());
add(_fieldManagerButton);
//OBJECT CHOICE FIELD
String choicestrs[] = {"Weekly", "Bi-Weekly", "Monthly", "Quarterly", "Annually"};
ObjectChoiceField choice = new ObjectChoiceField("Compound Period: ", choicestrs, 0);
//ADD PAGE, INFO, OBJECT CHOIE TO FIELD
_fieldManagerTop.add(_page);
_fieldManagerMiddle.add(_info);
_fieldManagerBottom.add(choice);
//CREATE BUTTON TO NEXT PAGE
ButtonField press2Button = new ButtonField("NEXT");
press2Button.setChangeListener(this);
_fieldManagerButton.add(press2Button);
}
//BUTTON FIELD CHANGE: ACTIONS
public void fieldChanged(Field field, int context)
{
UiApplication.getUiApplication().popScreen(this);
UiApplication.getUiApplication().pushScreen(new Screen4());
}
}
//SCREEN 4 INPUTS *LENGTH OF INVESTMENT*
package com.rim.samples.Finance;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
public class Screen4 extends MainScreen implements FieldChangeListener
{
//DECLARE MENU BUTTONS
protected void makeMenu(Menu menu, int instance)
{
menu.add(_home);
menu.add(_close);
}
private MenuItem _home = new MenuItem("Home Page", 110, 10)
{
public void run()
{
onHome();
}
};
public boolean onHome()
{
Dialog.alert("Homepage Selected");
UiApplication.getUiApplication().popScreen(this);
UiApplication.getUiApplication().pushScreen(new Screen1());
return true;
}
private MenuItem _close = new MenuItem("Close", 110, 10)
{
public void run()
{
onClose();
}
};
public boolean onClose()
{
Dialog.alert("Goodbye!");
System.exit(0);
return true;
}
//SCREEN 4 METHOD
Screen4()
{
HorizontalFieldManager _fieldManagerTop = new HorizontalFieldManager();
VerticalFieldManager _fieldManagerMiddle = new VerticalFieldManager();
HorizontalFieldManager _fieldManagerBottom = new HorizontalFieldManager();
HorizontalFieldManager _fieldManagerButton = new HorizontalFieldManager();
//SET TITLE: FINANCIAL APPLICATION
LabelField title = new LabelField ("Financial Application", LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
Font titleFont = this.getFont().derive(Font.BOLD | Font.ITALIC);
title.setFont(titleFont);
setTitle(title);
//SET PAGE & INFO TEXT
LabelField _page = new LabelField("Page Four ~ Investment Length", LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
LabelField _info = new LabelField("\nNow please enter how long you will have this investment for?\nPlease enter the amount in months:", LabelField.USE_ALL_WIDTH);
//INPUT FIELD W/ BORDER
BasicEditField _input = new BasicEditField();
XYEdges padding = new XYEdges(10, 10, 10, 10);
int color = Color.DARKGREEN;
int lineStyle = Border.STYLE_SOLID;
Border inputBorder = BorderFactory.createRoundedBorder(padding, color, lineStyle);
_input.setBorder(inputBorder);
//ADD FIELDS AND SEPARATORS
add(_fieldManagerTop);
add(new SeparatorField());
add(_fieldManagerMiddle);
add(new SeparatorField());
add(_fieldManagerBottom);
add(new SeparatorField());
add(_fieldManagerButton);
//ADD PAGE, INFO, INPUT
_fieldManagerTop.add(_page);
_fieldManagerMiddle.add(_info);
_fieldManagerBottom.add(_input);
//CREATE BUTTON TO NEXT PAGE
ButtonField pressButton = new ButtonField("NEXT");
pressButton.setChangeListener(this);
_fieldManagerButton.add(pressButton);
}
//BUTTON FIELD CHANGE: ACTIONS
public void fieldChanged(Field field, int context)
{
UiApplication.getUiApplication().popScreen(this);
UiApplication.getUiApplication().pushScreen(new Screen3());
}
}
//INVESTMENT WORTH
package com.rim.samples.Finance;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
public class InvestmentWorth extends MainScreen implements FieldChangeListener
{
//DECLARE MENU BUTTONS
protected void makeMenu(Menu menu, int instance)
{
menu.add(_close);
}
private MenuItem _close = new MenuItem("Close", 110, 10)
{
public void run()
{
onClose();
}
};
public boolean onClose()
{
Dialog.alert("Goodbye!");
System.exit(0);
return true;
}
//SCREEN 1 METHOD
InvestmentWorth()
{
HorizontalFieldManager _fieldManagerTop = new HorizontalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
VerticalFieldManager _fieldManagerMiddle = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
HorizontalFieldManager _fieldManagerBottom = new HorizontalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
HorizontalFieldManager _fieldManagerButton = new HorizontalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
//SET TITLE: FINANCIAL APPLICATION
LabelField title = new LabelField ("Financial Application", LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
Font titleFont = this.getFont().derive(Font.BOLD | Font.ITALIC);
title.setFont(titleFont);
setTitle(title);
//SET PAGE & INFO TEXT
LabelField _page = new LabelField("Homepage ~ Initial Investment", LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
LabelField _info = new LabelField("\nThis application acts as a an investment or growth calculator. The user inputs the amount of money he or she would like to invest originally, input the interest, the compound period, the regular monthly deposit into the investment, and the time her or she will have the investment and this application will calculate how much the investment will be worth.\n\n Please enter initial amount of money you are depositing into the investment.\n$XX.xx", LabelField.USE_ALL_WIDTH);
//INPUT FIELD W/ BORDER
LabelField _worth = new LabelField();
XYEdges padding = new XYEdges(10, 10, 10, 10);
int color = Color.RED;
int lineStyle = Border.STYLE_SOLID;
Border inputBorder = BorderFactory.createRoundedBorder(padding, color, lineStyle);
_worth.setBorder(inputBorder);
//ADD FIELDS AND SEPARATORS
add(_fieldManagerTop);
add(new SeparatorField());
add(_fieldManagerMiddle);
add(new SeparatorField());
add(_fieldManagerBottom);
add(new SeparatorField());
add(_fieldManagerButton);
//ADD PAGE, INFO, INPUT
_fieldManagerTop.add(_page);
_fieldManagerMiddle.add(_info);
_fieldManagerBottom.add(_worth);
//CREATE BUTTON TO NEXT PAGE
ButtonField pressButton = new ButtonField("NEXT");
pressButton.setChangeListener(this);
_fieldManagerButton.add(pressButton);
}
//BUTTON FIELD CHANGE: ACTIONS
public void fieldChanged(Field field, int context)
{
}
}
This is a lot of code for what i want to do. But i have never written a blackberry application before and im learning as i go. So basically each 'screen' has a basic edit field for the user to type in the initial investment, interest or whatever. But i want to know how to make it so whatever is typed (and i want to set it so only , say a Double variable could be entered) to be set to a variable that can be used in a final equation at the end of the program. i really don't want to start from scratch again, any ideas on how to do this? maybe something different then basic edit field?
Please help me out here! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Barbarrosa
|
Posted: Tue Nov 16, 2010 9:32 pm Post subject: Re: HELP: setting variables for basic edit fields |
|
|
I'll help you if you put your code between syntax tags, like these:
[syntax="java"]
public class MyCode{
}
[/syntax]
so it should look like this when you preview or post it:
Java: |
public class MyCode{
}
|
|
|
|
|
|
|
DelGee
|
Posted: Wed Nov 17, 2010 10:45 am Post subject: RE:HELP: setting variables for basic edit fields |
|
|
Java: |
package com. rim. samples. Finance;
import net.rim.device.api.ui.UiApplication;
public class FinanceApplication extends UiApplication
{
//VARIABLES FOR THE INVESTMENT GROWTH
public double initialInvestment;
public double interest;
public int compoundPeriod;
public int investmentLength;
public static void main (String[] args )
{
FinanceApplication firstApplication = new FinanceApplication ();
firstApplication. enterEventDispatcher();
}
public FinanceApplication ()
{
//OPENING SCREEN FOR INVESTMENT GROWTH
pushScreen (new Screen1 ());
}
}
|
Java: |
//SCREEN 1 INPUTS *INITIAL INVESTMENT*
package com. rim. samples. Finance;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
public class Screen1 extends MainScreen implements FieldChangeListener
{
//DECLARE MENU BUTTONS
protected void makeMenu (Menu menu, int instance )
{
menu. add(_close );
}
private MenuItem _close = new MenuItem("Close", 110, 10)
{
public void run ()
{
onClose ();
}
};
public boolean onClose ()
{
Dialog. alert("Goodbye!");
System. exit(0);
return true;
}
//SCREEN 1 METHOD
Screen1 ()
{
HorizontalFieldManager _fieldManagerTop = new HorizontalFieldManager (Manager. VERTICAL_SCROLL | Manager. VERTICAL_SCROLLBAR);
VerticalFieldManager _fieldManagerMiddle = new VerticalFieldManager (Manager. VERTICAL_SCROLL | Manager. VERTICAL_SCROLLBAR);
HorizontalFieldManager _fieldManagerBottom = new HorizontalFieldManager (Manager. VERTICAL_SCROLL | Manager. VERTICAL_SCROLLBAR);
HorizontalFieldManager _fieldManagerButton = new HorizontalFieldManager (Manager. VERTICAL_SCROLL | Manager. VERTICAL_SCROLLBAR);
//SET TITLE: FINANCIAL APPLICATION
LabelField title = new LabelField ("Financial Application", LabelField. USE_ALL_WIDTH | DrawStyle. HCENTER);
Font titleFont = this. getFont(). derive(Font. BOLD | Font. ITALIC);
title. setFont(titleFont );
setTitle (title );
//SET PAGE & INFO TEXT
LabelField _page = new LabelField ("Homepage ~ Initial Investment", LabelField. USE_ALL_WIDTH | DrawStyle. HCENTER);
LabelField _info = new LabelField ("\nThis application acts as a an investment or growth calculator. The user inputs the amount of money he or she would like to invest originally, input the interest, the compound period, the regular monthly deposit into the investment, and the time her or she will have the investment and this application will calculate how much the investment will be worth.\n\n Please enter initial amount of money you are depositing into the investment.\n$XX.xx", LabelField. USE_ALL_WIDTH);
//INPUT FIELDS TESTING!!!!
//INPUT FIELD W/ BORDER
BasicEditField _input = new BasicEditField ();
XYEdges padding = new XYEdges (10, 10, 10, 10);
int color = Color. DARKGREEN;
int lineStyle = Border. STYLE_SOLID;
Border inputBorder = BorderFactory. createRoundedBorder(padding, color, lineStyle );
_input. setBorder(inputBorder );
//ADD FIELDS AND SEPARATORS
add (_fieldManagerTop );
add (new SeparatorField ());
add (_fieldManagerMiddle );
add (new SeparatorField ());
add (_fieldManagerBottom );
add (new SeparatorField ());
add (_fieldManagerButton );
//ADD PAGE, INFO, INPUT
_fieldManagerTop. add(_page );
_fieldManagerMiddle. add(_info );
_fieldManagerBottom. add(_input );
//CREATE BUTTON TO NEXT PAGE
ButtonField pressButton = new ButtonField ("NEXT");
pressButton. setChangeListener(this);
_fieldManagerButton. add(pressButton );
}
//BUTTON FIELD CHANGE: ACTIONS
public void fieldChanged (Field field, int context )
{
FinanceApplication. initialInvestment=_input;
UiApplication. getUiApplication(). popScreen(this);
UiApplication. getUiApplication(). pushScreen(new Screen2 ());
}
}
|
Java: |
//SCREEN 2 INPUTS *INTEREST*
package com. rim. samples. Finance;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
public class Screen2 extends MainScreen implements FieldChangeListener
{
//DECLARE MENU BUTTONS
protected void makeMenu (Menu menu, int instance )
{
menu. add(_home );
menu. add(_close );
}
private MenuItem _home = new MenuItem("Home Page", 110, 10)
{
public void run ()
{
onHome ();
}
};
public boolean onHome ()
{
Dialog. alert("Homepage Selected");
UiApplication. getUiApplication(). popScreen(this);
UiApplication. getUiApplication(). pushScreen(new Screen1 ());
return true;
}
private MenuItem _close = new MenuItem("Close", 110, 10)
{
public void run ()
{
onClose ();
}
};
public boolean onClose ()
{
Dialog. alert("Goodbye!");
System. exit(0);
return true;
}
//SCREEN 2 METHOD
Screen2 ()
{
HorizontalFieldManager _fieldManagerTop = new HorizontalFieldManager ();
VerticalFieldManager _fieldManagerMiddle = new VerticalFieldManager ();
HorizontalFieldManager _fieldManagerBottom = new HorizontalFieldManager ();
HorizontalFieldManager _fieldManagerButton = new HorizontalFieldManager ();
//SET TITLE: FINANCIAL APPLICATION
LabelField title = new LabelField ("Financial Application", LabelField. USE_ALL_WIDTH | DrawStyle. HCENTER);
Font titleFont = this. getFont(). derive(Font. BOLD | Font. ITALIC);
title. setFont(titleFont );
setTitle (title );
//SET PAGE & INFO TEXT
LabelField _page = new LabelField ("Page Two ~ Interest", LabelField. USE_ALL_WIDTH | DrawStyle. HCENTER);
LabelField _info = new LabelField ("\nNow please enter the interest that will be put on this investment", LabelField. USE_ALL_WIDTH);
//INPUT FIELD W/ BORDER
BasicEditField _input = new BasicEditField ();
XYEdges padding = new XYEdges (10, 10, 10, 10);
int color = Color. DARKGREEN;
int lineStyle = Border. STYLE_SOLID;
Border inputBorder = BorderFactory. createRoundedBorder(padding, color, lineStyle );
_input. setBorder(inputBorder );
//ADD FIELDS AND SEPARATORS
add (_fieldManagerTop );
add (new SeparatorField ());
add (_fieldManagerMiddle );
add (new SeparatorField ());
add (_fieldManagerBottom );
add (new SeparatorField ());
add (_fieldManagerButton );
//ADD PAGE, INFO, INPUT
_fieldManagerTop. add(_page );
_fieldManagerMiddle. add(_info );
_fieldManagerBottom. add(_input );
//CREATE BUTTON TO NEXT PAGE
ButtonField pressButton = new ButtonField ("NEXT");
pressButton. setChangeListener(this);
_fieldManagerButton. add(pressButton );
}
//BUTTON FIELD CHANGE: ACTIONS
public void fieldChanged (Field field, int context )
{
UiApplication. getUiApplication(). popScreen(this);
UiApplication. getUiApplication(). pushScreen(new Screen3 ());
}
}
|
Java: |
//SCREEN 3 INPUTS *COMPOUND PERIOD*
package com. rim. samples. Finance;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
public class Screen3 extends MainScreen implements FieldChangeListener
{
//DECLARE MENU BUTTONS
protected void makeMenu (Menu menu, int instance )
{
menu. add(_home );
menu. add(_close );
}
private MenuItem _home = new MenuItem("Home Page", 110, 10)
{
public void run ()
{
onHome ();
}
};
public boolean onHome ()
{
Dialog. alert("Homepage Selected");
UiApplication. getUiApplication(). popScreen(this);
UiApplication. getUiApplication(). pushScreen(new Screen1 ());
return true;
}
private MenuItem _close = new MenuItem("Close", 110, 10)
{
public void run ()
{
onClose ();
}
};
public boolean onClose ()
{
Dialog. alert("Goodbye!");
System. exit(0);
return true;
}
//SCREEN 3 METHOD
Screen3 ()
{
HorizontalFieldManager _fieldManagerTop = new HorizontalFieldManager ();
VerticalFieldManager _fieldManagerMiddle = new VerticalFieldManager ();
HorizontalFieldManager _fieldManagerBottom = new HorizontalFieldManager ();
HorizontalFieldManager _fieldManagerButton = new HorizontalFieldManager ();
//SET TITLE: FINANCIAL APPLICATION
LabelField title = new LabelField ("Financial Application", LabelField. USE_ALL_WIDTH | DrawStyle. HCENTER);
Font titleFont = this. getFont(). derive(Font. BOLD | Font. ITALIC);
title. setFont(titleFont );
setTitle (title );
//SET PAGE & INFO TEXT
LabelField _page = new LabelField ("Page Three ~ Compound Period", LabelField. USE_ALL_WIDTH | DrawStyle. HCENTER);;
LabelField _info = new LabelField ("\nNow please enter the compound period.\npress spacebar to go through the choices:", LabelField. USE_ALL_WIDTH);;
//ADD FIELDS AND SEPARATORS
add (_fieldManagerTop );
add (new SeparatorField ());
add (_fieldManagerMiddle );
add (new SeparatorField ());
add (_fieldManagerBottom );
add (new SeparatorField ());
add (_fieldManagerButton );
//OBJECT CHOICE FIELD
String choicestrs [] = {"Weekly", "Bi-Weekly", "Monthly", "Quarterly", "Annually"};
ObjectChoiceField choice = new ObjectChoiceField ("Compound Period: ", choicestrs, 0);
//ADD PAGE, INFO, OBJECT CHOIE TO FIELD
_fieldManagerTop. add(_page );
_fieldManagerMiddle. add(_info );
_fieldManagerBottom. add(choice );
//CREATE BUTTON TO NEXT PAGE
ButtonField press2Button = new ButtonField ("NEXT");
press2Button. setChangeListener(this);
_fieldManagerButton. add(press2Button );
}
//BUTTON FIELD CHANGE: ACTIONS
public void fieldChanged (Field field, int context )
{
UiApplication. getUiApplication(). popScreen(this);
UiApplication. getUiApplication(). pushScreen(new Screen4 ());
}
}
|
Java: |
//SCREEN 4 INPUTS *LENGTH OF INVESTMENT*
package com. rim. samples. Finance;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
public class Screen4 extends MainScreen implements FieldChangeListener
{
//DECLARE MENU BUTTONS
protected void makeMenu (Menu menu, int instance )
{
menu. add(_home );
menu. add(_close );
}
private MenuItem _home = new MenuItem("Home Page", 110, 10)
{
public void run ()
{
onHome ();
}
};
public boolean onHome ()
{
Dialog. alert("Homepage Selected");
UiApplication. getUiApplication(). popScreen(this);
UiApplication. getUiApplication(). pushScreen(new Screen1 ());
return true;
}
private MenuItem _close = new MenuItem("Close", 110, 10)
{
public void run ()
{
onClose ();
}
};
public boolean onClose ()
{
Dialog. alert("Goodbye!");
System. exit(0);
return true;
}
//SCREEN 4 METHOD
Screen4 ()
{
HorizontalFieldManager _fieldManagerTop = new HorizontalFieldManager ();
VerticalFieldManager _fieldManagerMiddle = new VerticalFieldManager ();
HorizontalFieldManager _fieldManagerBottom = new HorizontalFieldManager ();
HorizontalFieldManager _fieldManagerButton = new HorizontalFieldManager ();
//SET TITLE: FINANCIAL APPLICATION
LabelField title = new LabelField ("Financial Application", LabelField. USE_ALL_WIDTH | DrawStyle. HCENTER);
Font titleFont = this. getFont(). derive(Font. BOLD | Font. ITALIC);
title. setFont(titleFont );
setTitle (title );
//SET PAGE & INFO TEXT
LabelField _page = new LabelField ("Page Four ~ Investment Length", LabelField. USE_ALL_WIDTH | DrawStyle. HCENTER);
LabelField _info = new LabelField ("\nNow please enter how long you will have this investment for?\nPlease enter the amount in months:", LabelField. USE_ALL_WIDTH);
//INPUT FIELD W/ BORDER
BasicEditField _input = new BasicEditField ();
XYEdges padding = new XYEdges (10, 10, 10, 10);
int color = Color. DARKGREEN;
int lineStyle = Border. STYLE_SOLID;
Border inputBorder = BorderFactory. createRoundedBorder(padding, color, lineStyle );
_input. setBorder(inputBorder );
//ADD FIELDS AND SEPARATORS
add (_fieldManagerTop );
add (new SeparatorField ());
add (_fieldManagerMiddle );
add (new SeparatorField ());
add (_fieldManagerBottom );
add (new SeparatorField ());
add (_fieldManagerButton );
//ADD PAGE, INFO, INPUT
_fieldManagerTop. add(_page );
_fieldManagerMiddle. add(_info );
_fieldManagerBottom. add(_input );
//CREATE BUTTON TO NEXT PAGE
ButtonField pressButton = new ButtonField ("NEXT");
pressButton. setChangeListener(this);
_fieldManagerButton. add(pressButton );
}
//BUTTON FIELD CHANGE: ACTIONS
public void fieldChanged (Field field, int context )
{
UiApplication. getUiApplication(). popScreen(this);
UiApplication. getUiApplication(). pushScreen(new Screen3 ());
}
}
|
Java: |
//INVESTMENT WORTH
package com. rim. samples. Finance;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
public class InvestmentWorth extends MainScreen
{
//DECLARE MENU BUTTONS
protected void makeMenu (Menu menu, int instance )
{
menu. add(_close );
}
private MenuItem _close = new MenuItem("Close", 110, 10)
{
public void run ()
{
onClose ();
}
};
public boolean onClose ()
{
Dialog. alert("Goodbye!");
System. exit(0);
return true;
}
//SCREEN 1 METHOD
InvestmentWorth ()
{
HorizontalFieldManager _fieldManagerTop = new HorizontalFieldManager (Manager. VERTICAL_SCROLL | Manager. VERTICAL_SCROLLBAR);
VerticalFieldManager _fieldManagerMiddle = new VerticalFieldManager (Manager. VERTICAL_SCROLL | Manager. VERTICAL_SCROLLBAR);
HorizontalFieldManager _fieldManagerBottom = new HorizontalFieldManager (Manager. VERTICAL_SCROLL | Manager. VERTICAL_SCROLLBAR);
//SET TITLE: FINANCIAL APPLICATION
LabelField title = new LabelField ("Financial Application", LabelField. USE_ALL_WIDTH | DrawStyle. HCENTER);
Font titleFont = this. getFont(). derive(Font. BOLD | Font. ITALIC);
title. setFont(titleFont );
setTitle (title );
//SET PAGE & INFO TEXT
LabelField _page = new LabelField ("Homepage ~ Initial Investment", LabelField. USE_ALL_WIDTH | DrawStyle. HCENTER);
LabelField _info = new LabelField ("\nThis application acts as a an investment or growth calculator. The user inputs the amount of money he or she would like to invest originally, input the interest, the compound period, the regular monthly deposit into the investment, and the time her or she will have the investment and this application will calculate how much the investment will be worth.\n\n Please enter initial amount of money you are depositing into the investment.\n$XX.xx", LabelField. USE_ALL_WIDTH);
//INPUT FIELD W/ BORDER
LabelField _worth = new LabelField ();
XYEdges padding = new XYEdges (10, 10, 10, 10);
int color = Color. RED;
int lineStyle = Border. STYLE_SOLID;
Border inputBorder = BorderFactory. createRoundedBorder(padding, color, lineStyle );
_worth. setBorder(inputBorder );
//ADD FIELDS AND SEPARATORS
add (_fieldManagerTop );
add (new SeparatorField ());
add (_fieldManagerMiddle );
add (new SeparatorField ());
add (_fieldManagerBottom );
//ADD PAGE, INFO, INPUT
_fieldManagerTop. add(_page );
_fieldManagerMiddle. add(_info );
_fieldManagerBottom. add(_worth );
}
}
|
|
|
|
|
|
|
DelGee
|
Posted: Wed Nov 17, 2010 10:47 am Post subject: RE:HELP: setting variables for basic edit fields |
|
|
Thanks, so im trying to make whatever is input in screen1 to be put into the variable initialInvestment so i can use it for the rest of the program. and i want to make it so it only takes a double in screen 1, if anything else , i want a dialog message to pop saying to enter the right type |
|
|
|
|
|
Barbarrosa
|
Posted: Thu Nov 18, 2010 10:23 pm Post subject: Re: HELP: setting variables for basic edit fields |
|
|
It's bad practice to allow other objects direct access to one object's variables. Use a setter and getter method, instead - this allows you to determine the terms on which a class may access your object. Make the variable private or protected. You can pass in what objects you need to show the dialog, although that's not always good practice.
You could try using a regular expression matcher to ensure it's a double. Using a pre-compiled regular expression can be a lot faster than using a String comparison.
from http://download.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html:
Java: |
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
|
All you need to do is check for a series of digits with one period between them. This can be done with:
Java: |
//Pattern p = Pattern.compile("(?<!\\D)-?\\d*\\.?\\d*(?!\\D)");//for an optional negative double
//Pattern p = Pattern.compile("(?<!\\D)\\d*\\.\\d*(?!\\D)");//for an mandatory, rather than optional, dot
Pattern p = Pattern. compile("(?<!\\D)\\d*\\.?\\d*(?!\\D)");
Matcher m = p. matcher("32.3d");
System. out. println(m. matches()); //prints false
m = p. matcher("32.3");
System. out. println(m. matches()); //prints true
|
I recommend compiling the pattern as few times as possible, to make things (VERY slightly) faster.
I haven't programmed for Blackberry before, but on Android I have access to most of the core java libraries. |
|
|
|
|
|
jcollins1991
|
Posted: Thu Nov 18, 2010 10:55 pm Post subject: Re: HELP: setting variables for basic edit fields |
|
|
I definitely agree that using regular expressions is the best way to go, except those regular expressions allow for strings such as "32." and "-.9", which are sort of lazy ways to write numbers. Something like:
... is a bit more accurate in describing a decimal number. Also, there's really no need for the negative lookahead and lookbehind, if you're matching a string from start to finish you can just use the predefined ^ and $ symbols, so in the end the simplest RE (with back-slashes escaped for Java) would be
|
|
|
|
|
|
Barbarrosa
|
Posted: Thu Nov 18, 2010 11:30 pm Post subject: Re: HELP: setting variables for basic edit fields |
|
|
Duly noted, jcollins.
DelGee:
The best way to share the variable would probably be to create or use a custom class object with a simple getter and setter for values. This avoids passing in references to the loads of stuff loaded by the UIApplication. Just pass an instance into a screen's method after you've instantiated that screen.
If you're worried about different threads trying to change it at the same time, add the "synchronized" keyword into all of the custom class's method declarations (right before the return type). |
|
|
|
|
|
|
|