Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
javacomponent ( tab constructor )

Subject: javacomponent ( tab constructor )

From: Jens

Date: 15 Aug, 2011 21:35:10

Message: 1 of 6

Hello,

i am trying to work with the "javacomponent" command creating a tabgroup with several panels on it. The code, which is not working seems like this:

f=figure();
[jTabbedPane, jTabbedPaneC] = javacomponent(javax.swing.JTabbedPane(),[0 0 400 400],f);
[jPanel1,jPanel1C] = javacomponent(javax.swing.JPanel,[],jTabbedPaneC);
[jPanel2,jPanel2C] = javacomponent(javax.swing.JPanel,[],jTabbedPaneC);
[jPanel3,jPanel3C] = javacomponent(javax.swing.JPanel,[],jTabbedPaneC);
[jLabel1,jLabel1C] = javacomponent(javax.swing.JLabel('Tab 1'),[],jPanel1C);
[jLabel2,jLabel2C] = javacomponent(javax.swing.JLabel('Tab 2'),[],jPanel2C);
[jLabel3,jLabel3C] = javacomponent(javax.swing.JLabel('Tab 3'),[],jPanel3C);

button = uicontrol('style','pushbutton','parent',jPanel1C);

jPanel1.add(jLabel1);
jPanel2.add(jLabel2);
jPanel3.add(jLabel3);
jTabbedPane.add(jPanel1);
jTabbedPane.add(jPanel2);
jTabbedPane.add(jPanel3);

It does not work at all and i really dont know why!

I know the methods using "uitabgroup" and "uitab" as well as you should know that i want to avoid this method. I observed, that adding new tabs using the "uitab" command is very slow in deployed applications. It takes nearly a second to update the tabgroup. And that is really anoying.

Does anyone know how to deal with this problem using "directly" the underlaying javacomponent functions to create a fast and professional look and feel uitabgroup?

Subject: javacomponent ( tab constructor )

From: Steven_Lord

Date: 16 Aug, 2011 21:21:19

Message: 2 of 6



