JIDE 3.5.15

com.jidesoft.swing
Class Calculator

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by com.jidesoft.swing.Calculator
All Implemented Interfaces:
ActionListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible

public class Calculator
extends JPanel
implements ActionListener

Calculator is a component that can do simple arithmetic calculation. Since it extends JPanel, you can use it at any place in your application.

To make it more flexible, the Calculator has no text field to display the result. You can create your own JTextField or JLabel to display the result. Here is a simple example to create a text field and associate it with Calculator.


 final JTextField textField = new JTextField();
 textField.setColumns(20);
 textField.setHorizontalAlignment(JTextField.TRAILING);
 Calculator calculator = new Calculator();
 calculator.registerKeyboardActions(textField, JComponent.WHEN_FOCUSED);
 calculator.addPropertyChangeListener(Calculator.PROPERTY_DISPLAY_TEXT, new PropertyChangeListener() {
     public void propertyChange(PropertyChangeEvent evt) {
         textField.setText("" + evt.getNewValue());
     }
 });
 calculator.clear();
 
With the code above, user can type in directly into text field and do the calculation. If you just want to display the result and don't mind if the text field accepts keyboard input, you don't need to call registerKeyboardActions method.

All numeric and operator keys work as expected. Here are a few special keys that worth mentioning

Another interesting way to use Calculator is to use it without using GUI.


 Calculator calculator = new Calculator();
 calculator.input('1');
 calculator.input('0');
 calculator.input('*');
 calculator.input('2');
 calculator.input('4');
 calculator.input('=');
 System.out.println("10 * 24 = " + calculator.getDisplayText());
 
The print out will be "10 * 24 = 240".

There are several methods you can use to get internal state of the Calculator.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JPanel
JPanel.AccessibleJPanel
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
static char CHAR_0
           
static char CHAR_1
           
static char CHAR_2
           
static char CHAR_3
           
static char CHAR_4
           
static char CHAR_5
           
static char CHAR_6
           
static char CHAR_7
           
static char CHAR_8
           
static char CHAR_9
           
static char CHAR_ADD
           
static char CHAR_BACKSPACE
           
static char CHAR_CLEAR
           
static char CHAR_DIVIDE
           
static char CHAR_EQUAL
           
static char CHAR_MINUS
           
static char CHAR_MULTIPLY
           
static char CHAR_NEGATIVE
           
static char CHAR_POINT
           
static int OPERATOR_ADD
           
static int OPERATOR_DIVIDE
           
static int OPERATOR_MINUS
           
static int OPERATOR_MULTIPLY
           
static int OPERATOR_NONE
           
static String PROPERTY_DISPLAY_TEXT
           
static String PROPERTY_OPERATOR
           
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
Calculator()
          Creates a Calculator.
 
Method Summary
 void actionPerformed(ActionEvent e)
           
protected  void beep()
           
 void clear()
          Clears the internal state and reset the calculator.
 void commit()
          Calculates the pending calculation.
protected  void configureNumberFormat()
          Configures the number format for displaying purpose.
protected  AbstractButton createButton(String text)
          Creates the button that is used in the Calculator.
protected  AbstractButton createButton(String text, Icon icon)
          Creates the button that is used in the Calculator.
protected  void fakePressButton(AbstractButton button)
          Press the button.
 int getButtonGap()
          Gets the gap between buttons.
 int getButtonHeight()
          Gets the button height.
 int getButtonWidth()
          Gets the button width.
 NumberFormat getDisplayFormat()
          Gets the display format for the number.
 String getDisplayText()
          Gets the display text.
 int getOperator()
          Gets the current operator.
 double getResult()
          Gets the last calculated result.
protected  void initComponents()
           
 void input(char c)
          Inputs a char to the calculator.
protected  boolean isCellEditor()
          If this method return true, ENTER and ESCAPE key will be registered.
static boolean isEnterOrEqual(KeyEvent keyEvent)
          Checks if the key event a key event for enter.
static boolean isOperator(KeyEvent keyEvent)
          Checks if the key event a key event for operators.
 boolean isOverflow()
          Checks if the calculator is in overflow state.
 boolean isResultCalculated()
          Get the flag indicating if the result was calculated at least once.
static boolean isValidKeyEvent(KeyEvent keyEvent)
          Checks if the key event a valid key event that can be accepted by the Calculator.
