JIDE 3.5.15

com.jidesoft.field
Class IPTextField

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.field.IPTextField
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

public class IPTextField
extends JPanel

IPTextField is a component for editing an IP address. It can accept IP address as either a String such as "255.16.32.15" (using setText(String) or an int array such as new int[]{255, 16, 32, 15} (using setValue(int[]) . It also can return IP address in either of the two formats. You can use static methods convertIPToString(int[]) and convertStringToIP(String) to convert between the two formats.

IPTextField also supports subnet mask. You can specify a Class B mask "255.255.0.0" for example using setSubnetMask(String) method. Any IP addresses that are outside the subnet will be automatically corrected.

Here are some technical notes. At first glance, you may think it is possible to implement IPTextField using JFormattedTextField with MaskFormatter. Sure, it can get your almost there. However it's not easy to enforce each octet of the IP address to less than 255. In our implementation, we actually use four JTextFields for each octet and three JLabels for the "." between octets. However we still want to give user the impression that it is one text field. That's why we add code to handle all the keystrokes that can be possibly used. For example, left, right, home, end and backspace keys will transverse among text fields transparently. "." key will navigate to next field. You could invoke getTextFields() to get all the JTextFields it created.

Although it is one of the JTextFields that owns the focus, you may want to know if IPTextField right now "owns" the focus. For this purpose, you could add focus listener to this component. As long as the focus is switched between the JTextFields inside, you will not get the listener triggered.

This component is still in beta.

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
protected  ChangeEvent changeEvent
           
 String PROPERTY_EDITABLE
           
 String PROPERTY_SUBNET_MASK
           
 
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
IPTextField()
           
IPTextField(int[] ip)
           
IPTextField(String ip)
           
 
Method Summary
 void addChangeListener(ChangeListener l)
          Adds a ChangeListener to the model.
 void addFocusListener(FocusListener l)
           
static String convertIPToString(int[] ip)
          Converts an ip (int array) to a string.
static int[] convertStringToIP(String str)
          Converts a string to ip (int array).
protected  JTextField createTextField(JTextField previousTextField)
          Creates the text fields for this IPTextField.
protected  void fireStateChanged()
          Runs each ChangeListener's stateChanged method.
 int getBaseline(int width, int height)
           
 ChangeListener[] getChangeListeners()
          Returns an array of all the ChangeListeners added to this DefaultColorSelectionModel with addChangeListener.
 FocusListener[] getFocusListeners()
           
 String[] getRawText()
          Gets the raw texts in each text field.
 int[] getSubnetMask()
           
 String getText()
          Gets the IP as text.
 JTextField[] getTextFields()
          Gets the four text fields that used by this IPTextField.
 int[] getValue()
          Gets the IP address as int[].
 boolean hasFocus()
           
 boolean isEditable()
          Returns the boolean indicating whether this IPTextField is editable or not.
protected  boolean isNavigateToNextField(char ch)
          Check if the input character should trigger navigating to next available field.
 boolean isValueValid()
          Checks if the value in IPTextField is valid.
 void removeChangeListener(ChangeListener l)
          Removes a ChangeListener from the model.
 void removeFocusListener(FocusListener l)
           
 void requestFocus()
           
 boolean requestFocus(boolean temporary)
           
 boolean requestFocusInWindow()
           
 void setEditable(boolean b)
          Sets the specified boolean to indicate whether or not this IPTextField should be editable.
 void setFocusable(boolean focusable)
           
 void setRawText(String[] rawText)
          Sets the raw texts in each text field.
 void setSubnetMask(int[] subnetMask)
           
 void setSubnetMask(String subnetMask)
           
 void setText(String str)
          Sets the IP as text.
 void setToolTipText(String text)
           
 void setToolTipText(String[] toolTips)
          Set the tool tips to the sub fields
 void setToolTipText(String text, boolean forward)
          Set the tool tip to the component, could be forwarded to set the tool tip of sub fields
 void setValue(int[] ip)
          Sets the IP as int[].
 void updateUI()
           
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI
 
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, 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, 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, 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, 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, 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, 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, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, 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

PROPERTY_SUBNET_MASK

public final String PROPERTY_SUBNET_MASK
See Also:
Constant Field Values

PROPERTY_EDITABLE

public final String PROPERTY_EDITABLE
See Also:
Constant Field Values

changeEvent

protected transient ChangeEvent changeEvent
Constructor Detail

IPTextField

public IPTextField()

IPTextField

public IPTextField(String ip)

IPTextField

public IPTextField(int[] ip)
Method Detail

updateUI

public void updateUI()
Overrides:
updateUI in class JPanel

setText

public void setText(String str)
Sets the IP as text.

Parameters:
str - the new IP text

getText

public String getText()
Gets the IP as text. If an octet is not filled in, it will be 0.

Returns:
IP as text.

setValue

public void setValue(int[] ip)
Sets the IP as int[]. Each part in the IP address is an integer in the array.

Parameters:
ip - the IP address int array

getValue

public int[] getValue()
Gets the IP address as int[]. If an octet is not filled in, it will be 0.

Returns:
IP address as int[].

getRawText

public String[] getRawText()
Gets the raw texts in each text field.

Returns:
the actual text that is in each text field.
See Also:
setRawText(String[])

setRawText

public void setRawText(String[] rawText)
Sets the raw texts in each text field.

Parameters:
rawText - the actual text that is in each text field.

isValueValid

public boolean isValueValid()
Checks if the value in IPTextField is valid. It will return false if Even if this method returns false, getValue() will fill invalid fields with 0 to make a valid IP address. However if you use getRawText(), it will give you the exact text in each octet so that you can check to see which value is invalid.

Returns:
true if the value is valid. Otherwise false.

getSubnetMask

public int[] getSubnetMask()

setSubnetMask

public void setSubnetMask(int[] subnetMask)

setSubnetMask

public void setSubnetMask(String subnetMask)

convertIPToString

public static String convertIPToString(int[] ip)
Converts an ip (int array) to a string.

Parameters:
ip - the IP address in int array
Returns:
the String of the IP.

convertStringToIP

public static int[] convertStringToIP(String str)
Converts a string to ip (int array).

Parameters:
str - the IP string
Returns:
the IP address in int array.

requestFocus

public void requestFocus()
Overrides:
requestFocus in class JComponent

requestFocus

public boolean requestFocus(boolean temporary)
Overrides:
requestFocus in class JComponent

requestFocusInWindow

public boolean requestFocusInWindow()
Overrides:
requestFocusInWindow in class JComponent

hasFocus

public boolean hasFocus()
Overrides:
hasFocus in class Component

createTextField

protected JTextField createTextField(JTextField previousTextField)
Creates the text fields for this IPTextField.

Parameters:
previousTextField - the previous JTextField
Returns:
the text field.

setFocusable

public void setFocusable(boolean focusable)
Overrides:
setFocusable in class Component

addFocusListener

public void addFocusListener(FocusListener l)
Overrides:
addFocusListener in class Component

removeFocusListener

public void removeFocusListener(FocusListener l)
Overrides:
removeFocusListener in class Component

getFocusListeners

public FocusListener[] getFocusListeners()
Overrides:
getFocusListeners in class Component

isNavigateToNextField

protected boolean isNavigateToNextField(char ch)
Check if the input character should trigger navigating to next available field.

By default, '.' and ' ' are both deemed as the navigation key.

Parameters:
ch - the input character
Returns:
true if the character input is a navigation key. Otherwise false.

isEditable

public boolean isEditable()
Returns the boolean indicating whether this IPTextField is editable or not.

Returns:
the boolean value
See Also:
setEditable(boolean)

setEditable

public void setEditable(boolean b)
Sets the specified boolean to indicate whether or not this IPTextField should be editable. A PropertyChange event ("editable") is fired when the state is changed.

Parameters:
b - the boolean to be set
See Also:
isEditable()

getTextFields

public JTextField[] getTextFields()
Gets the four text fields that used by this IPTextField.

Please note, we make this method public in 1.9.3 release because there are users need to access the JTextFields directly. Ideally, you shouldn't be aware of the four JTextFields as it's our implementation to use four JTextField. We might roll out another implement to just use one JTextField.

Returns:
the four text fields that used by this IPTextField

addChangeListener

public void addChangeListener(ChangeListener l)
Adds a ChangeListener to the model.

Parameters:
l - the ChangeListener to be added
See Also:
removeChangeListener(javax.swing.event.ChangeListener), getChangeListeners()

removeChangeListener

public void removeChangeListener(ChangeListener l)
Removes a ChangeListener from the model.

Parameters:
l - the ChangeListener to be removed

getChangeListeners

public ChangeListener[] getChangeListeners()
Returns an array of all the ChangeListeners added to this DefaultColorSelectionModel with addChangeListener.

Returns:
all of the ChangeListeners added, or an empty array if no listeners have been added

fireStateChanged

protected void fireStateChanged()
Runs each ChangeListener's stateChanged method.

See Also:
EventListenerList

getBaseline

public int getBaseline(int width,
                       int height)
Overrides:
getBaseline in class JComponent

setToolTipText

public void setToolTipText(String[] toolTips)
Set the tool tips to the sub fields

Parameters:
toolTips - The String array of tool tips, will try to set the most available toop tips to the sub fields
Since:
3.4.6

setToolTipText

public void setToolTipText(String text,
                           boolean forward)
Set the tool tip to the component, could be forwarded to set the tool tip of sub fields

Parameters:
text - the tool tip text
forward - if true, the text will be forwarded to sub fields

setToolTipText

public void setToolTipText(String text)
Overrides:
setToolTipText in class JComponent

JIDE 3.5.15