4.625

4.6 | 20 ratings Rate this file 34 Downloads (last 30 days) File Size: 41.1 KB File ID: #14225
image thumbnail

Java-based data table

by

 

10 Mar 2007 (Updated )

Spreadsheet display/editing/sorting of data with multiple features

| Watch this File

File Information
Description

createTable creates a 2D java-based table that enables display, editing and sorting of 1D and 2D data using familiar look-and-feel. Columns can be resized and sorted; data can be selected, printed, copied, pasted etc., including to/from Excel; Rows can be added/deleted; Columns can have different alignments (right/center/left) and editors (checkbox/drop-down/...); Callback functions can be specified for multiple events (Data change, structure change, mouse click, ...) and multiple other aspects can be set programatically (see extensive help within the m file).

Works on Matlab 7.1 onward; not sure about earlier versions.

Installation: unzip attached file; place the jar file in classpath.txt or use javaaddpath; then run the m file.

Syntax: [mtable, buttons] = createTable (pnContainer, headers, data, buttonsFlag, 'PropName',PropValue, ...)

See detailed help within the file (run: help createTable)

Acknowledgements

This file inspired Tree Table Create A Sortable Table Control With Collapsible Row Groups and Data Logging Gui Manager.

MATLAB release MATLAB 7.2 (R2006a)
Other requirements Should work on Matlab 7+, but might work on earlier versions too. Java should be enabled (duh!)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (52)
29 Jun 2014 andrea andrea

Very nice script
I wonder how to sort table and if its possible to Apply Excel like row filter

12 Nov 2013 Ronald Smits

mTable.getTableScrollPane.getRowHeader.setVisible(0)

works better (without annoying warning ;) )

12 Nov 2013 Ronald Smits

I can recommend the book to anyone, it provides really clear documentation and 'nearly' covers all problems you can encounter when seeking for an intermediate between matlab and java!

12 Nov 2013 Ronald Smits

In answer to SSOI:

set(mTable.getTableScrollPane.getRowHeader,'Visible','off') works for me!

30 Jul 2013 SHADY SALAMA

.

01 Feb 2013 Harold

Suppose I have a plot that I add points onto using mouse clicks. I put createTable.m in the mouseclick function so that every time a point gets added on the plot, the table adds another row with those (x,y) coordinate values in the column. I've got this working good. What I want to do now is to delete a point from the plot if a row(coordinate pair) gets deleted from the table. To do this I just need to pass the updated data out of createTable.m. Questions is what is the variable that holds updated table data after a row gets deleted or all rows get deleted for that matter?

03 Aug 2012 Ivar Eskerud Smith

Is it possible to register a callback to detect clicks/when the user selects a row?

14 Feb 2012 Rachmawan

Mmm...excuse me, I have no experience in adding some libraries, jar, or whatever in Matlab. Would anybody teach me how to use this uitable?

13 Jan 2012 SSOI SS

I have a exception error everytime if I close the GUI with a built-in uitable. Yair, any idea?
http://www.mathworks.com/matlabcentral/answers/25912-exception-in-thread-awt-eventqueue-0-java-lang-nullpointerexception-using-createtable-java
Also another question, how can I remove the row numbers in the first column??

13 Jan 2012 SSOI SS

Also another question, how can I disable the row numbers in the first column??

07 Nov 2011 David

Hi Yair, thanks for all the nifty work on the sortable uitable. Quick question: the standard uitable cell edit callback takes three agruments, the final one being the “handles” argument. We use that in conjunction with the “guidata” function to create some side-effects from the cell edits that affect other figures that are open along with the one containing the uitable. Any ideas for how to replicate that functionality? Thanks.

07 Jun 2011 Anton Kotelyanskii

Also, if I run the script in debug mode, with a breakpoint when the table is created, it does show up.

07 Jun 2011 Anton Kotelyanskii

Figured it out. It's the default context menu from one of the old versions.

07 Jun 2011 Anton Kotelyanskii

I'm trying to modify the table. How did you implement the pop-up menu? I want to add some items to it, but I can't find it.

Thanks,
Anton

06 Apr 2011 Loren Majersik

I'm got the same error (not warning) as Dima on 2010a.
The error only occurred when trying to use setValueAt with enumerated classes.
I cast the enumerated values to ints to fix the problem.

21 Feb 2011 Yair Altman

Dima - the warning you see is because Matlab started using a different type of uitable in R2008a which is different than the one used in this utility. This warning is harmless.

You need to be more specific about the error otherwise I cannot help you because I do not see any error.

What exactly did you try to do with setValueAt()?

21 Feb 2011 Dima

The errors I described where with R2010a btw

21 Feb 2011 Yair Altman

Dima - why do you say this? this submission works splendidly all the way up to R2011a (Matlab 7.12).

You must have entered incorrect input parameters or something similar.

21 Feb 2011 Dima

I get the following warning when using createTable():

Warning: It appears you are using an obsolete version of uitable.

also if i try to use setValueAt method I get this error:

