Customizing uicontrol border

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 =$hgTextField
>> jEdit.Border  % check the control's border
ans =$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 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 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.

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

Tags: , , ,

Bookmark and SharePrint Print

14 Responses to Customizing uicontrol border

  1. Jos von Asmuth says:

    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 – 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

  2. marco says:

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

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

    • marco says:

      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?

  3. Lemikainen says:

    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.

    • Lemikainen says:

      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);
  4. liu says:

    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);
    • @Liu – why not simply move the uistack command before findjobj, then modify the border?

    • liu says:

      😉 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!

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

    • liu says:

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *