Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to make a JScrollbar transparent  RSS feed

 
Chris Kimball
Ranch Hand
Posts: 35
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try setPreferredSize instead of setSize
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!