"Jens" <alpha60@freenet.de> wrote in message
news:j2c3ee$4r0$1@newscl01ah.mathworks.com...
> Hello,
>
> i am trying to work with the "javacomponent" command creating a tabgroup
> with several panels on it. The code, which is not working seems like this:
>
> f=figure();
> [jTabbedPane, jTabbedPaneC] = javacomponent(javax.swing.JTabbedPane(),[0 0
> 400 400],f);
> [jPanel1,jPanel1C] = javacomponent(javax.swing.JPanel,[],jTabbedPaneC);
> [jPanel2,jPanel2C] = javacomponent(javax.swing.JPanel,[],jTabbedPaneC);
> [jPanel3,jPanel3C] = javacomponent(javax.swing.JPanel,[],jTabbedPaneC);
> [jLabel1,jLabel1C] = javacomponent(javax.swing.JLabel('Tab
> 1'),[],jPanel1C);
> [jLabel2,jLabel2C] = javacomponent(javax.swing.JLabel('Tab
> 2'),[],jPanel2C);
> [jLabel3,jLabel3C] = javacomponent(javax.swing.JLabel('Tab
> 3'),[],jPanel3C);
>
> button = uicontrol('style','pushbutton','parent',jPanel1C);
>
> jPanel1.add(jLabel1);
> jPanel2.add(jLabel2);
> jPanel3.add(jLabel3);
> jTabbedPane.add(jPanel1);
> jTabbedPane.add(jPanel2);
> jTabbedPane.add(jPanel3);
>
> It does not work at all and i really dont know why!

What do you mean by "does not work at all" -- does it throw a warning, does
it throw an error, does it close the figure window, does it crash MATLAB,
does it shut down your operating system, does it cause your computer to
burst into flames, etc.?

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: javacomponent ( tab constructor )

From: Jens

Date: 17 Aug, 2011 15:48:09

Message: 3 of 6

Hi,

fortunatly this code snippet is not as harmful at it seems to be.

As i unterstand the javacomponent function in above case it should create a certain java swing object placing it in a specific parent handle with postion p and returns the java handle and its hg container.

[javaHandle, hgContainer] = javacomponent(swingObject, position, parentHandle);

The way i thought it should work:
1. Create a figure object.
2. Create a java object (jTabbedPane) and place it in the figure object.
3. Create a java object (jPanel) and place it in the jTabbedPane object.
4. Create a java object (jLabel) and place it in the jPanel object.
5. Add the jPanel object to the jTabbedPane object.
-> A tabbedPane with a single tab named like defined while creating the jPanel object is placed on the figure object.

Code:
1. f=figure();
2. [jTabbedPane, jTabbedPaneC] = javacomponent(javax.swing.JTabbedPane(),[0 0 400 400],f);
3. [jPanel,jPanelC] = javacomponent(javax.swing.JPanel,[],jTabbedPaneC);
4. [jLabel,jLabelC] = javacomponent(javax.swing.JLabel('Tab 1'),[],jPanelC);
5. jTabbedPane.addTab(jPanel);

The jPanel isn't named like defined in jLabel. Placing jLabel in jPanel seems not to work. But this is not critical, there are many ways to solve this problem:

Code:
1. f=figure();
2. [jTabbedPane, jTabbedPaneC] = javacomponent(javax.swing.JTabbedPane(),[0 0 400 400],f);
3. [jPanel,jPanelC] = javacomponent(javax.swing.JPanel,[],jTabbedPaneC);
4. jPanel.setName('Tab 1');pause(0.1);
5. jTabbedPane.add(jPanel);

This works fine! Unfortunately i missed to talk about the bigger problem:

--> How to place an uicontrol like an uipanel or axes to this java created tab? <--

My first intention was using the returned hg container handle while creating the java object with the javacomponent command and define this handle as parent handle for the specific object created with the common matlab coding commands.
e.g.
...
6. matPanel = uipanel('parent',jPanelC,'units','normalized','position',[0 0 1 1],'BackgroundColor','white');
7. myAxes = axes('parent',matPanel);

Returning neither warning nor error messages and indeed returning a valid handle to the uicontrol created object and even valid object itself, it does not appear on the parent handle.

Subject: javacomponent ( tab constructor )

From: Yair Altman

Date: 17 Aug, 2011 20:25:10

Message: 4 of 6

Unfortunately, AFAIK, you cannot add Matlab controls like axes or uipanels to a Java container. On the other hand, you can use the semi-documented (and unsupported) built-in functions uitab and uitabgroup to achieve a visual illusion of exactly this.

Se description here:
http://UndocumentedMatlab.com/blog/tab-panels-uitab-and-relatives/
http://UndocumentedMatlab.com/blog/uitab-customizations/
http://UndocumentedMatlab.com/blog/uitab-colors-icons-images/

Yair Altman
http://UndocumentedMatlab.com

Subject: javacomponent ( tab constructor )

From: Jens

Date: 18 Aug, 2011 16:37:09

Message: 5 of 6

Hello Yari,

first of all i have to take some time to congratulate and thank you to/for your great work on your website. I know it since a couple of weeks and as far as i think it is the most useful page in the web dealing with matlab and matlab code and real problems trying to get some great solutions and workarounds instead of handling all the basic stuff like many other sites do. ( For those problems its seems to be better getting known of the great MATLAB Help Documentation - in my opinion one of the greatest Documentation of commercial developing tools ). Enough! :)

You are right, i could decide working with the uitabgroup and uitab commands.
But please consider the following code snippet:
___________________________________________________________________

function easyTab()

handles.form1 = [];
handles.toolbar1 = [];
handles.toolbutton1 = [];
handles.tabgroup1 = [];
handles.tabs = zeros(1,21);
handles.panels = zeros(1,21);

handles.form1 = figure('name','easyTab','toolbar','none',...
                       'menubar','none','NumberTitle','off');
                   
handles.toolbar1 = uitoolbar('parent',handles.form1);

handles.toolbutton1 = uipushtool('parent',handles.toolbar1,...
                                 'cdata',rand(16,16,3));
                             
warning off MATLAB:uitabgroup:OldVersion

handles.tabgroup1 = uitabgroup('parent',handles.form1,...
                               'units','normalized','position',[0 0 1 1]);
                           
handles.tabs(1) = uitab('parent',handles.tabgroup1,'title','Tab 1');

handles.panels(1) = uipanel('parent',handles.tabs(1),...
                            'units','normalized',...
                            'position',[0 0 1 1],...
                            'BackgroundColor','white');
                        
handles.tabs(1,21) = 1; %Number of tabs already added
handles.panels(1,21) = 1; %Number of panels already added
                          %Not necessary at all ( always add a panel on a
                          %tab -> number of panels = number of tabs )
                          
guidata(handles.form1,handles);
set(handles.toolbutton1,'ClickedCallback',{@ClickedCallback_toolbutton1,handles});
%--------------------------------------------------------------------------
function ClickedCallback_toolbutton1(hObject, eventdata, handles)
%This function creates every time toolbutton1 is pressed a new tab with
%panel until the max number of tabs/panels ( 20 ) has been reached!

handles = guidata(handles.form1);

tabcount = handles.tabs(1,21);
tabcount_max = 20;

if tabcount >= tabcount_max, return;end

startTime = tic;
handles.tabs(1,tabcount+1) = uitab('parent',handles.tabgroup1,...
                                   'title',['Tab ' num2str(tabcount+1)]);
                               
handles.panels(1,tabcount+1) = uipanel('parent',handles.tabs(1,tabcount+1),...
                                       'units','normalized',...
                                       'position',[0 0 1 1],...
                                       'BackgroundColor','white');
                                   
handles.tabs(1,21) = tabcount + 1;
handles.panels(1,21) = tabcount + 1;
guidata(handles.form1, handles);

elapsedTime = toc(startTime);
set(handles.form1,'name',['easyTab | time elapsed adding new tab: '...
                          num2str(elapsedTime) ' s']);
___________________________________________________________________

When running the code adding some new tabs by clicking the toolbutton the figure appears very very slowely to be updated.

Even more in deployed applications by using the >>mcc -m easyTab
command.

If anyone has the time and passion to run the code or even more to run the deployable application outside Matlab i would be very very happy.

Thx

Subject: javacomponent ( tab constructor )

From: Jens

Date: 21 Aug, 2011 13:24:08

Message: 6 of 6

Hello,

the problem seems to be solved. Just adding a "drawnow" after adding a new tab with
the uitab command refreshes the tabgroup "instantly".

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us