MATLAB Answers

Get IP address over a Edit box

15 views (last 30 days)
Vick on 4 Jan 2018
Commented: Yair Altman on 5 Jan 2018
Hi all,
I need to get a IP address kind of values from the edit box.i.e, I have to get like 10 values from one edit box. I've planned to give an edit box like image shown below,
How to do this in edit box . I remember seeing this in some undocumented matlab using combo boxes but i can't recall it.


Sign in to comment.

Accepted Answer

Yair Altman
Yair Altman on 5 Jan 2018
Edited: Yair Altman on 5 Jan 2018
Matlab comes with a built-in IP component, com.jidesoft.field.IPTextField, which is part of the JIDE library of components. You can use it as follows:
jIPField = com.jidesoft.field.IPTextField(''); % set default IP
[jIPField, hContainer] = javacomponent(jIPField, [10,10,120,20], hParent); % hParent: panel/figure handle
You can modify the position/size of the text-field in the javacomponent call above, or by modifying the Position / Units properties of hContainer.
You can retrieve the IP text/numeric values using:
vals = jIPField.getValue'; % 1x4 uint32 array => [255,255,255,0]
vals = cell(jIPField.getRawText)'; % 1x4 string cells => {'255','255','255','0'}
ip = char(jIPField.getText); % entire IP string => ''
The component auto-validates the IP values, ensuring that the displayed IP is always valid. The component has many other features, including the ability to enable/disable, color or format the IP components etc.
You can set a callback function to process user changes, by setting the component's StateChangedCallback property, for example:
jIPField.StateChangedCallback = @(jComponent,jEventData) disp(jComponent.getValue');
The JIDE libraries that come with Matlab contain numerous other similarly-useful components, including date/time selectors, credit-card fields, etc. etc.
For more information about using the javacomponent function and handling Java components in Matlab GUI, see my website or book (" Undocumented Secrets of Matlab-Java Programming ", CRC Press 2014)
Yair Altman


Jan on 5 Jan 2018
+1. Yair, do you read all questions in the forum and post such perfect answers if they belong to the Java treasure? Or do you let a robot scan the forum for the term "undocumented"? I thought about this in the times of CSSM already. Sometimes I simply added "This is a question for Yair" in my answers to catch your attraction, and you posted an answer an hour later. Very attentive. Thanks.
Yair Altman
Yair Altman on 5 Jan 2018
Hi Jan - I do indeed have keyword scans in place, and that's how I got here. In the good ol' days of CSSM (RIP), traffic was lower, I had more spare time, and I also got a daily digest of all new threads, so I could quickly review everything. But nowadays I no longer have time to review all the new posts, and I no longer get a digest, so I just use keyword filters (which obviously ignores many potentially interesting posts).
Bottom line: if you want to get my attention, then writing the words "Yair" or "undocumented" will have a good chance of doing this...

Sign in to comment.

More Answers (1)

Jan on 4 Jan 2018
Edited: Jan on 4 Jan 2018
You can use a standard edit box and perform the checks in the callback, e.g.:
function EditCallback(hObject, EventData, handles)
Str = get(hObject, 'String');
[Num, N] = sscanf(Str, '%d.%d.%d.%d');
if N == 4 && && all(Num > 0) && all(Num < 256)
Str = sprintf('%03d.%03d.%03d.%03d', Num); % Or: '%d.%d.%d.%d' if you like it
set(hObject, 'String', Str, 'Color', [0,0,0]);
handles.IP = Str;
guidata(hObject, handles);
else % Warn the user, that this is no valid IP:
set(hObject, 'Color', [1,0,0]);


Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!