Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a simple way to add mouse event on flatlaf scrollbar component? #115

Closed
UltraBurstXD opened this issue Jun 17, 2020 · 5 comments
Closed
Milestone

Comments

@UltraBurstXD
Copy link

UltraBurstXD commented Jun 17, 2020

Hi JFormDesigner,

I'm trying to add a simple mouse event (mouse Pressed) to change the thumbColor paint (momentarily) from the user's mice left button input, in my flatscrollbarUI class but it doesn't work... Is there a convenient way to do it?

Pls help...

@DevCharly
Copy link
Collaborator

You can simply add the mouse listener to the JScrollBar. See:

scrollbar.addMouseListener( hoverListener );

You probably have to change hoverThumbColor because this color is used when the mouse pointer is over the thumb.

Or override method getThumbColor:

protected Color getThumbColor( JComponent c, boolean hover ) {
Color trackColor = FlatUIUtils.deriveColor( this.trackColor, c.getBackground() );
Color thumbColor = FlatUIUtils.deriveColor( this.thumbColor, trackColor );
return hover ? FlatUIUtils.deriveColor( hoverThumbColor, thumbColor ) : thumbColor;
}

And maybe invoke scrollbar.repaint() ?

Anyway, I think it would be nice if FlatScrollBarUI would support some "pressed" colors out of the box. This is now missing. I'll add them in the next days...

@UltraBurstXD
Copy link
Author

UltraBurstXD commented Jun 17, 2020

Thanks, I really appreciate it.

@UltraBurstXD
Copy link
Author

UltraBurstXD commented Jun 17, 2020

Well, I tried to invoke and use repaint() method...

Screenshot from 2020-06-17 20-21-23

But it seems not going anywhere... (I'm new to graphics object stuff...)

@DevCharly
Copy link
Collaborator

@UltimateYZ g.setColor(...) should be invoked only in paint methods just before painting lines, rectangles, text, etc. Not sure where the g comes from in your mousePressed method.

Try following:

hoverThumbColor = Color.red;
scrollbar.repaint();

BTW I've pushed commit fe15f44 to master branch that implements pressed colors for ScrollBars. By default the thumb is made slightly darker/lighter when pressed.

You can use own colors with:

UIManager.put( "ScrollBar.pressedThumbColor", Color.red );

If you like to change the pressed color dynamically, you can override method FlatScrollBarUI.getThumbColor:

@Override
protected Color getThumbColor( JComponent c, boolean hover, boolean pressed ) {
    if( pressed )
        return anyColor;
    return super.getThumbColor( c, hover, pressed );
}

@UltraBurstXD
Copy link
Author

Thanks for the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants