File Exchange

image thumbnail

Set figure window to be always on top.

version (1.8 KB) by Per-Anders Ekstrom
Changes the always-on-top window state.


Updated 27 Feb 2007

View License

SETALWAYSONTOP(HFIG,TRUE) will make Matlab figure with handle HFIG to be on top of other windows in the OS even though it might not be in focus.

SETALWAYSONTOP(HFIG,FALSE) will put figure back to normal window state.


Second boolean argument TRUE/FALSE can also be exchanged to numerical 1/0.

HFIG must have property value: Visible = on.
HFIG must have property value: WindowStyle = normal.
Swing components must be available in the current Matlab session.

hFig = figure;
setAlwaysOnTop(hFig,true); % figure is now on top of other windows

Cite As

Per-Anders Ekstrom (2021). Set figure window to be always on top. (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (11)


It looks like author is not supporting this submission any more. In R2013a it produces an error.
The fix is quite simple, but I've decided to upload my own submission, quite similar to this.
(See "Acknowledgements" section above.)


i am making a project for windows control using voice commands
i want my main figure of gui always on top even after opening any window from my voice command
pls help me
thanks in advance

Per-Anders Ekstrom

Albert, yes it is. The line:


will return boolean true/false if it is set to true or not.
The Java object fJFrame you'll obtain doing the same procedure as in the source code of this submission.


Thanks for sharing this. While it is really usefull I still have a question.

Is there a way to check if SetAlwaysOnTop is already set to true?

Christophe Charbu

Well done. Thank you.

I have tested on the (R2007b) (R2008b)
and it works good.

PS: In the release R2008b, a warning occur :
Warning: figure JavaFrame property will be obsoleted in a future release.

Reid Pulley

This function worked if I stepped through the code, but not running free - I kept getting this error:

??? Attempt to reference field of non-structure array.

Error in ==> setAlwaysOnTop at 54

I inserted a slight pause in the code and it worked great.

pause( 0.1 );

Matthew Whitaker

The trick that I found for setWindowOnTop was to do that Java calls inside awtinvoke which 'invokes the method on the AWT event thread'

This made it a lot more stable. I was not aware of the expose option for drawnow. learn something everyday!!

Per-Anders Ekström

Comment 1. Thank you.

Comment 2. I have not tested this utility using R2006b, if I had I would have written that under the MATLAB Release. Could be the problem since Mathworks might have modified their graphics code. For me the code works under both "Windoze" and OS X.

I find it very strange that you get caught in the 'fighandle' error check though. The error is only invoked if:
~ishandle(hFig) % this you have already tested.
length(hFig)~=1 % since figure handle is a scalar this cannot be the case.
~strcmp('figure',get(hFig,'Type')) % according to documentation, for figures, Type is always the string 'figure'.

What am I'm missing?

Comment 3. I had no idea that there existed a code that did this thing already, my bad. The "core" in your code does exactly the same thing as mine.

Matthew Whitaker

See setWindowOnTop

for an implementation that has been tested with R14SP2 through R2006b

John D'Errico

I was expecting to give this a good review. So I tried it out, only to see the following:

>> plot(1:5)
>> setAlwaysOnTop(gcf)

??? Error using ==> setAlwaysOnTop
First Arg. Must be a Figure-Handle

Error in ==> setAlwaysOnTop at 54

I'm fairly confident that gcf is a figure handle. ;-)

>> ishandle(gcf)
ans =

I tried the example shown by the author too:

>> h = figure;
>> setAlwaysOnTop(h,true)
??? Error using ==> setAlwaysOnTop
First Arg. Must be a Figure-Handle

Error in ==> setAlwaysOnTop at 54

All testing was done in R2006b. If I have to hazard a guess, there is an undocumented requirement of Windoze to run this utility.

F Moisy

Simple but very useful. well done.

MATLAB Release Compatibility
Created with R2006a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: WinOnTop

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!