我正在更新我的通用数据操作GUI,我想我会使用uitable
而不是uicontrol
列表框来显示路径和文件名。加载的文件。我注意到列标题默认居中,如果文件名长于表的宽度,我似乎无法弄清楚如何左对齐它们。我检查了get
返回的属性并使用findjobj
进行了搜索,但没有找到任何明显的内容。我尝试使用html字符串也不成功。
我错过了一些明显的东西吗?
示例代码:
function testcode
res = get(0,'ScreenSize');
figdim = [1280 720]; % Main figure size, pixels
handles.mainfig = figure( ...
'Units', 'Pixels', ...
'Position', [(res(3) - figdim(1))/2 (res(4) - figdim(2))/2 figdim(1) figdim(2)], ...
'Name', 'Meep', ...
'Resize', 'off', ...
'DockControls', 'off', ...
'NumberTitle', 'off', ...
'Toolbar', 'figure' ...
);
handles.filetable = uitable( ...
'Parent', handles.mainfig, ...
'Units', 'normalized', ...
'Position', [0.038 0.71 0.235 0.1], ...
'ColumnName', 'File Loaded', ...
'Data', {'None'} ...
);
handles.loadfilebtn = uicontrol( ...
'Parent', handles.mainfig, ...
'Style', 'pushbutton', ...
'Units', 'normalized', ...
'Position', [0.075 0.85 0.16 0.07], ...
'String', 'This is a Button, Click', ...
'Callback', {@abutton} ...
);
guidata(handles.mainfig, handles);
end
function abutton(hObject,~)
handles = guidata(hObject);
filepath = 'C:\folder\folder\folder\folder\folder\folder\folder\folder\folder\superduperreallylongfilename.fileextension';
set(handles.filetable,'Data',{filepath});
set(handles.filetable,'ColumnWidth',{length(filepath)*5});
test = '<html><left />File Loaded</html>';
set(handles.filetable,'ColumnName',test); % Attempt #1, doesn't work
end
答案 0 :(得分:2)
我能想到的最简单的解决方法是将表和表头分成两个单独的uitable。正确设置位置矢量有点费劲,但通常它就像一个魅力。重要提示:放置&#34; header-uitable&#34;下面(在代码中)&#34;数据表&#34;。你这样可以省去一些麻烦。
function testcode
close all
res = get(0,'ScreenSize');
figdim = [1280 720]; % Main figure size, pixels
handles.mainfig = figure( ...
'Units', 'Pixels', ...
'Position', [(res(3) - figdim(1))/2 (res(4) - figdim(2))/2 figdim(1) figdim(2)], ...
'Name', 'Meep', ...
'Resize', 'off', ...
'DockControls', 'off', ...
'NumberTitle', 'off', ...
'Toolbar', 'figure' ...
);
handles.tableheader = uitable( ...
'Parent', handles.mainfig, ...
'Units', 'normalized', ...
'Position', [0.038 0.71 0.235 0.1], ...
'ColumnName', 'File Loaded', ...
'ColumnWidth', {271} ...
);
handles.filetable = uitable( ...
'Parent', handles.mainfig, ...
'Units', 'normalized', ...
'Position', [0.038 0.682 0.235 0.1], ...
'ColumnName', {}, ...
'Data', {'None'} ...
);
handles.loadfilebtn = uicontrol( ...
'Parent', handles.mainfig, ...
'Style', 'pushbutton', ...
'Units', 'normalized', ...
'Position', [0.075 0.85 0.16 0.07], ...
'String', 'This is a Button, Click', ...
'Callback', {@abutton} ...
);
guidata(handles.mainfig, handles);
end
function abutton(hObject,~)
handles = guidata(hObject);
filepath = 'C:\folder\folder\folder\folder\folder\folder\folder\folder\folder\superduperreallylongfilename.fileextension';
set(handles.filetable,'Data',{filepath});
set(handles.filetable,'ColumnWidth',{length(filepath)*5});
end
答案 1 :(得分:2)
您已经提到findjobj
,它允许您获取表格后面的JTable
对象。您实际要做的是更改header或JTableHeader
对象的某些属性。一旦你知道你在寻找什么,就很容易找到解决方案(例如previous thread)。
总之,将此代码放在按钮回调的末尾:
function abutton(hObject,~)
%// ....
hTable = handles.filetable;
jScrollpane = findjobj(hTable);
jTable = jScrollpane.getComponent(0).getComponent(0);
%// You can see the list of components using findjobj(hTable).list
%// Part1:
dtcr = javaObject('javax.swing.table.DefaultTableCellRenderer');
dtcr.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
jTable.getColumn(jTable.getColumnName(0)).setHeaderRenderer(dtcr);
%// Part2:
jRenderer = jTable.getTableHeader().getDefaultRenderer();
jRenderer.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
%// Finalization:
jTable.invalidate();
end
单击该按钮,结果为:
几点说明:
Parts
应该是两种不同的解决方案,但我注意到它只有在两者存在的情况下才有效(我没有解释)。