File Exchange

image thumbnail

Java-based data table

version 1.3 (40.2 KB) by

Spreadsheet display/editing/sorting of data with multiple features

22 Ratings



View License

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

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

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

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

Comments and Ratings (54)


Brecht (view profile)

Very nice.
I have a question. I created a jtable in an uipanel. Now from a different callback I want to retrieve the java reference to the table.

When I create the table I get the as output. To retrieve this in a unrelated callback I found three ways:

1) [jcomp,~,~,~,~]=findjobj(handles.pPanel);
Which gives com.jidesoft.grid.GroupTable.

2) [jcomp,~,~,~]=findjobj('property',{'Name','pPanelTable'})
Which gives
This works as the name of treetable is specified as the panel name concat 'Table' in your function.

3) jcomp=get(handles.pPanel,'UserData')
This works as you specify the Userdata to be the java reference in your function.

All work, however which is the preferred method? + I don't know what is the difference between the references: and

as both work to manipulate the table's content.

Thank you for your nice contribution!

Kind regards,

andrea andrea

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

Ronald Smits


works better (without annoying warning ;) )

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!

Ronald Smits

In answer to SSOI:

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




Harold (view profile)

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?

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


Rachmawan (view profile)

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?


I have a exception error everytime if I close the GUI with a built-in uitable. Yair, any idea?
Also another question, how can I remove the row numbers in the first column??


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


David (view profile)

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.

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

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

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.


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.

Yair Altman

Yair Altman (view profile)

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()?


Dima (view profile)

The errors I described where with R2010a btw

Yair Altman

Yair Altman (view profile)

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.


Dima (view profile)

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


Aditya (view profile)

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?


Ho He

Ho He (view profile)

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

Yair Altman

Yair Altman (view profile)

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

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 ?

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


DNF (view profile)

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.

Yair Altman

Yair Altman (view profile)

@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):

Yair Altman


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?



Hans (view profile)

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.


Hey Yair,

Can you update this for the new uitable?



Patrick Tai

Add an additional input argument 'fontSz' to createTable:
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:

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.

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?

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.


Hans (view profile)

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

Sahin Aktas

That is extremely powerful. Thanks for the share.


DNF (view profile)

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


DNF (view profile)

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?


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.

Yair Altman

Yair Altman (view profile)

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



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.


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!

Patrick Tai

I am learning to use this tool, looks very good. Thank you Elmer.
However, I wish there are more tutorial or documentations.
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.


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.

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

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

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?


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.

Yair Altman

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

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

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

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.



fixes for HG2 and a couple of input params edge-cases


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

minor fixes

MATLAB Release
MATLAB 7.2 (R2006a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video