??? No method 'setValueAt' with matching signature found for class
'com.mathworks.hg.peer.UitablePeer$PeerSpreadsheetTable'.

09 Jul 2010 Aditya

Hi Yair,
Nice tool here. I have a requirement and I could not find a comment or question above that can cover this question. Can I select a row and then extract it to my workspace. I am planning to use this code a selection table so the user can select one row from the table which will be returned back for further processing? How can I do that?

Thanks,
Aditya.

25 May 2010 Ho He

I recently found this java-table tool and I already start to like it very much.
Beeing an absolute beginner with java i didn't figure out how to change the color of a specific ROW in the java-table created - I only found exemplary code to change the color of a COLUMN.

Could you provide the code for me?

Thanks in advance

08 Apr 2010 Yair Altman

@Amitabh - you probably did not install the TableSorter.jar properly, as explained in the main help section

08 Apr 2010 Amitabh Verma

Seems like a nice piece of code but I am unable to get the sort function to work. Any help/pointers on this ?

26 Mar 2010 Robert Barrie

If you copy and paste the first line in the example (Programming tips/cues/examples:), make sure you add the 'e' to creatE:
mtable = creatTable(...)

Also - why is this tagged as excel when there doesnt seem to be any example or obvious way to link the table to excel? (Or am I missing something?)

10 Mar 2010 Ivar Eskerud Smith  
02 Feb 2010 DNF

I am liking this table more and more as my dislike for the built-in uitable increases.

One thing I have not been able to figure out: When I manually edit a table with numerical entries they are converted to strings. Aparently Java allows some sort of type checking that converts the strings back to numbers, but I cannot figure out how to accomplish this in Matlab. The only idea I have is setting a FocusLostCallback that does a str2num on the cell, but, firstly,
FocusLostCallback doesn't seem to be triggered when you navigate by keyboard, and secondly I do not seem to be able to set a cell value to a vector (e.g. [3,4]) which is what I really want.

15 Jan 2010 Yair Altman

@Benoit - I'm not sure what you mean by "The description mentions callback that can be added on mouse click" - please quote the exact comment in the code and/or specify what exactly you wish to do.

A related reference that can help you implement callbacks on the Java table (and any other Java object): http://undocumentedmatlab.com/blog/uicontrol-callbacks/

Yair Altman
http://UndocumentedMatlab.com

12 Jan 2010 Benoit Mazeaud

Hello!

Very useful & interesting work!
The description mentions callback that can be added on mouse click but could anyone give me some tips to implement it please?

Benoit

30 Sep 2009 Hans

Hello Yair, hello Jeffrey,

