|
||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||
com.numobjects.mathbeans.awt.CalcPanel
Class CalcPanel (Calculator panel bean) provides the following functionality: based solely on a string representing a mathematical expression with any number of variables, it automatically constructs and displays a mini-calculator panel for this expression. The panel will have input fields for all variables in the expression, each with the label representing its name, and similar fields and labels for results. The user enters any numbers into the input fields and presses Enter in any of these fields. The result is immediately computed and displayed in the result field. The user can set parameters that control the format of the output. If desired, the appearance of the panel can be further customized. For instance, one can add a button to the panel to control the start of calculation (in addition to pressing Enter). To do so just register the panel as this button's ActionListener. See the java.awt package docs for information how to do this.
This bean provides a unique possibility of creating various calculators, static or dynamic, avoiding the hassle of setting up GUI, adding logic, dispatching events, etc. The user's only input is the formula set itself, all the rest is figured out by the bean.
The formulas must have the form
Result1=expression1;...ResultN=expressionN
Here "ResultX" is any string providing the name for the result, e.g. "Total amount". The "expressionX" must be a valid non-constant mathematical expression (i.e. must contain variables). Variable names in the expression cannot contain spaces; if there is a need for the name to consist of several words, use quotes around the name in the formula. Here is one simple example:
"Total tax"=Quantity*"Item price"*"Tax rate"/100;
"Total amount"=Quantity*"Item price"*(1+"Tax rate"/100)+Shipping
The resulting calculator panel would contain six fields labeled "Quantity", "Item price", "Tax rate", "Shipping", "Total tax" and "Total amount". As soon as one enters values in the first four fields and presses Enter, the fields labeled "Total tax" and "Total amount" will display the computed value. Missing values are treated as zero.
Here is the code that you have to include in your program:
import com.numobjects.mathbeans.awt.CalcPanel;
...
String formula = "\"Total tax\"=Quantity*\"Item price\"*\"Tax rate\"/100;\"Total amount\"=Quantity*\"Item price\"*(1+\"Tax rate\"/100)+Shipping";
CalcPanel panel = new CalcPanel ();
panel.setFormula (formula);
The calculator panel is ready to be added to your container.
The parameter entry fields can be any subclasses of java.awt.TextField, but the default entry fields are in fact CalcFields, so it is safe to cast them to this type if desired as well as use them to compute constant expressions.
The new feature in version 2 is the ability to support several formulas.
CalcField,
CalcGraphPanel, Serialized Form| Constructor Summary | |
CalcPanel()
Constructs new CalcPanel |
|
| Method Summary | |
void |
actionPerformed(java.awt.event.ActionEvent e)
Handles action events |
int |
getFieldHeight()
Gets entry field height |
int |
getFieldWidth()
Gets entry field width |
java.lang.String |
getFormulae()
Gets formula set |
int |
getHorizontalSpacing()
Gets horizontal spacing between fields |
java.awt.Insets |
getInsets()
Gets insets |
java.awt.Dimension |
getMinimumSize()
Gets minimum size for the panel |
java.awt.TextField |
getParameterField(java.lang.String parameterName)
Gets parameter entry field for given parameter name |
int |
getPrecision()
Gets precision |
java.awt.TextField |
getResultField(java.lang.String resultName)
Gets field for the result |
java.lang.String |
getResultName(int index)
Gets name (label) for the result field |
java.lang.String[] |
getResultNames()
Gets result names |
java.lang.String |
getResultNames(int i)
Gets i-th result name |
int |
getRounding()
Gets rounding |
java.lang.String |
getValue(java.lang.String parameterName)
Gets parameter value |
int |
getVerticalSpacing()
Gets vertical spacing between fields |
boolean |
isDisplayBorder()
Gets border display flag |
boolean |
isDisplayLogo()
Gets logo display flag |
void |
paint(java.awt.Graphics g)
Paints the panel |
void |
setBackground(java.awt.Color c)
Sets background |
void |
setDisplayLogo(boolean flag)
Sets logo display flag |
void |
setFieldHeight(int value)
Sets entry field height |
void |
setFieldWidth(int value)
Sets entry field width |
void |
setFormulae(java.lang.String formulaSet)
Sets formula set |
void |
setHorizontalSpacing(int value)
Sets horizontal spacing |
void |
setInsets(java.awt.Insets insets)
Sets insets for the panel |
void |
setParameterError(java.lang.String parameterName)
Sets parameter error |
void |
setParameterField(java.lang.String parameterName,
java.awt.TextField field)
Tags a text field as a parameter field for given parameter name |
void |
setPrecision(int value)
Sets precision (the number of significant digits in the calculation); range 1 - 15, default precision is 6 |
void |
setResultField(java.lang.String resultName,
java.awt.TextField field)
Tags a text field as the result field |
void |
setResultNames(int i,
java.lang.String resultName)
Sets result labels |
void |
setResultNames(java.lang.String[] resultNames)
Sets result labels |
void |
setResultValue(int index,
java.lang.String result)
Sets result value for given index |
void |
setRounding(int value)
Sets rounding (the number of digits displayed after the decimal dot); range -1 - 15, default rounding is -1 (no rounding) |
void |
setVerticalSpacing(int value)
Sets vertical spacing |
| Constructor Detail |
public CalcPanel()
| Method Detail |
public void setPrecision(int value)
value - precision to setpublic int getPrecision()
public void setRounding(int value)
value - roundingpublic int getRounding()
public void setInsets(java.awt.Insets insets)
insets - insetspublic java.awt.Insets getInsets()
public void setVerticalSpacing(int value)
value - value to setpublic int getVerticalSpacing()
public void setHorizontalSpacing(int value)
value - value to setpublic int getHorizontalSpacing()
public void setFieldWidth(int value)
value - value to setpublic int getFieldWidth()
public void setFieldHeight(int value)
value - value to setpublic int getFieldHeight()
public void setDisplayLogo(boolean flag)
flag - value to setpublic boolean isDisplayLogo()
public boolean isDisplayBorder()
public java.lang.String getFormulae()
public void setFormulae(java.lang.String formulaSet)
formulaSet - formula set stringpublic void setResultNames(java.lang.String[] resultNames)
resultNames - result names to setpublic java.lang.String[] getResultNames()
public void setResultNames(int i,
java.lang.String resultName)
resultNames - result names to setpublic java.lang.String getResultNames(int i)
i - indexpublic java.lang.String getValue(java.lang.String parameterName)
parameterName - parameter name
public void setResultValue(int index,
java.lang.String result)
index - formula indexvalue - value to setpublic java.lang.String getResultName(int index)
public void setParameterField(java.lang.String parameterName,
java.awt.TextField field)
parameterName - parameter namefield - text field to tagpublic java.awt.TextField getParameterField(java.lang.String parameterName)
parameterName - parameter name
public void setResultField(java.lang.String resultName,
java.awt.TextField field)
field - text field to tagpublic java.awt.TextField getResultField(java.lang.String resultName)
public java.awt.Dimension getMinimumSize()
public void paint(java.awt.Graphics g)
g - graphicspublic void actionPerformed(java.awt.event.ActionEvent e)
e - eventpublic void setBackground(java.awt.Color c)
c - colorpublic void setParameterError(java.lang.String parameterName)
parameterName - parameter name
|
||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||