Code covered by the BSD License  

Highlights from
Set figure window to be always on top.

4.0

4.0 | 4 ratings Rate this file 10 Downloads (last 30 days) File Size: 1.8 KB File ID: #14103

Set figure window to be always on top.

by Per-Anders Ekstrom

 

27 Feb 2007 (Updated 27 Feb 2007)

Changes the always-on-top window state.

| Watch this File

File Information
Description

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.

SETALWAYSONTOP(HFIG) is the same as SETALWAYSONTOP(HFIG,TRUE).

Second boolean argument TRUE/FALSE can also be exchanged to numerical 1/0.
    
Restrictions:
HFIG must have property value: Visible = on.
HFIG must have property value: WindowStyle = normal.
Swing components must be available in the current Matlab session.
    
Example:
    hFig = figure;
    setAlwaysOnTop(hFig,true); % figure is now on top of other windows

MATLAB release MATLAB 7.2 (R2006a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (10)
28 Feb 2007 F Moisy

Simple but very useful. well done.

28 Feb 2007 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
fJFrame.fFigureClient.getWindow.setAlwaysOnTop(bool);

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

>> ishandle(gcf)
ans =
     1

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
fJFrame.fFigureClient.getWindow.setAlwaysOnTop(bool);

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

28 Feb 2007 Matthew Whitaker

See setWindowOnTop
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=11684&objectType=FILE

for an implementation that has been tested with R14SP2 through R2006b

28 Feb 2007 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.

28 Feb 2007 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!!

09 Jul 2007 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
fJFrame.fFigureClient.getWindow.setAlwaysOnTop(bool);

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

pause( 0.1 );
fJFrame.fFigureClient.getWindow.setAlwaysOnTop(bool);

14 Aug 2009 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.

27 Aug 2010 Albert

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?

31 Aug 2010 Per-Anders Ekstrom

Albert, yes it is. The line:

fJFrame.fFigureClient.getWindow.isAlwaysOnTop()

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.

19 Jun 2011 srajan

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

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
annotation Per-Anders Ekstrom 22 Oct 2008 09:02:09
customization Per-Anders Ekstrom 22 Oct 2008 09:02:09
figure Per-Anders Ekstrom 22 Oct 2008 09:02:09
set Per-Anders Ekstrom 22 Oct 2008 09:02:09
window Per-Anders Ekstrom 22 Oct 2008 09:02:10
always on top Per-Anders Ekstrom 22 Oct 2008 09:02:10

Contact us at files@mathworks.com