Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
|
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
In a previous post I showed how we can create custom Matlab app toolstrips. Toolstrips can be a bit complex to develop so I’m trying to proceed slowly, with each post in the miniseries building on the previous posts. I encourage you to review the earlier posts in the Toolstrip miniseries before reading this post. In today’s post we continue the discussion of the toolstrip created in the previous post:
![]() Today’s post will show how to attach user-defined functionality to toolstrip components, as well as some additional customizations. At the end of today’s article, you should be able to create a fully-functional custom Matlab toolstrip. Today’s post will remain within the confines of a Matlab “app”, i.e. a tool-group that displays docked figures. Future posts will discuss lower-level toolstrip mechanisms, that enable advanced customizations as well as integration in legacy (Java-based, even GUIDE-created) Matlab figures. Control callbacks Controls are useless without settable callbacks that affect the program state based on user interactions. There are two different mechanisms for setting callbacks for Matlab toolstrip controls. Refer to the example in the previous post:
Component text labels, where relevant, can be set using the component’s Text property, and the tooltip can be set via the Description property. As I noted in my previous post, I believe that this is an unfortunate choice of property names. In addition, components have control-specific properties such as Value (checkboxes and toggle buttons). These properties can generally be modified in runtime, in order to reflect the program state. For example, we can disable/enable controls, and modify their label, tooltip and state depending on the control’s new state and the program state in general. The component icon can be set via the Icon property, where available (for example, buttons have an icon, but checkboxes do not). There are several different ways in which we can set this Icon. I will discuss this in detail in the following post; in the meantime you can review the usage examples in the previous post. There are a couple of additional hidden component properties that seem promising, most notably Shortcut and Mnemonic (the latter (Mnemonic) is also available in Section and Tab, not just in components). Unfortunately, at least as of R2018b these properties do not seem to be connected yet to any functionality. In the future, I would expect them to correspond to keyboard shortcuts and underlined mnemonic characters, as these functionalities behave in standard menu items. Accessing the underlying Java control As long as we’re not displaying the toolstrip on a browser page (i.e., inside a uifigure or Matlab Online), the toolstrip is basically composed of Java Swing components from the com.mathworks.toolstrip.components package (such as TSButton or TSCheckBox). I will discuss these Java classes and their customizations in a later post, but for now I just wish to show how to access the underlying Java component of any Matlab MCOS control. This can be done using a central registry of toolstrip components (so-called “widgets”), which is accessible via the ToolGroup‘s hidden ToolstripSwingService property, and then via each component’s hidden widget Id. For example: >> widgetRegistry = hToolGroup.ToolstripSwingService.Registry;>> jButton = widgetRegistry.getWidgetById(hButton.getId) % get the hButton's underlying Java controlans =com.mathworks.toolstrip.components.TSToggleButton[,"Colorbar",layout,NORMAL] We can now apply a wide variety of Java-based customizations to the retrieved jButton, as I have shown in many other articles on this website over the past decade. Another way to access the toolstrip Java component hierarchy is via hToolGroup.Peer.get(tabIndex).getComponent. This returns the top-level Java control representing the tab whose index in tabIndex (0=left-most tab): >> jToolGroup = hToolGroup.Peer; % or: =hToolGroup.ToolstripSwingService.SwingToolGroup;>> jDataTab = jToolGroup.get(0).getComponent; % Get tab #0 (first tab: "Data")>> jDataTab.list % The following is abridged for brevitycom.mathworks.toolstrip.impl.ToolstripTabContentPanel[tab0069230a-52b0-4973-b025-2171cd96301b,0,0,831x93,...]SectionWrapper(section54fb084c-934d-4d31-9468-7e4d66cd85e5)com.mathworks.toolstrip.impl.ToolstripSectionComponentWithHeader[,0,0,241x92,...]com.mathworks.toolstrip.components.TSPanel[section54fb084c-934d-4d31-9468-7e4d66cd85e5,,layout,NORMAL]TSColumn -> layout :com.mathworks.toolstrip.components.TSButton[,"Refresh all",layout,NORMAL]TSColumn -> layout :com.mathworks.toolstrip.components.TSButton[,"Refresh X,Y",layout,NORMAL]com.mathworks.toolstrip.components.TSButton[,"Refresh Y,Z",layout,NORMAL]TSColumn -> layout :com.mathworks.toolstrip.components.TSButton[,"Refresh X",layout,NORMAL]com.mathworks.toolstrip.components.TSButton[,"Refresh Y",layout,NORMAL]com.mathworks.toolstrip.components.TSButton[,"Refresh Z",layout,NORMAL]SectionWrapper(sectionebd8ab95-fd33-4a3d-8f24-152589713994)com.mathworks.toolstrip.impl.ToolstripSectionComponentWithHeader[,0,0,159x92,...]com.mathworks.toolstrip.components.TSPanel[sectionebd8ab95-fd33-4a3d-8f24-152589713994,,layout,NORMAL]TSColumn -> layout :com.mathworks.toolstrip.components.TSCheckBox[,"Axes borders",layout,NORMAL]com.mathworks.toolstrip.components.TSCheckBox[,"Log scaling",layout,NORMAL]com.mathworks.toolstrip.components.TSCheckBox[,"Inverted Y",layout,NORMAL]SectionWrapper(section01995bfd-61de-490f-aa22-de50bae1af75)com.mathworks.toolstrip.impl.ToolstripSectionComponentWithHeader[,0,0,125x92,...]com.mathworks.toolstrip.components.TSPanel[section01995bfd-61de-490f-aa22-de50bae1af75,,layout,NORMAL]TSColumn -> layout :com.mathworks.toolstrip.components.TSToggleButton[,"Legend",layout,NORMAL]TSColumn -> layout :com.mathworks.toolstrip.components.TSLabel[null," ",layout,NORMAL]com.mathworks.toolstrip.components.TSToggleButton[,"Colorbar",layout,NORMAL]com.mathworks.toolstrip.components.TSLabel[null," ",layout,NORMAL]com.mathworks.mwswing.MJButton[toolstrip.header.collapseButton,808,70,20x20,...] Toolstrip miniseries roadmap The next post will discuss icons, for both toolstrip controls as well as the ToolGroup app window. I plan to discuss complex components in subsequent posts. Such components include button-group, drop-down, listbox, split-button, slider, popup form, gallery etc. Following that, my plan is to discuss toolstrip collapsibility, the ToolPack framework, docking layout, DataBrowser panel, QAB (Quick Access Bar), underlying Java controls, and adding toolstrips to figures – not necessarily in this order. Have I already mentioned that Matlab toolstrips can be a bit complex? If you would like me to assist you in building a custom toolstrip or GUI for your Matlab program, please let me know. Happy New Year, everyone! Related posts:
更多... |
![]() |
![]() |