I have a JPanel that I put inside a JScrollPane . I manually draw it using paintComponent() , since it is used as a canvas, and I want the panel to automatically match the width of the scroll bar. Then I can use getWidth() in the color code to automatically scale to fit the container. However, I want to be able to manually set my preferred height so that I use the scrollbar's vertical scroll capabilities.
What is the best way to do this? This will obviously work anyway if I could just get the width of the scroll bar in the drawing code, but I don't want to break encapsulation too much with hacked code like getParent() , for example.
source share