static void main(String[] args)
           
 void registerKeyboardActions(JComponent component, int condition)
          Registers necessary keyboard actions onto the component.
 void setButtonGap(int buttonGap)
           
 void setButtonHeight(int buttonHeight)
          Sets the button height.
 void setButtonWidth(int buttonWidth)
          Sets the button width.
 void setDisplayFormat(NumberFormat displayFormat)
          Sets the display format for the number.
 void setDisplayText(String displayText)
          Sets the display text and fire property change event on property named PROPERTY_DISPLAY_TEXT.
 void setInitialValue(String value)
           
 void setLocale(Locale l)
           
 void setOperator(int operator)
          Sets the operator and fire property change event on property named PROPERTY_OPERATOR.
 void setOverflow(boolean overflow)
          Sets the overflow flag.
 void setResultCalculated(boolean resultCalculated)
          Set the flag indicating if the result was calculated at least once.
 void unregisterKeyboardActions(JComponent component)
          Unregisters the keyboard actions you registered using registerKeyboardActions(javax.swing.JComponent, int).
 void updateResult()
          Update the result as if the equal was pressed.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

OPERATOR_NONE

public static final int OPERATOR_NONE
See Also:
Constant Field Values

OPERATOR_ADD

public static final int OPERATOR_ADD
See Also:
Constant Field Values

OPERATOR_MINUS

public static final int OPERATOR_MINUS
See Also:
Constant Field Values

OPERATOR_MULTIPLY

public static final int OPERATOR_MULTIPLY
See Also:
Constant Field Values

OPERATOR_DIVIDE

public static final int OPERATOR_DIVIDE
See Also:
Constant Field Values

CHAR_CLEAR

public static final char CHAR_CLEAR
See Also:
Constant Field Values

CHAR_POINT

public static final char CHAR_POINT
See Also:
Constant Field Values

CHAR_ADD

public static final char CHAR_ADD
See Also:
Constant Field Values

CHAR_MINUS

public static final char CHAR_MINUS
See Also:
Constant Field Values

CHAR_MULTIPLY

public static final char CHAR_MULTIPLY
See Also:
Constant Field Values

CHAR_DIVIDE

public static final char CHAR_DIVIDE
See Also:
Constant Field Values

CHAR_EQUAL

public static final char CHAR_EQUAL
See Also:
Constant Field Values

CHAR_NEGATIVE

public static final char CHAR_NEGATIVE
See Also:
Constant Field Values

CHAR_BACKSPACE

public static final char CHAR_BACKSPACE
See Also:
Constant Field Values

CHAR_0

public static final char CHAR_0
See Also:
Constant Field Values

CHAR_1

public static final char CHAR_1
See Also:
Constant Field Values

CHAR_2

public static final char CHAR_2
See Also:
Constant Field Values

CHAR_3

public static final char CHAR_3
See Also:
Constant Field Values

CHAR_4

public static final char CHAR_4
See Also:
Constant Field Values

CHAR_5

public static final char CHAR_5
See Also:
Constant Field Values

CHAR_6

public static final char CHAR_6
See Also:
Constant Field Values

CHAR_7

public static final char CHAR_7
See Also:
Constant Field Values

CHAR_8

public static final char CHAR_8
See Also:
Constant Field Values

CHAR_9

public static final char CHAR_9
See Also:
Constant Field Values

PROPERTY_DISPLAY_TEXT

public static final String PROPERTY_DISPLAY_TEXT
See Also:
Constant Field Values

PROPERTY_OPERATOR

public static final String PROPERTY_OPERATOR
See Also:
Constant Field Values
Constructor Detail

Calculator

public Calculator()
Creates a Calculator.

Method Detail

setLocale

public void setLocale(Locale l)
Overrides:
setLocale in class Component

configureNumberFormat

protected void configureNumberFormat()
Configures the number format for displaying purpose.


isValidKeyEvent

public static boolean isValidKeyEvent(KeyEvent keyEvent)
Checks if the key event a valid key event that can be accepted by the Calculator.

Parameters:
keyEvent - the key event.
Returns:
true if it is a valid key event for the Calculator.

isOperator

public static boolean isOperator(KeyEvent keyEvent)
Checks if the key event a key event for operators. In the other words, if it is CHAR_ADD, CHAR_MINUS, CHAR_MULTIPLY or CHAR_DIVIDE, this method will return true.

Parameters:
keyEvent - the key event.
Returns:
true if it is a valid key event is an operator.

isEnterOrEqual

public static boolean isEnterOrEqual(KeyEvent keyEvent)
Checks if the key event a key event for enter. In the other words, if it is KeyEvent.VK_ENTER or KeyEvent.VK_EQUALS, this method will return true.

Parameters:
keyEvent - the key event.
Returns:
true if it is a valid key event is an enter key or an equal key.

registerKeyboardActions

public void registerKeyboardActions(JComponent component,
                                    int condition)
Registers necessary keyboard actions onto the component. Usually the component is a JTextField.

