Here is a simple way to use java tables in matlab. I've wrapped the standard java table model with a matlab data model. Nested functions are taken advantage of to provide data set and get methods, cell rendering and entry validation.
% XTARGETS_UITABLE
%
% t = xtargets_uitable(table, rows, cols, getfun, setfun, labelfun, editfun)
%
% Arguments
% table Any instance or subclass instance of javax.swing.JTable
% rows numbers of rows
% cols number of cols
% getfun function handle for aquiring data to the table.
% val = getfun(row, col)
% setfun function handle for setting data to the table.
% setfun(row, col, val)
% labelfun function handle for generating a label in the table
% string = labelfun(row, col, val)
% editfun function handle for validating string input
% value = editfun(row, col, string)
% throw an error if the string is invalid
% to show a dialog box and reset the old value
% of the cell
% table an instance of javax.swing.JTable or a subclass of
%
% Example
% See xtargets_uitable_test
%
% Copyright Brad Phelan (C) 2005 under the LGPL license
% http://xtargets.com
function t = xtargets_uitable(table, rows, cols, getfun, setfun, labelfun, editfun)
import javax.swing.*;
import javax.swing.table.*;
import ca.odell.renderpack.*;
tableModel = javax.swing.table.DefaultTableModel(rows,cols);
% Initialize the table model
for r = 1:rows
for c = 1:cols
tableModel.setValueAt(labelfun(r,c,getfun(r,c)),r-1,c-1);
end
end
table.setModel(tableModel);
scroller = JScrollPane(table);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
[hcom, hg] = javacomponent(scroller);
hg = handle(hg);
p = schema.prop(hg,'table','mxArray');
hg.table = handle(table);
set(hg,'position',[0 0 200 200]);
%Fix for JTable focus bug : see http://www.mycgiserver.com/~Kleopatra/swing/table/merlineditfun.html
table.putClientProperty('terminateEditOnFocusLost', java.lang.Boolean.TRUE);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
t = hg;
% A flag to avoid change handler recursion
syncchange = false;
set(tableModel, 'TableChangedCallback', @change_fcn);
function change_fcn(src, ev)
if syncchange
% This change was invoked from this handler
syncchange = false;
return
end
syncchange = true;
row = ev.getFirstRow;
col = ev.getColumn;
string = tableModel.getValueAt(row,col);
row = row + 1;
col = col + 1;
% Perform validation on the string and convert it to a value
try
val = editfun(row, col, string);
setfun(row,col,val);
tableModel.setValueAt(labelfun(row,col,val), row-1,col-1);
catch
errordlg(lasterr,'Invalid Edit');
% The value was not valid so reset the cell
tableModel.setValueAt(labelfun(row,col,getfun(row,col)), row-1,col-1);
end
end
end
A test function demonstrates the behaviour of the table with a simple data model and validation.
% xtargets_uitable_test
%
% A simple example of using the table model to modify data in
% a nested function workspace. The data is in double format
% so we use the '%g' sscanf pattern to render and read the
% data from the table cells.
% Copyright Brad Phelan (C) 2005 under the LGPL license
% http://xtargets.com
function xtargets_uitable_test
close all;
data = rand(5);
table = javax.swing.JTable;
% Create a uitable with a simple matlab data model. Specify
% that the conversion class will be %g.
t = xtargets_uitable(table, 5,5, @getfun, @setfun, @labelfun, @editfun);
function val = getfun( row, col)
val = data(row,col);
end
function setfun( row, col, val)
data(row,col) = val;
end
function val = editfun(row, col, string)
val = sscanf(string, '%g');
if isempty(val)
error([ '''' string ''' is not a valid entry' ]);
end
end
function label = labelfun(row, col, val)
label = sprintf('%g',val);
end
% uicontrol to display the data model on the command line
uicontrol('string','push me','position',[300 0 100 50], ...
'callback',@disp_data);
function disp_data(src,ev)
disp(data);
end
end