"matt dash" wrote in message <jlcldu$fs3$
1...@newscl01ah.mathworks.com>...
--- snip ---
> >
> > So Matt, thank you very much so far.
> >
> > Alexander
>
> I'm not exactly clear on what parts are working/not working for you. Would you be able to post some example code that demonstrates the problems? You're right about needing to reset the renderer and column widths in (1). I keep the column widths stored in a matlab vector so they can be reused after the model is reset.
Dear Matt,
thanks again for the answer. I think the main point is your comment, that you do store column width and other stuff to rebuild the layout. Indeed, this is the solution (though strange you have to take care about this). For compleness, I attached an example at the bottom.
So for the moment, I am left with three questions:
1) Is it enough to reset ColumnWidth, Renderer (and Editor) and the ModelIndex to make the table look it should? Or are other things missing?
2) Is it right that Jtable does not offer an method to get the data for the whole table (didn't find it yet)?
3) when running my example program, the problematic layout only occures after the figure window is placed into background and foreground again. This is a thing I already discoverd at several places: The whole table layout is not updated until I bring the figure to bg and fg again. I tried several things and searched for a solution but havn't found anything. Is there a way to do it programmatically?
So, thank you very much for your help.
Alexander
---- Minimal example showing mentioned problems
function Table_Test
figure(2)
clf
%% Definitions
table_dim = 4;
Data = mat2cell(rand(table_dim,table_dim),ones(1,table_dim),ones(1,table_dim));
RowHeader_Data{1} = reshape(cellstr(char(65:(65+2*table_dim-1))'),table_dim,2);
RowHeader_Data{2} = {'a','b'};
cr = javax.swing.table.DefaultTableCellRenderer();
cr.setBackground(java.awt.Color(0.7,0.7,0.7))
needed_header_rows = [size(Data,1),1];
table_height = 200;
table_width = 500;
header_column_width = 50;
table_column_width = 50;
%% Main Table with Column Header
jtable = com.jidesoft.grid.JideTable(Data,repmat({' '},size(Data,2),1));
scrollpane = javax.swing.JScrollPane(jtable);
[jhandle,mhandle] = javacomponent(scrollpane,[10 10 table_width table_height],2);
jtable.setAutoCreateColumnsFromModel(0) % do not automatically update the table when a column is added. Otherwise, previous settings to columnwidth and header text are overridden
for n=1:jtable.getColumnCount
% width
jtable.getColumnModel.getColumn(n-1).setPreferredWidth(table_column_width)
jtable.getColumnModel.getColumn(n-1).setMinWidth(table_column_width)
jtable.getColumnModel.getColumn(n-1).setMaxWidth(table_column_width)
jtable.getColumnModel.getColumn(n-1).setCellRenderer(cr)
jtable.getColumnModel.getColumn(n-1).setHeaderValue(num2str(n))
jtable.getColumnModel.getColumn(n-1).setHeaderRenderer(cr)
end
jtable.getModel % display preinstalled model
%% Completely redo the Row Header
for n = 1:2 % Row header and upper right corner
header{n} = javax.swing.JTable(RowHeader_Data{n},{' ',' '});
header{n}.setPreferredScrollableViewportSize(java.awt.Dimension(2*header_column_width,table_width))
header{n}.setAutoCreateColumnsFromModel(0) % Warning: only to be used if the structure of the table will not change. This means, that columns need to bee added / and removed by hand (here it is ok!)
header{n}.setEnabled(0)
for m=1:2 % columns
% width
header{n}.getColumnModel().getColumn(m-1).setPreferredWidth(header_column_width)
header{n}.getColumnModel().getColumn(m-1).setMinWidth(header_column_width)
header{n}.getColumnModel().getColumn(m-1).setMaxWidth(header_column_width)
% cell renderer
header{n}.getColumnModel().getColumn(m-1).setCellRenderer(cr)
end
end
jhandle.setRowHeaderView(header{1}) % replace row header
jhandle.setCorner('UPPER_LEFT_CORNER',header{2}) % mimic an header for the row headers by filling upper right corner
% add some buttons for adding and removing
uicontrol('Parent',2,'Style','pushbutton','String','Add','Position',[200 250 40 20],'Callback',{@AddColumnFcn, jtable})
uicontrol('Parent',2,'Style','pushbutton','String','Rem','Position',[250 250 40 20],'Callback',{@RemoveColumnFcn, jtable})
%% Add a column
function AddColumnFcn(source, event, table_handle)
current_data = getData(table_handle); % get current data
current_data(:,end+1) = {''}; % append empty row to data
new_table_model = javax.swing.table.DefaultTableModel(current_data,repmat({''},size(current_data,2),1)); % redefine table model
table_handle.setModel(new_table_model) % set table model
% add column to table
table_handle.addColumn(javax.swing.table.TableColumn(table_handle.getColumnCount,table_column_width,cr,[]))
% and update settings
table_handle.getColumnModel.getColumn(table_handle.getColumnCount-1).setPreferredWidth(table_column_width)
table_handle.getColumnModel.getColumn(table_handle.getColumnCount-1).setMinWidth(table_column_width)
table_handle.getColumnModel.getColumn(table_handle.getColumnCount-1).setMaxWidth(table_column_width)
table_handle.getColumnModel.getColumn(table_handle.getColumnCount-1).setHeaderRenderer(cr)
table_handle.getColumnModel.getColumn(table_handle.getColumnCount-1).setHeaderValue(num2str(table_handle.getColumnCount))
end % Add columnFcn
%% Remove a column
function RemoveColumnFcn(source, event, table_handle)
current_data = getData(table_handle); % get data
col = table_handle.getColumnModel.getColumn(0); % delete first column
table_handle.removeColumn(col) % remove column
current_data = current_data(:,2:end); % restrict data
new_table_model = javax.swing.table.DefaultTableModel(current_data,repmat({''},size(current_data,2),1)); % define tablemodel
table_handle.setModel(new_table_model) % set table model
% for n = 1:table_handle.getColumnCount
% table_handle.getColumnModel.getColumn(n-1).setModelIndex(n-1)
% table_handle.getColumnModel.getColumn(n-1).setCellRenderer(cr)
% end
end % RemoveColumnFcn
%% Get data from Table
function table_data = getData(jtable_handle)
numrows = jtable_handle.getRowCount;
numcols = jtable_handle.getColumnCount;
table_data = cell(numrows, numcols);
for n = 1 : numrows
for m = 1 : numcols
[n,m]
temp_data = jtable_handle.getValueAt(n-1, m-1); % java indexing
if isempty(temp_data)
table_data{n,m} = '';
else
table_data{n,m} = temp_data;
end
end
end
end % function getData
end % function Table_Test