- Undocumented Matlab - https://undocumentedmatlab.com -

Customizing uicontrol border

Posted By Yair Altman On October 31, 2010 | 15 Comments

Today, a CSSM user posted [1] 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 [2]. 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 =
>> jEdit.Border  % check the control's border
ans =
>> 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 regular border
editbox with custom 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 [3] or any decent Swing textbook.
We can use the same technique to remove the uicontrol’s borders altogether:

jEdit.Border = [];

editbox with no 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 [4].

Categories: GUI, Handle graphics, Java, Medium risk of breaking in future versions

15 Comments (Open | Close)

15 Comments To "Customizing uicontrol border"

#1 Comment By Jos von Asmuth On November 3, 2010 @ 01:02

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,

#2 Comment By Yair Altman On November 3, 2010 @ 12:41

@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

#3 Comment By marco On August 31, 2011 @ 00:52

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

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])

#4 Comment By Yair Altman On August 31, 2011 @ 04:24

@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.

#5 Comment By marco On August 31, 2011 @ 05:23

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?

#6 Comment By Lemikainen On February 1, 2012 @ 09:44

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?

#7 Comment By Yair Altman On February 1, 2012 @ 09:49

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.

#8 Comment By Lemikainen On February 2, 2012 @ 02:35

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

lineColor = java.awt.Color(1,1,1); 
thickness = 1; 
roundedCorners = false;
newBorder = javax.swing.border.LineBorder(lineColor,thickness,roundedCorners);

jButton = findjobj('class','pushbutton');       

for i=1:1:length(jButton)
   set(jButton(i), 'Background', backgroundColor, 'Foreground', foregroundColor, 'Border', newBorder);

#9 Comment By liu On May 2, 2015 @ 15:43

Hello. I noticed that ‘uistack’ call cancels border changes made with the suggested java method. Is there a way to change stack order but still keep customized border?
I’m trying ‘findjobj’ & ‘setComponentZOrder’ combination. The latter works well, but when I attempt to close the figure I get ‘NullPointerException’ error. See the code below. (Thanks a lot!!)

% --- Figure with listbox and editbox
f = figure; 
hList = uicontrol(f,'style','listbox','position',[10 10 400 20]);  
hEdit = uicontrol(f,'style','edit','position',[10 10 200 50]); 

% --- Change border of the editbox
jEdit = findjobj(hEdit);
jEdit.Border = javax.swing.border.LineBorder(java.awt.Color(0.3,0.3,0.3),3,true);

% --- Method 1 - listbox to front - cancels border change

% --- Method 2 - listbox to front - java error on figure closing
jObj = findjobj(hList); 
jPar = jObj.getParent;
jPar.getParent().setComponentZOrder(jPar, 0);

#10 Comment By Yair Altman On May 2, 2015 @ 15:46

@Liu – why not simply move the uistack command before findjobj, then modify the border?

#11 Comment By liu On May 2, 2015 @ 16:08

😉 Unfortunately, it is impossible in the actual code, where I need this. I’ve shown a very simplified demo-script. The actual code does not allow to permute those operations. Is there a way to fix ‘z-Order’ solution in the previous code? Thank you!

#12 Comment By Yair Altman On May 2, 2015 @ 16:13

I do not accept your claim. You can always store the findjobj-ed handle in some variable and then use it immediately after uistack to reset the border.
I don’t know what the specific problem with the z-order is.
If you wish me to spend some time to investigate this, contact me by email for a private consulting.

#13 Comment By liu On May 2, 2015 @ 18:10

What happens is that depending on button presses editboxes are dynamically created, customized, removed all the time.

Essentially, I just need to keep new ones at the back of old components. The problem is that new ones always appear on top, and a single ‘uistack’ call removes all previous border changes.

To cut the long way with codes, could you give a quick tip (or keywords) on:
Is it possible to add new components right to the back with java? Or can we set some java ‘always on top’ property? (similar to TooltipString behavior)

(Thanks a lot for all your time and prompt response!)

#14 Comment By Yair Altman On May 2, 2015 @ 23:46

As I said, If you wish me to spend some time to investigate this, contact me by email for a private consulting.

#15 Comment By Patrick Fitzgerald On February 28, 2021 @ 08:37

Just a heads up to anyone else digging around for help related to this: if you assign BackrgoundColor to a pushbutton-style uicontrol, it seems to prevent you from changing the borders. Set the color with CData manually.

Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/customizing-uicontrol-border

URLs in this post:

[1] posted: https://www.mathworks.com/matlabcentral/newsreader/view_thread/295214

[2] FindJObj utility: http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/

[3] official documentation of Java Borders: http://download.oracle.com/javase/tutorial/uiswing/components/border.html

[4] post a comment: http://undocumentedmatlab.com/blog/customizing-uicontrol-border/#respond

[5] Customizing editboxes : https://undocumentedmatlab.com/articles/customizing-editboxes

[6] Customizing listbox & editbox scrollbars : https://undocumentedmatlab.com/articles/customizing-listbox-editbox-scrollbars

[7] Additional uicontrol tooltip hacks : https://undocumentedmatlab.com/articles/additional-uicontrol-tooltip-hacks

[8] Setting line position in an edit-box uicontrol : https://undocumentedmatlab.com/articles/setting-line-position-in-edit-box-uicontrol

[9] Uicontrol callbacks : https://undocumentedmatlab.com/articles/uicontrol-callbacks

[10] Customizing Matlab labels : https://undocumentedmatlab.com/articles/customizing-matlab-labels

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.