It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes Make the ScrollBar of a JScrollPane an image instead of a rectangle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Make the ScrollBar of a JScrollPane an image instead of a rectangle" Watch "Make the ScrollBar of a JScrollPane an image instead of a rectangle" New topic
Author

Make the ScrollBar of a JScrollPane an image instead of a rectangle

Paul Adcock
Ranch Hand

Joined: Jan 22, 2011
Posts: 48

I was trying to get the JScrollBar of a JScrollPane to have an image as the thumb, I think the thumb is the part that moves on the scroll bar, right, if not, that's what part I was meaning to refer to. I tried extending BasicScrollPaneUI like I found on a site, though theirs had changed it to an oval, and I wanted an image so I couldn't quite use theirs exactly. I tried adding these methods, and also, I had a JScrollBar subclass. That subclass had the BasicScrollPaneUI as a subclass of it.

I had been referring, as I couldn't think of another way, to the JScrollBar subclass by using JFancyScrollBar.this. Was that a bad move? I couldn't figure out how to refer to the actual JFancyScrollBar(the JScrollBar subclass) object itself inside the inner class when I think I might need it, unless I can use the JComponent param to a method which I'm going to post to show what I'm trying to do, instead. I've tried both. It's not moving. Also, I'd like to resize it so that it's not huge, i.e. 200 by 200 or whatever, even if it could work or move.


Now, there are some other JFancyScrollBar constructors besides the one I've shown. Will it know, I'm hoping it will, if I referred to JFancyScrollBar.this, which one of those I meant?


I also tried using the JComponent inside of that paintThumb method, in case that might be what was not working. No more luck trying that either.
I'm very confused on quite what to do. I know I'm heading in more or less the right direction, but I can't figure it out right now.

What I had done was tried using JFancyScrollBar.this instead of the JComponent. That didn't quite work so I tried using the JComponent instead. That still didn't work.

What it seems to be doing is painting the JScrollBar still instead of painting, or changing, the thumb thing to my image.

Also, the fact that I've set, in my much much larger program, I've set NimbusUI as the default overall, could that be affecting this one in any way?



----Edit----
I got it to work, though I wish I could now make the JScrollBar bigger so that my image wouldn't be so small.




Repeal Obamacare.
http://www.dontfundobamacare.com/
http://liberty-amendments.freeforums.net/
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I wish I could now make the JScrollBar bigger

if, by bigger, you mean wider, put this line before creating the scrollpane

UIManager.put("ScrollBar.width", new Integer(25));//where 25 is anything you want

but it will affect all scrollbars
Paul Adcock
Ranch Hand

Joined: Jan 22, 2011
Posts: 48

Michael Dunn wrote:> I wish I could now make the JScrollBar bigger

if, by bigger, you mean wider, put this line before creating the scrollpane

UIManager.put("ScrollBar.width", new Integer(25));//where 25 is anything you want

but it will affect all scrollbars


1.) Actually I meant height.

2.) How do I get it so that it does that for just that scroll bar? (Note, setSize() appears to not really affect anything, possibly due to the layout of the page maybe. Anyway, I can't seem to make a huge component say in a GridLayout. Or to make a huge component small in a gridLayout instead of making all the other parts bigger.



Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 1.) Actually I meant height.

just make the scrollPane taller?
if that's not an option, perhaps having the scrollbar elsewhere (and taller) might suit

> 2.) How do I get it so that it does that for just that scroll bar?
Paul Adcock
Ranch Hand

Joined: Jan 22, 2011
Posts: 48

Michael Dunn wrote:> 1.) Actually I meant height.

just make the scrollPane taller?
if that's not an option, perhaps having the scrollbar elsewhere (and taller) might suit

> 2.) How do I get it so that it does that for just that scroll bar?


I know I can set Preferred size, but often the layout manager, usually grid or border layout, screws things up.

I had thought of something. Is there a way, say, with getTrackBounds() in the MyScrollBarUI() that I could tamper with the bounds of the JScrollBar boundary? I'm assuming if I increase the size of the track, that could work too without having to resize it.




Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I know I can set Preferred size, but often the layout manager, usually grid or border layout, screws things up.

unless you've changed the layoutManager of the scrollpane, the scrollpane's parent layoutManager
should have no effect on the scrollbar (unless the scrollbar is separated from the scrollpane).

can you post a demo program that displays the problem i.e. where you resize and it doesn't work,
because of the layoutManager of the scrollpane's parent.

digging into the ScrollBarUI will probably work, but you may find if you change something via
an override, this breaks something else, and you end up with a lot of code.
Paul Adcock
Ranch Hand

Joined: Jan 22, 2011
Posts: 48

Michael Dunn wrote:> I know I can set Preferred size, but often the layout manager, usually grid or border layout, screws things up.

unless you've changed the layoutManager of the scrollpane, the scrollpane's parent layoutManager
should have no effect on the scrollbar (unless the scrollbar is separated from the scrollpane).

can you post a demo program that displays the problem i.e. where you resize and it doesn't work,
because of the layoutManager of the scrollpane's parent.

digging into the ScrollBarUI will probably work, but you may find if you change something via
an override, this breaks something else, and you end up with a lot of code.



Ok, forget the resize for now. What you did worked fine.

However, this might be hard to do individually, though I might be able to do it, though I don't know how, if I used UIManager, but can I change the highlight color around the scrolling thing? There's like this highlight which appears, and I think it's supposed to be appearing anyway, around the image as it moves. It was there when it was a rectangle too. Right now it's a medium to light blue. Is there a way I could make it, say, be Red or orange, without having to make all Scroll bars that way?

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> There's like this highlight which appears, and I think it's supposed to be appearing anyway, around the image as it moves.

I think what you're seeing is a combination of highlight color and shadow.

just to be sure we're talking about the same thing, add this to your program and see if it's the area you want
UIManager.put("ScrollBar.thumbShadow",new javax.swing.plaf.ColorUIResource(Color.RED));
UIManager.put("ScrollBar.thumbHighlight",new javax.swing.plaf.ColorUIResource(Color.YELLOW));

if it is, you should be able to change it in an overridden paintThumb()
Paul Adcock
Ranch Hand

Joined: Jan 22, 2011
Posts: 48

Michael Dunn wrote:> There's like this highlight which appears, and I think it's supposed to be appearing anyway, around the image as it moves.

I think what you're seeing is a combination of highlight color and shadow.

just to be sure we're talking about the same thing, add this to your program and see if it's the area you want
UIManager.put("ScrollBar.thumbShadow",new javax.swing.plaf.ColorUIResource(Color.RED));
UIManager.put("ScrollBar.thumbHighlight",new javax.swing.plaf.ColorUIResource(Color.YELLOW));

if it is, you should be able to change it in an overridden paintThumb()


I know that it might work but will this just affect my JScrollBar or ALL JScrollbars? UIManager I think seems to be a one size fits all sort of class.

Also, I did it and nothing happened. Are you sure you didn't mean BasicScrollBarUI?

Also, I already got the image there. I don't want to paint the image any more than it already is. There is like a highlight track that appears when you drag the scroll thing. Hard to describe it, only to say it's a medium blue color by default. Same default color as a selected JMenuItem.

Perhaps I should use paintIncreaseHighlight. The API guy forgot to describe what that does. I'm going to have to try it myself and hope it doesn't mess up anything.

Yes, it was indeed paintIncreaseHighlight() and paintDecreaseHighlight() that I was looking for.


 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Make the ScrollBar of a JScrollPane an image instead of a rectangle
 
Similar Threads
Problem with the JScrollPane
Checkbox using Swing
Converting from Swing to Applet
JPopupButton
After rotating couldnt view