aspose file tools
The moose likes Swing / AWT / SWT and the fly likes Trying to make a JScrollbar transparent Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Trying to make a JScrollbar transparent" Watch "Trying to make a JScrollbar transparent" New topic
Author

Trying to make a JScrollbar transparent

Chris Kimball
Ranch Hand

Joined: Apr 23, 2012
Posts: 30


My JScrollPane is transparent by setting the Viewport.setOpaque(false). Works fine, but I'd like to have the JScrollBar (Vertical in this case) transparent too. scrollBar.setOpaque(false) doesn't do anything, nor does setting scrollBar foreground/background colors do anything.

In looking at JScrollBar source code, I don't see where the drawing takes place. Or is that actually in the JScrollPane?

Thanks.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4206
    
    3

Like for most Swing components, the painting is done by the UI delegate. Take a look at classes whose names end with ScrollBarUI in the javax.plaf.xxx packages.


luck, db
There are no new questions, but there may be new answers.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
another option is to set the scrollBar width to 0 (won't be transparent, but you won't see it)

the mouseWheel and arrow keys will still work, but you won't be able to click the up/down arrow buttons.
Chris Kimball
Ranch Hand

Joined: Apr 23, 2012
Posts: 30

I tried setting the scrollbar width to zero, but nothing happened. I got scrollBar = scrollPane.getVerticalScrollBar() then d = scrollBar.getSize(), scrollBar.setSize(new Dimension(0, d.height) ) before and after repainting.

BTW UIManager.put("JScrollBar.(anything)", (something)) doesn't do anything either.

I'm working on a Mac, so I'm seeing the Mac L&F. But I'd like the transparency to work on all systems. Does this mean I need to modify all the L&F's to make this work?

Obviously, I'm no GUI expert!

Thanks.

Chris Kimball
Ranch Hand

Joined: Apr 23, 2012
Posts: 30

Ah! I set the L&F to "CrossPlatform", then setOpaque(false) on the scrollBar. The scrollBar track is now transparent!

IDEALLY, I'd like the track borders and the up/down buttons to disappear. But Even with "CrossPlatform" L & F I haven't been able to use UI.Manager.put to change anything. Where should the
UIManager.put(.) be done?

Thanks!
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 949
    
    2

Try setPreferredSize instead of setSize


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this works fine for me on win 7

UIManager.put("ScrollBar.width", new Integer(0));

1) it has to be prior to creating a JScrollPane
2) will affect all scrollBars in the app
Chris Kimball
Ranch Hand

Joined: Apr 23, 2012
Posts: 30

Thanks to all.

I was able to set the width to zero with Michael Dunn's approach, but the result wasn't what I wanted.

Sticking to the CrossPlatform L&F and a simple setOpaque(false) comes very close to what was needed. There is an still unalterable white box around the transparent scroll bar. It would be nice to colorize the box to another color, but it appears not to be in the range of UIManager constants.

I've considered modfying the MetalScrollBarUI, however, that looks really complicated to do.

Thanks,

Chris
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Trying to make a JScrollbar transparent
 
Similar Threads
JScrollBar Not Moving ViewPort
Programmatically set the knob position of a JScrollBar
JScrollPane
JScrollBar in color ?
Adding ScrollBar to JTextArea