3

I have a series of column labels that scrolls independently from the data that is displayed in a matrix below. I can make the whole scrollbar transparent except on hover. The labels are right up against the data, which I like, however, upon hover, unless I shift the vertical scroll (which I'd rather not do), the scrollbar obscures the beginning of all the labels.

I would like to set the background of the scrollbar as transparent so that only the "grabber" (or whatever it's called) is the only thing that is drawn. (It will obscure the beginning of the labels it is over, but would be a lot less so.)

Is there any way to do that? Here is what I tried:

Color bg = new Color(255,255,255,0);
colLabelScroll.setBackground(bg);

This does not seem to make the background of the scrollbar transparent.

What I'm shooting for is like how the iPhone's scrollbar grabber hovers over info in some apps. Is that even possible with JScrollBars?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
hepcat72
  • 890
  • 4
  • 22
  • Swing doesn't know how to deal with alpha based colors, either the component is fully transparent or it is not. If you are using a `JScrollPane`, the `JScrollBar`s are typically outside the viewport's area (they don't overlap), expect on MacOS, but they are done automatically – MadProgrammer May 27 '15 at 22:26
  • Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer May 28 '15 at 00:05
  • I "suspect" that you really should be using a `JScrollPane`... – MadProgrammer May 28 '15 at 00:06
  • I would like to set the background of the scrollbar as transparent so that only the "grabber" (or whatever it's called) is the only thing that is drawn. ---> [maybe transparent JScrollBar](http://stackoverflow.com/a/16375805/714968) by @aterai – mKorbel May 28 '15 at 05:43
  • I am using a JScrollPane, but I want only 1 of its contained JScrollBars to behave this way. +1 to @mKorbel. That looks like what I want. BTW, about to post another scrollbar question... I also want to change the display policy of the scrollbar on mouseEntered/Exited. – hepcat72 May 28 '15 at 16:13
  • @mKorbel - If you answer as such, I'll click it as the answer. – hepcat72 Aug 03 '15 at 13:18

3 Answers3

0

Transparent JScrollBar can do it, but consider this: if column labels are related to the data and you can scroll them independently, beginner users may not understand what is going on and associate column labels with whatever is visually aligned beneath it. Either you will need some sort of visual indicator that makes it clear that the labels are disconnected from the data, or you should change the way labels are scrolled that never leaves them statically in 1 place.

Here's how I ended up making the relationship between the labels and the data clearer:

  1. Instead of allowing the user to independently and intentionally scroll the labels, I decided to control the label scroll position via mouse hover. This eliminates the need for the obtrusive scrollbar.
  2. I created a scroll-bar-like indicator that shows the portion of the data the labels represent.
  3. I highlighted the currently hovered label that corresponds to the data below it, i.e. the only label that is ever correctly aligned with the data is the one that is under (or directly above) the cursor.
  4. When the mouse is not hovered over (or dragging from) the column labels, do not display any labels. This helps prevent invalid label/data associations by the user.

A few nuanced notes: Implementing your own scrollbar-like indicator is somewhat involved, especially if your labels are painted and then rotated, because the paint position of 0 is at the bottom of the pane, yet the vertical scroll position of the pane is at the top. You will have to track the vertical scroll position to be able to recover it again when the cursor returns since you are blanking the labels on mouse out.

Community
  • 1
  • 1
hepcat72
  • 890
  • 4
  • 22
0

When developing a plugin for IntelliJ, I accomplished it with:

scrollPane.getVerticalScrollBar().setUI(ButtonlessScrollBarUI.createTransparent());

It takes advantage of the the:

ButtonlessScrollBarUI.createTransparent()

method, which is an IntelliJ specific method. However, if you can find a ScrollBarUI which has a transparent background, you can use the same trick.

Nathan
  • 431
  • 5
  • 8
0

Since I got a bit lost myself at first after reading @hepcat72's answer I'm posting a little explanation about the BasicScrollBarUI class:

JScrollBar scrollbar = scrollPaneConversation.getVerticalScrollBar();
scrollbar.setUI(new BasicScrollBarUI(){

    // This function returns a JButton to be used as the increase button
    // You could create your own customized button or return an empty(invisible) button 
    @Override
    protected JButton createIncreaseButton(int orientation){
    }

    // Same as above for decrease button
    @Override
    protected JButton createDecreaseButton(int orientation){
    }

    // This function paints the "track" a.k.a the background of the scrollbar
    // If you want no background just return from this function without doing anything
    // If you want a custom background you can paint the 'Graphics g' object as you like
    @Override
    protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds)
    {
    }

    // This function paints the "thumb" a.k.a the thingy that you drag up and down
    // You can override this function to paint it as you like
    @Override
    protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
    {
    }

});

Refer to the Transparent JScrollBar link posted by @hepcat72 for hints about what to do exactly in these functions.

Pouria P
  • 565
  • 1
  • 8
  • 21
  • Yeah, I realize after reading my answer now (so long after having done it) that I’m taking a lot of contextual knowledge about my own project for granted, without which, my answer has very little meaning. So let me see if I can provide some of that context: the width of my columns in the table and the width of the column and row labels was different and I, at the time, was grappling with that issue. My attempts at scroll bars and independent label and matrix scrolling suffered from impedances to making correct visual associations and I decided not to use a scroll bar for the labels. – hepcat72 Mar 08 '20 at 15:14