Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
resize JSplitPane within uipanel?

Subject: resize JSplitPane within uipanel?

From: Lihua Pan

Date: 16 Jan, 2009 14:51:02

Message: 1 of 3

I use swing components within an uipanel, such as JSplitPane. Matlab provides the function "javacomponent" to put the java components in the container, like
========
posUipanel = get(hUipanel,'Position');
javacomponent(hMainPanel,[0,0,posUipanel(3),posUipanel(4)], hUipanel);

However, if I want to resize the uipanel, the java components within it do not resize accordingly. I've also defined the resize function. But it didn't work.
========
posUipanel = get(hUipanel, 'Position');
hMainPanel.resize(Dimension(posUipanel(3),posUipanel(4)));

Anyone know the reason?

Subject: resize JSplitPane within uipanel?

From: Yair Altman

Date: 13 Feb, 2009 14:58:01

Message: 2 of 3

"Lihua Pan" <plhalice@gmx.de> wrote in message <gkq6sm$ar1$1@fred.mathworks.com>...
> I use swing components within an uipanel, such as JSplitPane. Matlab provides the function "javacomponent" to put the java components in the container, like
> ========
> posUipanel = get(hUipanel,'Position');
> javacomponent(hMainPanel,[0,0,posUipanel(3),posUipanel(4)], hUipanel);
>
> However, if I want to resize the uipanel, the java components within it do not resize accordingly. I've also defined the resize function. But it didn't work.
> ========
> posUipanel = get(hUipanel, 'Position');
> hMainPanel.resize(Dimension(posUipanel(3),posUipanel(4)));
>
> Anyone know the reason?

You need to use normalized units for hUipanel and any other component that you wish to resize automatically - when you use fixed-size units the control will obviously not resize when its container (hMainPanel) resizes. You can find an example of a complex figure dialog with multiple resizable JSplitPanes in my FindJObj and UiInspect File-Exchange contributions.

Resizing automatically only the position and *not* the control's size (for example, re-centering a fixed-size button) is more tricky and requires trapping the control's 'ComponentResized' callback. Here's a related snippet from one of my projects, dealing with resizing a group of tabs each containing a scrollable viewport of a JTable:

%% Register table event handler
% adapted from %ML\toolbox\matlab\uitools\private\addlistener.m
function hL = addlistener(hContainer, hSrc, eventName, response)
  try
      hContainer = handle(hContainer);
      hSrc = hContainer.findprop(hSrc);
      hL = handle.listener(hContainer, hSrc, eventName, response);
      for i = 1:length(hContainer)
          hC = hContainer(i);
          p = findprop(hC, 'Listeners__');
          if (isempty(p))
              p = schema.prop(hC, 'Listeners__', 'handle vector');
              % Hide this property and make it non-serializable and non copy-able.
              set(p, 'AccessFlags.Serialize','off', 'AccessFlags.Copy','off', 'FactoryValue',[], 'Visible','off');
          end
          % filter out any non-handles
          hC.Listeners__ = hC.Listeners__(ishandle(hC.Listeners__));
          hC.Listeners__ = [hC.Listeners__; hL];
      end
  catch
      handleError;
  end

%% Handle table resize events
function handleResize(obj, evd) %#ok - obj is unused
  try
      % Modify the table & scroll-pane viewport sizes to the new panel's size
      try
          tp = evd.AffectedObject;

          % Fix problem when scrolling right/left and tabs overlap/de-overlap causing less/more available height
          % Note: drawnow or repaint() don't seem to work...
