Today, a CSSM user posted the question how to modify the border of Matlab editboxes. There is of course no documented way to do this in plain Matlab. However, this is so easy to do using the edit-box’s undocumented underlying Java, that I decided to skip my regular weekly article schedule and post an out-of-band explanation.
As the CSSM poster correctly figured out, we first need to get the uicontrol’s underlying Java reference. This is done using my FindJObj utility. The next step is to get the Java object’s Border property:
>> hEdit = uicontrol('Style','Edit'); % create Matlab uicontrol >> jEdit = findjobj(hEdit) % get underlying Java reference jEdit = javahandle_withcallbacks.com.mathworks.hg.peer.EditTextPeer$hgTextField >> jEdit.Border % check the control's border ans = com.sun.java.swing.plaf.windows.XPStyle$XPFillBorder@e5a137 >> jEdit.Border.get BorderOpaque = on Class = [ (1 by 1) java.lang.Class array] LineColor = [0.498039 0.615686 0.72549] RoundedCorners = off Thickness = [1] ...
Unfortunately, as the CSSM poster has discovered, this Border property cannot be modified. Such an object is called immutable in Java; another common example is the Font property.
However, we can easily replace the object’s Border with a custom-made border, as follows:
lineColor = java.awt.Color(1,0,0); % =red thickness = 3; % pixels roundedCorners = true; newBorder = javax.swing.border.LineBorder(lineColor,thickness,roundedCorners); jEdit.Border = newBorder; jEdit.repaint; % redraw the modified control
editbox with regular border
editbox with custom border
Much more complex and interesting borders can be created in much the same way. Interested readers are referred to the official documentation of Java Borders or any decent Swing textbook.
We can use the same technique to remove the uicontrol’s borders altogether:
jEdit.Border = [];
editbox with no border
Setting the border as shown above works in much the same manner for all Java uicontrols. Simply use findjobj to get the Java reference and then update its Border property.
This article has described the Border property. Matlab uicontrols’ underlying Java objects contain numerous other useful properties and methods that can greatly extend our Matlab GUI, and I intend to describe some of them in the upcoming months/years.
Do you have any particular uicontrol customization that you would like me to describe? If so, please do post a comment.
Related posts:
- Setting line position in an edit-box uicontrol Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....
- Additional uicontrol tooltip hacks Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....
- Uicontrol callbacks This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...
- Customizing Matlab labels Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...
- Customizing listbox & editbox scrollbars Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized....
- Customizing uitree This article describes how to customize Matlab GUI tree controls created using the undocumented uitree function...


Hi Yair,
Thanks for sharing all this knowledge! A uicontrol customization I would be interested in, is related to tabpanels. Although they could be very userfull, I am not very satisfied with the way uitabs look and function in Matlab. In particular, I would like to incorporate bitmap pictures in uitabs, comparable to how they can be incorporated in buttons or toolbars. Is that possible?
Thanks in advance,
Jos
@Jos – it is indeed possible to include icon images in tabs. For example, an ‘x’ button to close the tab, as in modern browsers. I’ll write about this in next week’s post.
- Yair
hello,
regarding borders i got a question: suppose to have a figure, put inside a panel, put inside this panel a uitext. suppose now to put a slider, to move the text in the panel. When the uitext reaches the borders of the panel, it won’t hide, but it overlap the panel’s borders, going outside it, over the figure ground.
Instead if you got a panel inside the another panel, and you move it, it is hiding itself when it go out the borders of its parents. It is a ”bug” of matlab or is there any property to set to the uitext? (it happen also with buttons etc…). Any solution?
see the example here:
%creating the figure, the panel and uitext
fig=figure();
pnl1 = uipanel(‘parent’,fig,’units’,'normalized’,'position’,[.1 .1 .5 .5],’BackgroundColor’,[.5 .8 .8]);
txt1 = uicontrol(‘parent’,pnl1,’style’,'text’,'string’,'hello’,…
‘BackgroundColor’,[.9 .7 .8],’units’,'normalized’,'position’,[.2 .2 .5 .5]);
%trying to move the text
set(txt1,’position’,[.3 .8 .5 .5])
%same example with a panel
pnl2 = uipanel(‘parent’,pnl1,’BackgroundColor’,[.9 .1 .8],’units’,'normalized’,'position’,[.2 .2 .5 .5]);
set(pnl2,’position’,[.7 .2 .5 .5])
@Marco – in theory this should be done by the Clipping property, which is ‘on’ by default. Unfortunately, as the documentation states: “This property has no effect on uicontrol objects“. You can play around with the Z-Order (see uistack), or you can trap and fix this programmatically (mouse motion callback etc.), if it really bothers you.
unfortunatly it really bothers me!!
because i build a complex gui, with panels that are updating if the user interact with it, adding or removing for examples some line or plots. In this panels there are for example text and check boxes…and when there are too many, a slider is implemented. there is a function to grid the visualization when the figure is resized or no. So when a slider is called and the user move it, the uitext and other objects on that panels are ”flying” in the figure…..
any idea or suggestion to solve it?
Hi,
I´m having some troubles combining this way of setting parameters with GUI created using matlab GUIDE. Guide really doesn´t offer much customisation especially for button borders, colors and so on.
My problem is, that i don´t know where exactly should i place the code, which changes button border… At first, I placed it logically into main figure opening function (executed before figure actually appears), but findjobj doesn´t find any java objects (it returns just empty array), although handle structure already consist of all matlab gui handles… it works If I change code and press, while app is already launched. (java objects existing…)
Do you have any suggestions how to solve my problem?
FindJObj *cannot* work in the GUIDE-created *_OpeningFcn because the figure is not yet visible at this time and so the Java components have still not rendered and therefore cannot be found.
Try placing your call to FindJObj *after* the figure has become visible and all the controls have completed rendering, for example by moving your FindJObj call to the *_OutputFcn function, hopefully after a call to drawnow to ensure that everything has completed rendering.
thx for quick reply, already tried method you suggest yesterday but had some troubles and noticeable delay before changes are applied. Especially if i try to change border and background at the same time, it only changes one property and i have to rerun it to change all… (changing foreground and background at the same time works)
Do you have any idea how to minimalize the delay before applying changes?
here is the code i use
**************************************************************