Package com.jidesoft.swing
Class Calculator
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JPanel
com.jidesoft.swing.Calculator
- All Implemented Interfaces:
ActionListener
,ImageObserver
,MenuContainer
,Serializable
,EventListener
,Accessible
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 - 'C', 'c' or ESC to clear current result
- '!' to make current displayed number from positive to negative (or from negative to positive)
- ENTER is equivalent to '='..
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. -
getDisplayText()
: to get the result that should be displayed. Please note, this method return a string. -
getResult()
: to get the last calculated result. This method returns a double value. -
getOperator()
: to get the current operator -
isOverflow()
: to check if there is an overflow. Usually if you try to divide by zero, you will get an overflow.
- See Also:
-
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
FieldsModifier and TypeFieldDescriptionstatic final char
static final char
static final char
static final char
static final char
static final char
static final char
static final char
static final char
static final char
static final char
static final char
static final char
static final char
static final char
static final char
static final char
static final char
static final char
static final int
static final int
static final int
static final int
static final int
static final String
static final String
Fields inherited from class javax.swing.JComponent
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
accessibleContext, 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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
protected void
beep()
void
clear()
Clears the internal state and reset the calculator.void
commit()
Calculates the pending calculation.protected void
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
Gets the gap between buttons.int
Gets the button height.int
Gets the button width.Gets the display format for the number.Gets the display text.int
Gets the current operator.double
Gets the last calculated result.protected void
void
input
(char c) Inputs a char to the calculator.protected boolean
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
Checks if the calculator is in overflow state.boolean
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
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 namedPROPERTY_DISPLAY_TEXT
.void
setInitialValue
(String value) void
void
setOperator
(int operator) Sets the operator and fire property change event on property namedPROPERTY_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 usingregisterKeyboardActions(javax.swing.JComponent, int)
.void
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, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, 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, 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, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, 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, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Field Details
-
OPERATOR_NONE
public static final int OPERATOR_NONE- See Also:
-
OPERATOR_ADD
public static final int OPERATOR_ADD- See Also:
-
OPERATOR_MINUS
public static final int OPERATOR_MINUS- See Also:
-
OPERATOR_MULTIPLY
public static final int OPERATOR_MULTIPLY- See Also:
-
OPERATOR_DIVIDE
public static final int OPERATOR_DIVIDE- See Also:
-
CHAR_CLEAR
public static final char CHAR_CLEAR- See Also:
-
CHAR_POINT
public static final char CHAR_POINT- See Also:
-
CHAR_ADD
public static final char CHAR_ADD- See Also:
-
CHAR_MINUS
public static final char CHAR_MINUS- See Also:
-
CHAR_MULTIPLY
public static final char CHAR_MULTIPLY- See Also:
-
CHAR_DIVIDE
public static final char CHAR_DIVIDE- See Also:
-
CHAR_EQUAL
public static final char CHAR_EQUAL- See Also:
-
CHAR_NEGATIVE
public static final char CHAR_NEGATIVE- See Also:
-
CHAR_BACKSPACE
public static final char CHAR_BACKSPACE- See Also:
-
CHAR_0
public static final char CHAR_0- See Also:
-
CHAR_1
public static final char CHAR_1- See Also:
-
CHAR_2
public static final char CHAR_2- See Also:
-
CHAR_3
public static final char CHAR_3- See Also:
-
CHAR_4
public static final char CHAR_4- See Also:
-
CHAR_5
public static final char CHAR_5- See Also:
-
CHAR_6
public static final char CHAR_6- See Also:
-
CHAR_7
public static final char CHAR_7- See Also:
-
CHAR_8
public static final char CHAR_8- See Also:
-
CHAR_9
public static final char CHAR_9- See Also:
-
PROPERTY_DISPLAY_TEXT
- See Also:
-
PROPERTY_OPERATOR
- See Also:
-
-
Constructor Details
-
Calculator
public Calculator()Creates aCalculator
.
-
-
Method Details
-
setLocale
-
configureNumberFormat
protected void configureNumberFormat()Configures the number format for displaying purpose. -
isValidKeyEvent
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
Checks if the key event a key event for operators. In the other words, if it isCHAR_ADD
,CHAR_MINUS
,CHAR_MULTIPLY
orCHAR_DIVIDE
, this method will return true.- Parameters:
keyEvent
- the key event.- Returns:
- true if it is a valid key event is an operator.
-
isEnterOrEqual
Checks if the key event a key event for enter. In the other words, if it isKeyEvent.VK_ENTER
orKeyEvent.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
Registers necessary keyboard actions onto the component. Usually the component is aJTextField
.- Parameters:
component
- the component where the key input will be taken and passed to theCalculator
.condition
- the condition as defined inJComponent.registerKeyboardAction(java.awt.event.ActionListener, javax.swing.KeyStroke, int)
.
-
unregisterKeyboardActions
Unregisters the keyboard actions you registered usingregisterKeyboardActions(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
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 overridecreateButton(String, javax.swing.Icon)
method.- Parameters:
text
- the text on the button.- Returns:
- the button.
-
createButton
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 inCalculator
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
Gets the display text.- Returns:
- the display text.
-
setDisplayText
Sets the display text and fire property change event on property namedPROPERTY_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 namedPROPERTY_OPERATOR
.- Parameters:
operator
- the operator.
-
actionPerformed
- Specified by:
actionPerformed
in interfaceActionListener
-
fakePressButton
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
Gets the display format for the number.- Returns:
- the display format for the number.
-
setDisplayFormat
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
-
main
-