4.45455

4.5 | 13 ratings Rate this file 175 downloads (last 30 days) File Size: 38.09 KB File ID: #14225

Java-based data table

by Yair Altman

 

10 Mar 2007 (Updated 23 Mar 2007)

No BSD License  

Spreadsheet display/editing/sorting of data with multiple features

Download Now | 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 to 7.4; 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, ...)

Acknowledgements
This submission has inspired the following:
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!)
Zip File Content  
Other Files TableSorter.jar,
table.png,
createTable.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (24)
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.

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...

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).

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.

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.

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

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

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...

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.

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.

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!

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.

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

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.

16 Mar 2009 Øyvind

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?

20 Mar 2009 Øyvind

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

27 Mar 2009 Sahin Aktas

That is extremely powerful. Thanks for the share.

03 Apr 2009 Hans

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

09 Apr 2009 Patrick

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.

20 Apr 2009 Patrick

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.

20 Apr 2009 Patrick

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.

25 May 2009 laurent  
20 Aug 2009 Jeffrey

Hey Yair,

Can you update this for the new uitable?

Thanks!

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.

Please login to add a comment or rating.
Updates
13 Mar 2007

minor fixes

23 Mar 2007

fixed several issues; improved help comment

Tag Activity for this File
Tag Applied By Date/Time
gui tools Yair Altman 22 Oct 2008 09:03:42
example Yair Altman 22 Oct 2008 09:03:42
table Yair Altman 22 Oct 2008 09:03:42
uitable Yair Altman 22 Oct 2008 09:03:42
spreadsheet Yair Altman 22 Oct 2008 09:03:42
excel Yair Altman 22 Oct 2008 09:03:42
java Yair Altman 22 Oct 2008 09:03:42
java Ivan 28 May 2009 05:56:08
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com