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

조회 수: 6 (최근 30일)
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 CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by