How to add a sortable property to uitable within a classic figure.

9 ビュー (過去 30 日間)
Alain Barraud
Alain Barraud 2019 年 7 月 4 日
コメント済み: Alain Barraud 2019 年 9 月 2 日
I am running matlab 2018 and 2019. Once a uitable created and populated I have tried some customisation without any success
function JavaCustom(hTable)
jscroll=findjobj(hTable);
jtable=jscroll.getViewport.getView;
%change background color of a given column k to red
k=1;
cr=javax.swing.table.DefaultTableCellRenderer;
%set color
cr.setBackground(java.awt.Color(1,0,0));
jtable.getColumnModel.getColumn(k-1).setCellRenderer(cr);
jtable.repaint;%update
%sorting
%jtable.setSortable=true;==> error
jtable.setSortingEnabled(true);
% jtable.setAutoResort=true;==> error
% jtable.setMultiColumnSortable=true;==> error
% jtable.setPreserveSelectionAfterSorting=true;==> error
jtable.setSortOrderForeground(java.awt.Color.blue);
jtable.setShowSortOrderNumber(true);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Any idea?
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 7 月 4 日
This is not supported. You might be able to get it to work.
You should probably poke around http://undocumentedmatlab.com to see if Yair has written about this.

サインインしてコメントする。

採用された回答

Yair Altman
Yair Altman 2019 年 7 月 5 日
My Java-based uitable utility on the Matlab File Exchange provides a sortable wrapper for the legacy uitable. The sorting functionality is provided by the TableSorter Java class, which is included in the submission.
An alternative is to use coderazzi's TableFilter, which provides both filtering and sorting in a single class.
I discuss uitable sorting in detail on:
  3 件のコメント
Alain Barraud
Alain Barraud 2019 年 9 月 2 日
I have successfully obtained meaningfull customisation see code below. However I frequently obtained a Exception in thread "AWT-EventQueue-0" in particuler when executing jtable.setSortable(true); Using pause() (does not work with pause(t) where t is some duration) just before jtable.setSortable(true); avoid exception. The same result is obtain using a debug stop. Why?
I have not found how to access to row header in order to change its renderer parameters.
I have also noticed that com.jidesoft.grid.TableUtils.autoResizeAllColumns(jtable) does not take into account sorting arrow width which is hiden by default. How to avoid that, except computing a larger width a posteriori.
Yair ColoredFieldCellRenderer works fine for me.
My code to reproduce what I observe
function [hTable,jtable]=TestJava(m,n)
%yair cell renderer
javaaddpath('D:\Developpement\UiUtilities\ColoredFieldCellRenderer.zip');
if nargin<2, n=15;end
if nargin<1, m=10;end
f=figure(99);clf;
dpiscale=round(java.awt.Toolkit.getDefaultToolkit().getScreenResolution()/72);
%color
R=(0:n)/n;G=(n:-1:0)/n;B=R;
com.mathworks.mwswing.MJUtilities.initJIDE;
CHeaders=num2cell((1:n));
data=randi(100,m,n)-50;
colwidth=repmat({30},1,n);
hTable = uitable('Data',data,'ColumnName',CHeaders,...
'Units','norm', 'Position',[0,0,1,1],'parent',f,'ColumnWidth',colwidth);
jScroll = findjobj(hTable);
jtable = jScroll.getViewport.getView;
jtable = handle(jtable,'CallbackProperties');
CHR=jtable.getColumnModel.getColumn(0).getHeaderRenderer;
%la partie non utilisée de position
jtable.getParent.setBackground(java.awt.Color.red)
jScroll.RowHeader.setBackground(java.awt.Color.blue)
jScroll.ColumnHeader.setBackground(java.awt.Color.blue)%don't work
%Exception in thread "AWT-EventQueue-0": appears with jtable.setSortable(true);
% to avoid excpetion I must use pause or a stop debug mode ???? !!!!
%sort
pause
jtable.setSortable(true);
jtable.setAutoResort(true);
jtable.setMultiColumnSortable(true);
jtable.setShowSortOrderNumber(false);
%%%
jtable.setModel(javax.swing.table.DefaultTableModel(num2cell(data),CHeaders));
set(hTable,'ColumnFormat',[]);
CHR.setBackground(javax.swing.plaf.ColorUIResource(0,1,1))
font=CHR.getFont;
CHR.setFont(javax.swing.plaf.FontUIResource(font.get.Name,3,12*dpiscale))
for j=1:n
jtable.getColumnModel.getColumn(j-1).setHeaderRenderer(CHR);
renderer = javax.swing.table.DefaultTableCellRenderer;
renderer.setBackground(javax.swing.plaf.ColorUIResource(R(j),G(j),B(j)));
jtable.getColumnModel.getColumn(j-1).setCellRenderer(renderer);
end
jtable.repaint;
jtable.sortColumn(0,true,true)
jtable.unsort()
com.jidesoft.grid.TableUtils.autoResizeAllColumns(jtable);
%does not take into account sorting arrow width !!
hTable.ColumnWidth=cellfun(@mtimes,hTable.ColumnWidth,repmat({1.6},1,15),'UniformOutput', false);
cr = ColoredFieldCellRenderer(java.awt.Color.cyan);
% cr.setDisabled(true); % to bg-color the entire column ??utile??
cr.setCellBgColor(2,3,java.awt.Color.black)
cr.setCellFgColor(2,3,java.awt.Color.white)
jtable.getColumnModel.getColumn(3).setCellRenderer(cr);
jtable.getColumnModel.getColumn(14).setCellRenderer(cr);
jtable.repaint;
pause(.1)%to avoid Exception in thread "AWT-EventQueue-0": when called with 2 outputs arg ??!!
%Hide column header
%jtable.getTableHeader.getParent.setVisible(0)
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Alain Barraud
Alain Barraud 2019 年 9 月 2 日
I have found a solution for Exception in thread "AWT-EventQueue-0 Only adding javaObjectEDT(jtable) before jtable.setSortable(true);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by