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:
java and matlab

Subject: java and matlab

From: Robert

Date: 17 Aug, 2010 07:44:07

Message: 1 of 5

I'm trying to build a gui with java swing and add a menu there:

import javax.swing.*
theTestWindow = JFrame('My Tool');
theTestWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
theTestWindow.setSize(300, 200);
theTestWindow.setVisible(1);
theMenuBar = JMenuBar();
theTestWindow.setJMenuBar(theMenuBar);
theTestMenu = JMenu('File');
theMenuBar.add(theTestMenu);
theNewTestMenuItem = JMenuItem('New');
theTestMenu.add(theNewTestMenuItem);

that's OK so far, but to add some functionality to the menu, I need to add an ActionListener to the JMenuItem. This is done with the addActionListener method:

theNewTestMenuItem.addActionListener(objectInQuestion)

But which ActionListener could I add? The objectInQuestion has to implement the ActionListener interface and should implement the abstract method actionPerformed(ActionEvent evt), calling my callback mfunction theNewTestMenuItemCallback from there. The java code for class and method should be something like

public class ClassOfObjectInQuestion implements ActionListener {
public void actionPerformed(ActionEvent evt) {
}
}

But how can I kick off the callback from there?

TIA,

     Robert

Subject: java and matlab

From: Yair Altman

Date: 17 Aug, 2010 14:40:21

Message: 2 of 5

"Robert " <notrust@lummert.net> wrote in message <i4dek7$smr$1@fred.mathworks.com>...
> I'm trying to build a gui with java swing and add a menu there:
>
> import javax.swing.*
> theTestWindow = JFrame('My Tool');
> theTestWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
> theTestWindow.setSize(300, 200);
> theTestWindow.setVisible(1);
> theMenuBar = JMenuBar();
> theTestWindow.setJMenuBar(theMenuBar);
> theTestMenu = JMenu('File');
> theMenuBar.add(theTestMenu);
> theNewTestMenuItem = JMenuItem('New');
> theTestMenu.add(theNewTestMenuItem);
>
> that's OK so far, but to add some functionality to the menu, I need to add an ActionListener to the JMenuItem. This is done with the addActionListener method:
>
> theNewTestMenuItem.addActionListener(objectInQuestion)
>
> But which ActionListener could I add? The objectInQuestion has to implement the ActionListener interface and should implement the abstract method actionPerformed(ActionEvent evt), calling my callback mfunction theNewTestMenuItemCallback from there. The java code for class and method should be something like
>
> public class ClassOfObjectInQuestion implements ActionListener {
> public void actionPerformed(ActionEvent evt) {
> }
> }
>
> But how can I kick off the callback from there?
>
> TIA,
>
> Robert


Look here: http://undocumentedmatlab.com/blog/adding-context-menu-to-uitree/

Yair Altman
http://UndocumentedMatlab.com

Subject: java and matlab

From: serenecrue

Date: 17 Aug, 2010 15:54:30

Message: 3 of 5

I am not that much good in math and math-lab but willing to learn.Are you free to talk about that?.

[url=http://www.stop-painting.com/pavement-marking.html] pavement marking [/url]

Subject: java and matlab

From: Robert

Date: 17 Aug, 2010 16:24:05

Message: 4 of 5

Thanks Yair, that does the trick!

set(theNewTestMenuItem, 'ActionPerformedCallback', @theNewTestMenuItemCallback);

Though, not just out of curiosity, but also to understand the mechanics behind: How's Matlab's "get" feeding the JMenuItem to call the mfunction? I already had a look at some java classes defined in util.jar but couldn't find something promising there.

TIA,

     Robert

Subject: java and matlab

From: Yair Altman

Date: 17 Aug, 2010 17:41:04

Message: 5 of 5

"Robert " <notrust@lummert.net> wrote in message <i4ed35$m7f$1@fred.mathworks.com>...
> Thanks Yair, that does the trick!
>
> set(theNewTestMenuItem, 'ActionPerformedCallback', @theNewTestMenuItemCallback);
>
> Though, not just out of curiosity, but also to understand the mechanics behind: How's Matlab's "get" feeding the JMenuItem to call the mfunction? I already had a look at some java classes defined in util.jar but couldn't find something promising there.
>
> TIA,
>
> Robert


Matlab's ActionPerformedCallback is actually a wrapper that traps the ActionPerformed event raised by the Java object. Matlab automatically wraps public Java events in similar Matlab callbacks.

You may be interested in reading the following related articles:
http://UndocumentedMatlab.com/blog/uicontrol-callbacks/
http://UndocumentedMatlab.com/blog/continuous-slider-callback/

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