7

I'm trying to use a JScrollPane 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.

1
  • 3
    what is JScrollPanel? did you mean JScrollPane? - may want to edit Apr 15, 2010 at 20:13

1 Answer 1

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.