BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 09-17-2010, 05:34 PM   #1 (permalink)
New Member
 
Join Date: Sep 2010
Location: Canada
Model: 8330
PIN: N/A
Carrier: Telus
Posts: 10
Post Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy ButtonField

Please Login to Remove!

I'm having difficulty getting my ButtonField to function, and need some assistance. Could you help me please? Here's my source file....

import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.text.TextFilter;
import net.rim.device.api.ui.text.NumericTextFilter;
import net.rim.device.api.ui.container.MainScreen;

public class CalcScreen extends MainScreen implements FieldChangeListener {

EditField curIncome;
EditField level;
EditField increase;
LabelField result;
ButtonField calcButton = null;

public CalcScreen() {

TextFilter numeric = new NumericTextFilter(NumericTextFilter.ALLOW_DECIMAL) ;

EditField curIncome = new EditField("Annual Income ($):", "0");
curIncome.setFilter(numeric);
add(curIncome);

EditField level = new EditField("Performance Level:", "0");
level.setFilter(TextFilter.get(TextFilter.NUMERIC) ); // no decimal
add(level);

EditField increase = new EditField("Approved Increase (%):", "0");
increase.setFilter(numeric);
add(increase);
EditField result = new EditField(Field.READONLY);
result.setLabel("New Annual Income");
add(result);
result.setText("n/a");

calcButton = new ButtonField("Calculate Increase", ButtonField.CONSUME_CLICK);

calcButton.setChangeListener new FieldChangeListener() {
}

public void fieldChanged(Field field, int context); {

if (calcButton == field) {
float a = Float.parseFloat(curIncome.getText());
int b = integer.parseInt(level.getText());
float c = Float.parseFloat(increase.getText()) / 100.; // percents!

int r = (a * c) / b;
add(new LabelField("Annual Wage Increase is " + r)); }

}
}
Offline  
Old 09-17-2010, 10:53 PM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

Can you explain what the issue is?

What works, what doesn't work...are you getting a compile error, or a runtime exception?

Just a quick observation: I don't see where you are adding the buttonfield to the screen. Also, your CalcScreen says it implements FieldChangeListener, but I don't see
where you actually implement this interface in this class.
Offline  
Old 09-20-2010, 12:08 PM   #3 (permalink)
Knows Where the Search Button Is
 
Join Date: Jun 2010
Model: 8530
PIN: N/A
Carrier: Sprint
Posts: 48
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Alex, try this code

Code:
package calcApp;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.text.TextFilter;
import net.rim.device.api.ui.text.NumericTextFilter;
import net.rim.device.api.ui.container.MainScreen;

public class CalcScreen extends MainScreen implements FieldChangeListener {
	EditField curIncome;
	EditField level;
	EditField increase;
	EditField result;
	ButtonField calcButton = null;

	public CalcScreen() {

		TextFilter numeric = new NumericTextFilter(
				NumericTextFilter.ALLOW_DECIMAL);
		curIncome = new EditField("Annual Income ($): ", "");
		curIncome.setFilter(numeric);
		add(curIncome);

		level = new EditField("Performance Level: ", "");
		level.setFilter(TextFilter.get(TextFilter.NUMERIC)); // no decimal
		add(level);

		increase = new EditField("Approved Increase (%): ", "");
		increase.setFilter(numeric);
		add(increase);

		result = new EditField(Field.READONLY);
		result.setLabel("New Annual Income: ");
		result.setText("n/a");
		add(result);

		calcButton = new ButtonField("Calculate Increase",
				ButtonField.CONSUME_CLICK);
		calcButton.setChangeListener(this);
add(calcButton);
	}

	public void fieldChanged(Field field, int context) {
		if (calcButton == field) {
			float a = Float.parseFloat(curIncome.getText());
			int b = Integer.parseInt(level.getText());
			double c = Float.parseFloat(increase.getText()) / 100.; // percents!
			double r = (a * c) / b;
			add(new LabelField("New Annual Wage adjustment is  " + r));
		}

	}

}
Offline  




Copyright 2004-2016 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.