How to implement Auto-Hide Scrollbar in SWT Text Component
Asked Answered
A

4

8

I have a SWT Text component, for which I set SWT.MULTI, SWT.V_SCROLL and SWT.H_SCROLL to show the scrollbar when required. I found that even content is smaller than the text component then also scrollbar are visible in disable state.

Is there is any way through which I can auto hide the scrollbar? Like java Swing has JScrollPane.horizontal_scrollbar_as_needed

Ardennes answered 17/12, 2011 at 19:40 Comment(0)
N
7

That works on all cases:

Text text = new Text(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);

Listener scrollBarListener = new Listener () {
  @Override
  public void handleEvent(Event event) {
    Text t = (Text)event.widget;
    Rectangle r1 = t.getClientArea();
    Rectangle r2 = t.computeTrim(r1.x, r1.y, r1.width, r1.height);
    Point p = t.computeSize(SWT.DEFAULT,  SWT.DEFAULT,  true);
    t.getHorizontalBar().setVisible(r2.width <= p.x);
    t.getVerticalBar().setVisible(r2.height <= p.y);
    if (event.type == SWT.Modify) {
      t.getParent().layout(true);
      t.showSelection();
    }
  }
};
text.addListener(SWT.Resize, scrollBarListener);
text.addListener(SWT.Modify, scrollBarListener);
Nib answered 6/11, 2013 at 17:18 Comment(1)
excellent, thanks so much! This finally worked for me, it is the only way to get rid of horizontal sliders in RAP.Eindhoven
V
10

You can use StyledText instead of Text. StyledText has method setAlwaysShowScrollBars which can be set to false.

Vincevincelette answered 9/8, 2014 at 17:14 Comment(3)
Works, but sorrowly StyledText seems to not support setEnabled(boolean b) correct. See bugs.eclipse.org/bugs/show_bug.cgi?id=4745Carlinecarling
@Carlinecarling The bug has been fixed 2019 so your comment is no longer valid.Kimono
Great to hear the problem exists no longer - thank you @KimonoCarlinecarling
N
7

That works on all cases:

Text text = new Text(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);

Listener scrollBarListener = new Listener () {
  @Override
  public void handleEvent(Event event) {
    Text t = (Text)event.widget;
    Rectangle r1 = t.getClientArea();
    Rectangle r2 = t.computeTrim(r1.x, r1.y, r1.width, r1.height);
    Point p = t.computeSize(SWT.DEFAULT,  SWT.DEFAULT,  true);
    t.getHorizontalBar().setVisible(r2.width <= p.x);
    t.getVerticalBar().setVisible(r2.height <= p.y);
    if (event.type == SWT.Modify) {
      t.getParent().layout(true);
      t.showSelection();
    }
  }
};
text.addListener(SWT.Resize, scrollBarListener);
text.addListener(SWT.Modify, scrollBarListener);
Nib answered 6/11, 2013 at 17:18 Comment(1)
excellent, thanks so much! This finally worked for me, it is the only way to get rid of horizontal sliders in RAP.Eindhoven
T
6

@Plamen: great solution thanks. I had the same problem but for a multiline-text with style SWT.WRAP without a horizontal scrollbar.

I had to change a few things in order to make this work properly:

Text text = new Text(parent, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);

Listener scrollBarListener = new Listener (){
    @Override
    public void handleEvent(Event event) {
        Text t = (Text)event.widget;
        Rectangle r1 = t.getClientArea();
        // use r1.x as wHint instead of SWT.DEFAULT
        Rectangle r2 = t.computeTrim(r1.x, r1.y, r1.width, r1.height); 
        Point p = t.computeSize(r1.x,  SWT.DEFAULT,  true); 
        t.getVerticalBar().setVisible(r2.height <= p.y);
        if (event.type == SWT.Modify){
           t.getParent().layout(true);
        t.showSelection();
    }
}};
text.addListener(SWT.Resize, scrollBarListener);
text.addListener(SWT.Modify, scrollBarListener);
Tabasco answered 3/7, 2014 at 14:7 Comment(0)
P
1

According to this you can't hide vertical scroll bar, it's OS (Windows) specific L&F. You can get rid of horizontal bar by using SWT.WRAP without SWT.H_SCROLL.

Protrude answered 19/12, 2011 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.