Parameters:
component - the component where the key input will be taken and passed to the Calculator.
condition - the condition as defined in JComponent.registerKeyboardAction(java.awt.event.ActionListener, javax.swing.KeyStroke, int).

unregisterKeyboardActions

public void unregisterKeyboardActions(JComponent component)
Unregisters the keyboard actions you registered using registerKeyboardActions(javax.swing.JComponent, int).

Parameters:
component - the component.

initComponents

protected void initComponents()

isResultCalculated

public boolean isResultCalculated()
Get the flag indicating if the result was calculated at least once.

Returns:
true if the result was calculated at least once. Otherwise false.

setResultCalculated

public void setResultCalculated(boolean resultCalculated)
Set the flag indicating if the result was calculated at least once.

Parameters:
resultCalculated - the flag

createButton

protected AbstractButton createButton(String text)
Creates the button that is used in the Calculator. By default, it will create a JideButton. Here is the code. You can override it to create your own button. This method is used to create all buttons except the backspace and the +/- button. So if you want to override it, it's better to override createButton(String, javax.swing.Icon) method.

Parameters:
text - the text on the button.
Returns:
the button.

createButton

protected AbstractButton createButton(String text,
                                      Icon icon)
Creates the button that is used in the Calculator. By default, it will create a JideButton. Here is the code. You can override it to create your own button.

 AbstractButton button = new JideButton(text, icon);
 button.setOpaque(true);
 button.setContentAreaFilled(true);
 button.setRequestFocusEnabled(false);
 button.setFocusable(false);
 button.addActionListener(this);
 return button;
 

Parameters:
text - the text on the button.
icon - the icon on the button.
Returns:
the button.

isOverflow

public boolean isOverflow()
Checks if the calculator is in overflow state.

Returns:
true if overflow.

setOverflow

public void setOverflow(boolean overflow)
Sets the overflow flag.

Parameters:
overflow - the overflow flag.

input

public void input(char c)
Inputs a char to the calculator. Please note, not all chars are acceptable. Valid chars are defined in Calculator class as CHAR_XXX constants.

Parameters:
c - the char input char.

beep

protected void beep()

updateResult

public void updateResult()
Update the result as if the equal was pressed. This method can be used while Enter key is pressed and you need keep the calculated result.


clear

public void clear()
Clears the internal state and reset the calculator.


getResult

public double getResult()
Gets the last calculated result.

Returns:
the last calculated result.

getDisplayText

public String getDisplayText()
Gets the display text.

Returns:
the display text.

setDisplayText

public void setDisplayText(String displayText)
Sets the display text and fire property change event on property named PROPERTY_DISPLAY_TEXT.

Parameters:
displayText - the displayed text.

getOperator

public int getOperator()
Gets the current operator.

Returns:
the current operator.

setOperator

public void setOperator(int operator)
Sets the operator and fire property change event on property named PROPERTY_OPERATOR.

Parameters:
operator - the operator.

actionPerformed

public void actionPerformed(ActionEvent e)
Specified by:
actionPerformed in interface ActionListener

fakePressButton

protected void fakePressButton(AbstractButton button)
Press the button. By default, we will trigger the action directly on this button. However subclass can override it to call doClick to mimic the user pressing the button.

Parameters:
button - the button

getDisplayFormat

public NumberFormat getDisplayFormat()
Gets the display format for the number.

Returns:
the display format for the number.

setDisplayFormat

public void setDisplayFormat(NumberFormat displayFormat)
Sets the display format for the number.

Parameters:
displayFormat - the display format.

commit

public void commit()
Calculates the pending calculation. If the Calculator has both operations and a valid operator, this method will do the calculation and set the display text and result.


getButtonWidth

public int getButtonWidth()
Gets the button width.

Returns:
the button width.

setButtonWidth

public void setButtonWidth(int buttonWidth)
Sets the button width.

Parameters:
buttonWidth - the new button width.

getButtonHeight

public int getButtonHeight()
Gets the button height.

Returns:
the button height.

setButtonHeight

public void setButtonHeight(int buttonHeight)
Sets the button height.

Parameters:
buttonHeight - the new button height.

getButtonGap

public int getButtonGap()
Gets the gap between buttons. Default is 2.

Returns:
the gap between buttons.

setButtonGap

public void setButtonGap(int buttonGap)

isCellEditor

protected boolean isCellEditor()
If this method return true, ENTER and ESCAPE key will be registered. Otherwise they will not be. The reason we do so because the two keys are conflicted with keys in JTable.

Returns:
true or false.

setInitialValue

public void setInitialValue(String value)

main

public static void main(String[] args)

JIDE 3.5.15