Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JScrollPanel without scrollbars

I'm trying to use a JScrollPanel to display a JPanel that might be too big for the containing Jpanel.

I don't want to show the scrollbars (yes, this is questionable UI design, but it is my best guess of what the customer wants. We use the same idea other places in the application, and I feel this case have given me enough time to ponder if I can do it in a better way, but if you have a better idea I might accept it an answer.)

First attempt: set verticalScrollBarPolicy to NEVER. Result: Scrolling using mouse wheel doesn't work.

Second attempt: set the scrollbars to null. Result: Scrolling using mouse wheel doesn't work.

Third attempt: set scrollbars visible property to false. Result: It is immidiately set visible by Swing.

Fourth attempt: inject a scrollbar where setVisible is overridden to do nothing when called with true. Result: Can't remember exactly, but I think it just didn't work.

Fifth attempt: inject a scrollbar where setBounds are overridden. Result: Just didn't look nice. (I might have missed something here, though.)

Sixth attempt: ask stackoverflow. Result: Worked like a charm this time.

Scrolling works once scrollbars are back.

like image 252
Erik I Avatar asked Apr 15 '10 20:04

Erik I


1 Answers

scrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(0,0));

Will hide the scrollbar, and let you scroll with your mouse wheel.

like image 152
ring bearer Avatar answered Sep 21 '22 13:09

ring bearer