% t = timer('TimerFcn',{@handleResize2,obj,evd.AffectedObject}, 'StartDelay',.5);
% start(t);
          fprintf('\n'); % this works, oddly enough...
          pause(0.1);
      catch
          tp = evd;
      end
      ud = get(tp.userData,'userdata');
      %newPosTp = getpixelposition(tp); % a matlab position vector: [x,y,w,h] of the tab pane
      %newPosTb = ud.tables(1).getTable.getSize; % a java.awt.Dimension object
      %newPosVp = ud.tables(1).getTable.getVisibleRect; %a java.awt.Rectangle obj
      %newPosPn = ud.panels(1).getSize; % a java.awt.Dimension object
      %newSize = java.awt.Dimension(newPosTp(3)-5, newPosPn.height-5);
      newSizeTp = tp.userData.getSize;
      newWidth = newSizeTp.getWidth() - 5;
      newHeight = newSizeTp.getHeight() - 17*(tp.userData.getTabRunCount()+1);
      newSize = java.awt.Dimension(newWidth, newHeight);

      % Loop over all tabs and modify the displayed table & viewport
      matlabVersion = str2double(regexprep(version,'^(\d+\.\d+).*','$1'));
      for tabIdx = 1 : length(ud.tables)
          try
              ud.tables(tabIdx).getTable.setPreferredScrollableViewportSize(newSize);
          catch
              ud.tables(tabIdx).setPreferredScrollableViewportSize(newSize);
          end
          ud.tables(tabIdx).getTable.revalidate;
          ud.scroll(tabIdx).setPreferredSize(newSize);
          if matlabVersion >= 7.2
              ud.scroll(tabIdx).setSize(newSize);
          end
      end

      % Modify the table column widths
      updateTablesWidth(ud.tables,tp);
      drawnow;
  catch
      % never mind...
  end
  return;


%% --- Update column widths based on table width
function updateTablesWidth(et,hndl)
  try
      % Update column widths based on overall available table width
      tabPanel = get(hndl,'userData');
      availableWidth = tabPanel.getSize.getWidth;
      for tabIdx = 1 : length(et)
          % Update column widths based on actual table data, not table width
          data = et(tabIdx).Data;
          colWidth = cellfun('length',cell(et(tabIdx).getColumnNames));
          for colIdx = 3 : et(tabIdx).getNumColumns
              for rowIdx = 1 : length(data)
                  colWidth(colIdx) = min(20, max(colWidth(colIdx), length(num2str(data(rowIdx,colIdx)))));
              end
          end
          colWidth = fix((availableWidth-62)*colWidth/sum(colWidth));
          columns = et(tabIdx).getTable.getColumnModel;
          %et(tabIdx).setColumnWidth(mean(colWidth));
          pause(0.05);
          for colIdx = 1 : et(tabIdx).getNumColumns-1
              columns.getColumn(colIdx).setPreferredWidth(colWidth(colIdx+1));
          end

          % Set the max width for columns 0-1 (just in case they were not set before)
          columns.getColumn(0).setMaxWidth(10);
          columns.getColumn(1).setMaxWidth(25);
      end
  catch
      % Never mind...
  end

% Main function
function main(...)
      [...]
      % Register the resize event handler
      addlistener(hndl, 'PixelBounds', 'PropertyPostSet', @handleResize);
end

Yair Altman

Subject: resize JSplitPane within uipanel?

From: Yair Altman

Date: 23 Feb, 2009 18:02:47

Message: 3 of 3

"Lihua Pan" <plhalice@gmx.de> wrote in message <gkq6sm$ar1$1@fred.mathworks.com>...
> I use swing components within an uipanel, such as JSplitPane. Matlab provides the function "javacomponent" to put the java components in the container, like
> ========
> posUipanel = get(hUipanel,'Position');
> javacomponent(hMainPanel,[0,0,posUipanel(3),posUipanel(4)], hUipanel);
>
> However, if I want to resize the uipanel, the java components within it do not resize accordingly. I've also defined the resize function. But it didn't work.
> ========
> posUipanel = get(hUipanel, 'Position');
> hMainPanel.resize(Dimension(posUipanel(3),posUipanel(4)));
>
> Anyone know the reason?

Continuing my earlier response, I've just uploaded a new contribution to the File Exchange called UISplitPane (http://www.mathworks.com/matlabcentral/fileexchange/23073), which mimics JSplitPane behavior with full Matlab support. This means that you can add axes and controls to the split-panes (2 uipanels), and they resize automatically when the split-pane divider is moved interactively or programatically. Feedback is most welcome.

Yair Altman

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us