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:
adding items to a java panel?

Subject: adding items to a java panel?

From: Francki

Date: 3 Nov, 2010 21:13:04

Message: 1 of 3

hi guys,

basically I've this code (I want the UI to have some java components):

  if ~usejava('jvm')
      error([mfilename ' requires Java to run.']);
  end
   
  if usejava('swing')
  
   figH=figure('Name','UITEST',...
               'Menubar','none',...
               'NumberTitle','off',...
               'Resize','off',...
               'Position',[360 278 320 320],...
               'Tag','sfig',...
               'ToolBar','none');
           
  panel=awtcreate('javax.swing.JPanel');
 [r,p]=javacomponent(panel,[6,6,310,305],figH);
  Lb=javax.swing.JLabel('Selected ".sef" File(s):') ;
  javacomponent(Lb,[8 280 120 25],p);
  Bt=javax.swing.JButton('Clear File(s)');
  javacomponent(Bt,[8,200 50 30],p);

end

The code works but the problem is the label and the button do not come up in the position specified but instead at the bottom of the panel. Note I do note want the label and the button on "figH" but on "panel". how can I code it so that they come up at the shown position in the above code?......any help would be massively appreciated.

Subject: adding items to a java panel?

From: Ed Yu

Date: 13 Dec, 2010 17:45:23

Message: 2 of 3

What is the unit of your figure... Is it normalized or pixels...

Good luck!
Ed.

"Francki " <jonesfranckandi@yahoo.fr> wrote in message <iasj90$5dv$1@fred.mathworks.com>...
> hi guys,
>
> basically I've this code (I want the UI to have some java components):
>
> if ~usejava('jvm')
> error([mfilename ' requires Java to run.']);
> end
>
> if usejava('swing')
>
> figH=figure('Name','UITEST',...
> 'Menubar','none',...
> 'NumberTitle','off',...
> 'Resize','off',...
> 'Position',[360 278 320 320],...
> 'Tag','sfig',...
> 'ToolBar','none');
>
> panel=awtcreate('javax.swing.JPanel');
> [r,p]=javacomponent(panel,[6,6,310,305],figH);
> Lb=javax.swing.JLabel('Selected ".sef" File(s):') ;
> javacomponent(Lb,[8 280 120 25],p);
> Bt=javax.swing.JButton('Clear File(s)');
> javacomponent(Bt,[8,200 50 30],p);
>
> end
>
> The code works but the problem is the label and the button do not come up in the position specified but instead at the bottom of the panel. Note I do note want the label and the button on "figH" but on "panel". how can I code it so that they come up at the shown position in the above code?......any help would be massively appreciated.

Subject: adding items to a java panel?

From: Yair Altman

Date: 14 Dec, 2010 16:42:05

Message: 3 of 3

"Francki " <jonesfranckandi@yahoo.fr>...
>
> panel=awtcreate('javax.swing.JPanel');
> [r,p]=javacomponent(panel,[6,6,310,305],figH);
> Lb=javax.swing.JLabel('Selected ".sef" File(s):') ;
> javacomponent(Lb,[8 280 120 25],p);
> Bt=javax.swing.JButton('Clear File(s)');
> javacomponent(Bt,[8,200 50 30],p);
>
> end
>
> The code works but the problem is the label and the button do not come up in the position specified but instead at the bottom of the panel. Note I do note want the label and the button on "figH" but on "panel". how can I code it so that they come up at the shown position in the above code?......any help would be massively appreciated.


The short answer is that this appears to be a bug in Matlab's implementation of the hgjavacomponent container. This container can indeed be specified as a parent to other javacomponents, but the position vector will always refer to the containing frame.

There are two workarounds:
1. Since you're only using Java components, you can create your Java panel completely before displaying it, then simply display the top container using javacomponent.
2. You can place the Java controls as children of a Matlab uipanel rather than as children of a Java panel.

Yair Altman
http://UndocumentedMatlab.com

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