MATLAB Answers

Scrollable text box for GUI with selectable/editable text

44 views (last 30 days)
John
John on 23 Jan 2020
Commented: John on 28 Jan 2020
Hello
I am trying to create a scrollable GUI text box with selectable text. It seems that this can't be done using the standard Matlab uicontrols since text selection is disabled when scrolling is enabled. From other answers in this forum, I understand this has to be done in Java. I have had partial success using Yair Altman's excellent utilities "uicomponent" and "attachScrollPanelTo". By attaching a ScrollPanel to a JTextArea (demo code below), I nearly get what I need, but unfortunately I am only able to scroll through a limited range of the text. Is it possible to adjust the scroll limits so that the entire text can be seen? Any advice would be greatly appreciated!
Thanks!
John
____
%create a random string
s = 'a':'z';
textString = ['start ' s(randi(26,1,10000)) ' end'];
%create figure
hFig = figure(1);
hFig.Units='normalized';
hFig.OuterPosition=[0 0 1 1];
%create panel
hPan = uipanel('FontSize',12,...
'BackgroundColor','white',...
'Position',[0 0 1 1]);
pos = getpixelposition(hPan);
%create textarea
jTA = uicomponent('Parent',hPan,'style','javax.swing.jtextarea','tag','myObj',...
'Text',textString,'Position',pos,'Units','pixels',...
'BackgroundColor',[0.6 0.6 0.6],'Opaque',0,'LineWrap',1);
jTA.Font = java.awt.Font('Helvetica', java.awt.Font.PLAIN, 22); % font name, style, size
jTA.Foreground = java.awt.Color(1,1,1);
%attach scroll panel
hSP = attachScrollPanelTo(jTA);
%resize figure to reveal vertical scrollbar
hFig.OuterPosition=[0 0 1 0.5];

  0 Comments

Sign in to comment.

Accepted Answer

Yair Altman
Yair Altman on 24 Jan 2020
Your script generates a GUI that looks perfectly ok to me (fully scrollable & selectable) on my Win10, in both 18b and 20a.
You set the figure's OuterPosition to [0,0,1,.5], so the botttom part of the figure (with the horizontal scrollbar) is hidden beneath the Windows taskbar. But if you set the OuterPosition to be a bit higher (or if you simply drag the figure upward), you'll see the scrollbar.
In other words, I see nothing wrong.

  3 Comments

John
John on 24 Jan 2020
Dear Yair
Many thanks for your reply, and sorry if my original question was unclear. I have no problem accessing the scrollbars which, as you say, appear when the figure is resized. My problem is that vertical scrolling does not cover the full range of the text I am trying to display. (In the demo code, the first word of the text is 'start' and the final word is 'end'). When I slide the vertical scrollbar, I reach the upper limit of the scrollbar before 'start' is displayed, and I reach the lower limit of the scrollbar before 'end' is displayed, so I can only see a fraction of the full text. Is there a way to increase the vertical scrolling range? (Horizontal scrolling is not a problem in this particular case as I have linewrap enabled). For information, I am using Matlab 2018b on OS X.
Thanks and best wishes
John
Yair Altman
Yair Altman on 24 Jan 2020
The 'start' term is not visible because it is covered by the toolbar. If you set your figure's Toolbar property to 'none' or if you change your text-area's position to be beneath the toolbar, then you'll be able to see the 'start' term.
The 'end' term is not visible because of a limitation in JScrollPane/JTextArea that causes only ~3100 characters to be visible. You can search Java forums for a solution to this, it's not a Matlab problem.
John
John on 28 Jan 2020
Dear Yair
Many thanks for the helpful answer. By following your suggestion to remove the toolbar/menubar and reposition the JTextArea, and by also stretching the uipanel so it was large enough to fit the entire text, I was able to scroll over the full text. In case it's of use to anyone else, the code is given below.
Best wishes
John
%create a random string
s = ['a':'z' ' '];
textString = ['start ' s(randi(length(s),1,16200)) ' end'];
%create Fig1
hFig1 = figure('NumberTitle','off','Visible','Off');
hFig1.Units='normalized';
hFig1.OuterPosition=[0 0 1 1];
hFig1.ToolBar = 'none';
hFig1.MenuBar = 'none';
%create panel1
hPan1 = uipanel('FontSize',12,...
'BackgroundColor','white',...
'Position',[0 0 1 6]);
pos = getpixelposition(hPan1);
%create textarea1
jTA1 = uicomponent('Parent',hPan1,'style','javax.swing.jtextarea','tag','myObj','Units','pixels',...
'BackgroundColor',[0.6 0.6 0.6],'Opaque',0);
jTA1.Position = pos;
jTA1.Font = java.awt.Font('Helvetica', java.awt.Font.PLAIN, 22); % font name, style, size
jTA1.Foreground = java.awt.Color(1,1,1);
jTA1.Editable = 0;
jTA1.LineWrap = 1;
jTA1.WrapStyleWord = 1;
jTA1.Text = textString;
%adjust vertical position of jTextArea so the top of it aligns with top edge of figure frame
%you may need to adjust the value of -5145 to match your screen size
jTA1.Position = [pos(1) -5145 0.995*pos(3) pos(4)];
%attach and configure scroll panel
hSP1 = attachScrollPanelTo(jTA1);
hSP1.JavaPeer.getVerticalScrollBar.setPreferredSize(java.awt.Dimension(0,0));
%resize figure and make visible
hFig1.OuterPosition=[0 0.5375 1 0.4625];
hFig1.Visible = 'on';
hFig1

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by