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:
trapping clicked button on uitable

Subject: trapping clicked button on uitable

From: Bruce Elliott

Date: 9 Nov, 2007 17:44:02

Message: 1 of 9

I am using Yair Altman's very useful createTable.m to add a
table to a GUI created with GUIDE. I am using the control
buttons but need my DataChangedCallback to be able to
determine which uibutton was clicked (if that was what
triggered the call), so that the callback can do some
related house-keeping. I'm posting my solution in case it's
useful to anyone, or maybe someone can suggest a better way.

In each of the callbacks for the uibuttons in createTable.m
I added the line:

set(get(hObject,'parent'),'UserData',get(hObject,'tag'))

Whenever a button is clicked, the parent object of the
uitable has its UserData property set to the tag of the
uibutton. This can then be queried to find which button it was.

This works, but I don't really like it. It violates the
general principle that a child object shouldn't be changing
its parent's data. For example, the user of createTable.m
might want to use the parent object's UserData for another
purpose, but it will be damaged by the child's callback.

In my case, the parent object is simply a panel whose only
purpose is to define the position of the uitable, so it
works fine. I'd prefer a cleaner solution, however, e.g.
for the DataChangedCallback to be able to query the table to
find out which button was just clicked. I've tried using
gcbo.m, but all I get is the handle to the jtable.

Any suggestions would be welcome.

Subject: trapping clicked button on uitable

From: Yair Altman

Date: 10 Nov, 2007 16:10:54

Message: 2 of 9

"Bruce Elliott" <bruce.elliott@jhuapl.nospam.edu> wrote in
message <fh2692$s00$1@fred.mathworks.com>...
> I am using Yair Altman's very useful createTable.m to add a
> table to a GUI created with GUIDE. I am using the control
> buttons but need my DataChangedCallback to be able to
> determine which uibutton was clicked (if that was what
> triggered the call), so that the callback can do some
> related house-keeping. I'm posting my solution in case it's
> useful to anyone, or maybe someone can suggest a better way.
>
> In each of the callbacks for the uibuttons in createTable.m
> I added the line:
>
> set(get(hObject,'parent'),'UserData',get(hObject,'tag'))
>
> Whenever a button is clicked, the parent object of the
> uitable has its UserData property set to the tag of the
> uibutton. This can then be queried to find which button
it was.
>
> This works, but I don't really like it. It violates the
> general principle that a child object shouldn't be changing
> its parent's data. For example, the user of createTable.m
> might want to use the parent object's UserData for another
> purpose, but it will be damaged by the child's callback.
>
> In my case, the parent object is simply a panel whose only
> purpose is to define the position of the uitable, so it
> works fine. I'd prefer a cleaner solution, however, e.g.
> for the DataChangedCallback to be able to query the table to
> find out which button was just clicked. I've tried using
> gcbo.m, but all I get is the handle to the jtable.
>
> Any suggestions would be welcome.


Thanks for the compliments - I'm glad you find my submission
useful. You could try
setappdata(gcf,'tableButtonClicked',hObject) and get (&
clear) this value within the table's DataChangedCallback.
You can do the same using the table handle (not gcf).

Yair Altman
http://ymasoftware.com

Subject: trapping clicked button on uitable

From: Bruce Elliott

Date: 12 Nov, 2007 18:26:24

Message: 3 of 9

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message
<fh4l6e$5o8$1@fred.mathworks.com>...
> You could try
> setappdata(gcf,'tableButtonClicked',hObject) and get (&
> clear) this value within the table's DataChangedCallback.
> You can do the same using the table handle (not gcf).

Thanks! I like that much better; it does exactly what I'd
hoped for.

One note: I put the line
setappdata(mtable,'tableButtonClicked',hObject)
into each of the buttons callback functions embedded in
createTable.m, not in the dataChangedCallback in my parent
figure. Is that what you meant? The whole problem was that
I didn't have access to the button's handle from within the
dataChangedCallback, so I couldn't have used hObject there
(or, conversely, if I could have, then my problem would
already have been solved!).

- Bruce

Subject: Hiding the table made with createTable

From: Patrick Tai

Date: 15 Nov, 2008 20:34:02

Message: 4 of 9

Hello Mr. Yair Altman
Thank you very much for your MATLAB software for createTable.
I have been using it and it worked wonderfully. It is very well-done.

