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 =  ...
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
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 = ;
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.