I still appreciate the createTable() function, but honestly it took me some time to make it work with the new uitable() call using the r2009a release. My problem was NOT inserting 'vo' to modify the line ..
mtable = uitable(hFig, ..
to this ..
mtable = uitable('v0',hFig, ..
but my figure just did not have any jtable.

This got immediately solved when I set the visibility of the figure to 'on' before using the createTable() call. It seems like some parent/child objects needed by createTable() cannot be found when the figure is invisible. I never had this problem with the old r2007a release.

Anyway, it now works for me (hopefully for others to) and the modifications to Yair's createTable.m-code were less than tiny.

Thanks again Yair for your work.

20 Aug 2009 Jeffrey

Hey Yair,

Can you update this for the new uitable?

Thanks!

25 May 2009 laurent  
20 Apr 2009 Patrick Tai

Add an additional input argument 'fontSz' to createTable:
createTable(pnContainer,headers,data,buttonsFlag,fontSz,varargin)
In the code, after the statement jtable=mtable.getTable;
add the following:

If I may, let me answer my own question on changing font size of createTable cell contents:
One way to do it is to call createTable with additional argument fontSz:
createTable(pnContainer,headers,data,buttonsFlag,fontSz,varargin)

Add the following lines to the code createTable.m

jtable = mtable.getTable; % current line 178
fnt = get(jtable, 'font');
fnt1 = ff.deriveFont(fontSz); % fontSz from input
set(jtable, 'font', 'ff1);

You call createTable with fontSz = 14 (or whatever, default = 11)
That appears to work!
I hope that is useful.

20 Apr 2009 Patrick Tai

createTable fails to work in MATLAB 2008b.
Calls to uitable come back with 'obsolete' version warning.
I changed createTable to call uitable with first argument 'v0':

mtable = uitable('v0',hFig, 'position',tablePosition, 'Data',data, 'ColumnNames',headers);

The warnings disappear, however, the tables do not appear inside the uipanel 'pnContainer' that createTable is called with.

However, if the call is done again, the tables appear!

I am still trying to figure it out. Can someone help?
Thanks.
Patrick.

09 Apr 2009 Patrick Tai

Wonderful program.
I have been using this for long time.
One question:
How do I change the font size/weight of the entries in the table cells?
Thank you.

03 Apr 2009 Hans

very useful piece of code. saved lots of my time.

27 Mar 2009 Sahin Aktas

That is extremely powerful. Thanks for the share.

20 Mar 2009 DNF

Very nice code. A couple of things:

As I mentioned above, it doesn't work easily with the new uitable, and I guess it isn't trivial to fix this. Possibly uitable should be called with the 'v0'-switch to avoid getting a warning each time.

I just found out the hard way that if the first element in my data cell array is empty, I get the error
"Error: Data must be a MATLAB cell array"
This seems to be a bug in uitable.m, not createTable
The fix I'm using is
if isempty(data{1}), data{1} = ' '; end % one whitespace

16 Mar 2009 DNF

I guess there is a new version of uitable out (at least in R2008b), which causes a warning to be raised when using createTable:

"Warning: It appears you are using an obsolete version of uitable."

Is there any chance this function will be updated to take care of this?

09 Mar 2009 danders

You're a genius! Thanks Yair! I have a background in VB and am somewhat new to Matlab, so I'm a little slow to pick-up on the intricacies of Java syntax. I can't wait to plug your solution into my application. Rest assured that you will be kindly acknowledged in any of our publications.

03 Mar 2009 Yair Altman

danders - this is due to the fact that tablehandle.getData returns a 2D Java object, which is actually represented as a 1D array of arrays, therefore always returning "1". in order to get the correct size, simply convert this Java array into Matlab using the cell() function, as follows:

[numRows,numCols] = size(cell(tablehandle.getData));

Yair

02 Mar 2009 danders

I forgot to mention that I am extracting the data with:
data = tablehandle.getData from the parent figure. This is also where I'm attempting to perform the size.

02 Mar 2009 danders

Hello Yair,
Your code is absolutely fantastic! I have been able to accomplish dynamically embedding your jtable within a panel within a figure. However, after the user is done entering data, I am attempting to extract the size of the rows and columns with a simple [RowCount,ColCount] = size(data)
For some reason, the Column count always returns "1", whereas the row count is accurate. Do you have any idea why this might be happening? I need the dimensions of the array to loop through the values and perform calculations. Thanks in advance!

27 Feb 2009 Patrick Tai

I am learning to use this tool, looks very good. Thank you Elmer.
However, I wish there are more tutorial or documentations.
Questions:
1. Is there anyway to make different sizes for different tab pages?
2. Can the UIs for the tab pages to held in different .m files? Right now, one big UI.m file holds specs for all the tab pages, resulting in a very big file.
3. How to control the font of the texts in the tabs?
Thank you very much.

30 Oct 2008 Anthony Beauchamp

I really some help with table. I really like on what you done. It is a very useful function. The problem I am having is how to add an event/callback whenver the user change from one to another? In my application, each row is a file information from the database and it also has a image attahched to it. Therefore when ever the row selection changes the image plot changes automatically. In order to do this i need some form of callback when user change the row selection similiar to DataChange callback. Thank you.

06 Sep 2008 Moran B

Great code !
After fighting with the new uitable for 3 months, this code was able to solve all my problems.

The examples supplied are really useful (especially for a Java newbie like myself). Adding more examples would be great...

02 Apr 2008 Denis Davydov

3) I seems that visible method doesn't work. I cant hide the table. Can someone check this out...
Or, maybe, you have to delete table when creating new one connected with the same holder. It seems that the original table still exists and actually covered or is covered by another one....

Thank you in advance.

Best regards,
Denis

29 Feb 2008 Denis Davydov

Grate stuff! thank you very much!!!
I got only two questions:
1) how can I select Row in table using some public method?
I want to change selected row according to user interaction to another object (listbox or maybe another one object of this class).
2) how do I write callback to selection of raw\column?

i'm sorry, i'm not very experienced in Java...

Thank you in advance.
Best regards,
Denis

15 Feb 2008 Hoi Wong

Excellent program! However, for people who don't know java much, more documentation would be appreciated. For example, is there a way to control the column alignment?

Thanks.

09 Nov 2007 Bruce Elliott

This is a very nice function for working with uitables. Expertise with Java helps a lot, since it uses much of the properties of the underlying jtable object. Even without knowledge of Java, however, it's still a big improvement on the native uitable functionality.

11 Aug 2007 Yair Altman

jtable = mtable.getTable;
modelRow = jtable.getModel.modelIndex(jtable.getSelectedRow);

Note: jtable.getModel.viewIndex() returns the reverse mapping (model-to-view).

10 Aug 2007 Fred Koehler

Great function!
Dumb question -once sorted, how do you determine the selected row(s) in the model? jtable.getSelectedRows is relative to the current view...

29 Jun 2007 Yogesh PARTE

A good wrapper code for inbuilt uitable that allows far more options than native uitable code. Simple and nice illustrations in the help section shows possible usage of Java. The versatility of the code is just limited by users knowledge of Java. Recommended even for those with very little or no background in Java.

Updates
13 Mar 2007

minor fixes

27 Jun 2013

Multiple fixes; support for image cells, column reordering/resizing, uicontextmenu; full P-V parameters processing

Contact us