focus
Syntax
Description
focus(
gives keyboard focus to the UI
component c
)c
.
Calling focus
on a UI component has these effects:
The figure containing the component is displayed on top of all other figures.
The UI component is displayed with a blue focus ring.
App users can interact with the UI component using the keyboard.
Examples
Focus UI Component
Create a UI figure with a label, drop-down component, and two buttons. Make the OK button the default button by giving it keyboard focus. The user can push the button by pressing Enter or the space bar.
fig = uifigure(Position=[500 500 300 100]); gl = uigridlayout(fig,[2 2]); lbl = uilabel(gl,Text="Select a color:"); dd = uidropdown(gl,Items=["Red" "Green" "Blue"]); btn1 = uibutton(gl,Text="OK"); btn2 = uibutton(gl,Text="Cancel"); focus(btn1)
Change Focus in Response to User Interaction
Create a new script file in your current folder. In the script, create a UI figure
and a grid layout manager with two rows. Add a button to the first row, and specify that
the app executes a callback function named createTextArea
when a user
pushes the button. Give keyboard focus to the button.
fig = uifigure(Position=[500 500 300 200]); gl = uigridlayout(fig); gl.RowHeight = ["1x" "3x"]; gl.ColumnWidth = "1x"; btn = uibutton(gl,Text="Enter Comment", ... ButtonPushedFcn=@createTextArea); focus(btn)
Define the createTextArea
function at the bottom of the file. In
the function, create a text area in the second row of the grid layout manager. Then,
give keyboard focus to the text area.
function createTextArea(src,event) gl = src.Parent; ta = uitextarea(gl); focus(ta) end
Run the script. Press Enter to execute the
ButtonPushedFcn
callback, and the text area appears with keyboard
focus. You can then type in the text area.
Input Arguments
c
— UI component to focus
focusable UI component
UI component to focus, such as a Button
or
EditField
object.
Focusable UI components are those that users can interact with by using a keyboard.
Some UI components are not focusable, including most containers, as well as components
with Enable
or Visible
set to
'off'
.
Version History
Introduced in R2022a
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)