I have one problem to solve. I used your createTable in a GUI made with GUIDE. I put the MTable in a panel, with tag "tablePanel", which may be inside another panel. Then sometimes I want to hide the table. The command
       set(handles.tablePanel, 'Visible', 'off")
does not hide the table, it is still there!
Hiding the outer panel hides the panel and everything inside it except the table made with createTable! Very frustrating!

I will appreciate very much if you can suggest a solution.

Thank you.
Patrick Tai.

Subject: Hiding the table made with createTable

From: Yair Altman

Date: 15 Nov, 2008 21:59:03

Message: 5 of 9

"Patrick Tai" <patrick@cytel.com> wrote in message <gfnbnq$ncs$1@fred.mathworks.com>...
> Hello Mr. Yair Altman
> Thank you very much for your MATLAB software for createTable.
> I have been using it and it worked wonderfully. It is very well-done.
>
> I have one problem to solve. I used your createTable in a GUI made with GUIDE. I put the MTable in a panel, with tag "tablePanel", which may be inside another panel. Then sometimes I want to hide the table. The command
> set(handles.tablePanel, 'Visible', 'off")
> does not hide the table, it is still there!
> Hiding the outer panel hides the panel and everything inside it except the table made with createTable! Very frustrating!


createTable returns a handle to the table. Use this handle to hide the table:
hTable = createTable(...);
hTable.Visible=0; % or: set(hTable,'Visible',0) % 1 to display, 0 to hide

Yair Altman

Subject: Hiding the table made with createTable

From: Patrick Tai

Date: 18 Nov, 2008 16:36:03

Message: 6 of 9

Mr. Altman:
Thanks again for your reply. It worked well.
My program make a table, and update its content repeatedly.
However, I just realized that when I updated the table by calling createTable, I actually made another table overlaying on top of the old one! Thus, when I hide the table, I just hide the most recent one, the bottom ones are still displayed.
What I want, I think, is a way to remove the table made before making a new one.
Unless there is another way to easily update the table (change #rows/columns, change table contents).
Thank you very much again.
Patrick Tai

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <gfngn7$ad1$1@fred.mathworks.com>...
> "Patrick Tai" <patrick@cytel.com> wrote in message <gfnbnq$ncs$1@fred.mathworks.com>...
> > Hello Mr. Yair Altman
> > Thank you very much for your MATLAB software for createTable.
> > I have been using it and it worked wonderfully. It is very well-done.
> >
> > I have one problem to solve. I used your createTable in a GUI made with GUIDE. I put the MTable in a panel, with tag "tablePanel", which may be inside another panel. Then sometimes I want to hide the table. The command
> > set(handles.tablePanel, 'Visible', 'off")
> > does not hide the table, it is still there!
> > Hiding the outer panel hides the panel and everything inside it except the table made with createTable! Very frustrating!
>
>
> createTable returns a handle to the table. Use this handle to hide the table:
> hTable = createTable(...);
> hTable.Visible=0; % or: set(hTable,'Visible',0) % 1 to display, 0 to hide
>
> Yair Altman

Subject: Hiding the table made with createTable

From: Yair Altman

Date: 18 Nov, 2008 17:07:01

Message: 7 of 9

"Patrick Tai" <patrick@cytel.com> wrote in message...

> What I want, I think, is a way to remove the table made before making a new one.

You can simply use the delete() function on the table handle. However, this is a very awkward way to update a data table, so see below

> Unless there is another way to easily update the table (change #rows/columns, change table contents).

Of course there is, you just need to use some of the table's Java functionality for this. Look at the callback code used for createTable's Add and Delete buttons to see how to add/remove table rows. Modifying data can be done using the setValueAt() function. There are many many other functions that you can use - see here for a tutorial: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html. Don't be intimidated by the Java code - much of it can be used within Matlab as-is. You can look at some of my other File Exchange submissions for examples that manipulate a JTable in Matlab.

Yair

Subject: Modify the table column headers

From: Patrick Tai

Date: 5 Mar, 2009 22:17:52

Message: 8 of 9

Dear Mr. Altman:

Thanks for tips to change table cells, using jtable.setValuAt(val, r, c).

How do I change the texts of the column headers?
jtable.getColumnModel.getColumn(c).setHeaderValue('XYZ')
does not appear to work.

Thank you.
Patrick.

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <gfusnl$p23$1@fred.mathworks.com>...
> Of course there is, you just need to use some of the table's Java functionality for this. Look at the callback code used for createTable's Add and Delete buttons to see how to add/remove table rows. Modifying data can be done using the setValueAt() function. There are many many other functions that you can use - see here for a tutorial: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html. Don't be intimidated by the Java code - much of it can be used within Matlab as-is. You can look at some of my other File Exchange submissions for examples that manipulate a JTable in Matlab.
>
> Yair

Subject: Modify the table column headers

From: Yair Altman

Date: 26 Mar, 2009 22:21:01

Message: 9 of 9

> How do I change the texts of the column headers?
> jtable.getColumnModel.getColumn(c).setHeaderValue('XYZ')
> does not appear to work.
>
> Thank you.
> Patrick.

Sorry for the belated response:
hTable.getTableModel.setColumnIdentifiers({'ABC','xyz','123'});
or:
jtable.getModel.setColumnIdentifiers({'ABC','xyz','123'});

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