Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Customizing uicontrol border

October 31, 2010 15 Comments

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

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

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 = [];

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.

Related posts:

  1. Customizing editboxes – Matlab's editbox can be customized in many useful manners...
  2. Customizing listbox & editbox scrollbars – Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized....
  3. Additional uicontrol tooltip hacks – Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....
  4. 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....
  5. 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...
  6. 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...
FindJObj GUI Handle graphics Java
Print Print
« Previous
Next »
15 Responses
  1. Jos von Asmuth November 3, 2010 at 01:02 Reply

    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

    • Yair Altman November 3, 2010 at 12:41 Reply

      @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 August 31, 2011 at 00:52 Reply

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

    • Yair Altman August 31, 2011 at 04:24 Reply

      @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 August 31, 2011 at 05:23 Reply

      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 February 1, 2012 at 09:44 Reply

    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?

    • Yair Altman February 1, 2012 at 09:49 Reply

      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 February 2, 2012 at 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);
         
        backgroundColor=java.awt.Color(0.141,0.518,1);
        foregroundColor=java.awt.Color(1,1,1);
        jButton = findjobj('class','pushbutton');       
        length(jButton)
         
        for i=1:1:length(jButton)
           set(jButton(i), 'Background', backgroundColor, 'Foreground', foregroundColor, 'Border', newBorder);
        end

        lineColor = java.awt.Color(1,1,1); thickness = 1; roundedCorners = false; newBorder = javax.swing.border.LineBorder(lineColor,thickness,roundedCorners); backgroundColor=java.awt.Color(0.141,0.518,1); foregroundColor=java.awt.Color(1,1,1); jButton = findjobj('class','pushbutton'); length(jButton) for i=1:1:length(jButton) set(jButton(i), 'Background', backgroundColor, 'Foreground', foregroundColor, 'Border', newBorder); end

  4. liu May 2, 2015 at 15:43 Reply

    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);
    jEdit.repaint;             
     
    % --- Method 1 - listbox to front - cancels border change
    %uistack(hList,'top');
     
    % --- Method 2 - listbox to front - java error on figure closing
    jObj = findjobj(hList); 
    jPar = jObj.getParent;
    jPar.getParent().setComponentZOrder(jPar, 0);
    jPar.getParent().revalidate;

    % --- 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); jEdit.repaint; % --- Method 1 - listbox to front - cancels border change %uistack(hList,'top'); % --- Method 2 - listbox to front - java error on figure closing jObj = findjobj(hList); jPar = jObj.getParent; jPar.getParent().setComponentZOrder(jPar, 0); jPar.getParent().revalidate;

    • Yair Altman May 2, 2015 at 15:46 Reply

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

      • liu May 2, 2015 at 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!

      • Yair Altman May 2, 2015 at 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.

      • liu May 2, 2015 at 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!)

      • Yair Altman May 2, 2015 at 23:46

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

  5. Patrick Fitzgerald February 28, 2021 at 08:37 Reply

    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.

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitable (6) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top