Scroll doesn't work in JScrollPane - java

I have a JPanel inside a JScrollPane, where I draw some shapes with Graphics.
The problem is when I draw outside the bounds of the panel, the scroll in the scroll pane doesn't work. I have autoresize activated on the JPanel.
Any tip?

The custom painted JPanel should return a preferred size appropriate for the graphic elements it contains.

Related

How do I get the bounds of a JPanel centered in another JPanel?

I have a main panel with a border layout containing another panel in the center.
Because I want to paint inside the centered panel I have to get the bounds of it.
panel_main.add(panel, BorderLayout.CENTER);
I wanted to save the bounds in a rectangle like this:
Rectangle bounding = new Rectangle
(panel.getX(), panel.getY(), panel.getWidth(), panel.getHeight());
But every argument seems to be 0. So how do I get the bounds of the panel?
Could I get the bounds of the BorderLayout instead?
But every argument seems to be 0.
All Swing components have a size of (0, 0) when they are created. Components are only given a size when the frame is packed and made visible.
Because I want to paint inside the centered panel I have to get the bounds of it.
You override the paintComponent() method of the panel. Then you can use the getSize() method to control where the painting is done.
You will also need to override the getPreferredSize() method of the panel so the layout manager can pack() the frame properly.
Read the section from the Swing tutorial on Custom Painting for more information and working examples to get you started.

How to make JPanel resize to fits its components?

I have a JScrollPane whose viewport is a JPanel. The JPanel contains smaller JPanels that take up the entire viewport's width, and the big JPanel is set to a FlowLayout. The user should be able to add as many JPanels as they want (well, up to 200 for my purposes), and they should be able to scroll down the JScrollPane to see everything they have added. Basically I'm just trying to make the JScrollPane grow. I'm calculating what I thought the JPanel's height should be like this:
Dimension dimension = new Dimension(width, smallPanel.height * totalPanels
+ ((FlowLayout) getLayout()).getVGap * totalPanels);
setPreferredSize(dimension);
And it mostly works, but as you add more, it starts cutting the smaller JPanels off, and the bottom panel eventually isn't shown. Is there a way that I can determine the size of the JPanel for the viewport so that I wouldn't have to calculate its dimensions with variables? Like pack() does for JFrame? Or do I need to keep guessing and checking?
Thanks

How can I detect the scrollbar is dragged/clicked in a Jscrollpane?

I have a Jscrollpane with a Jpanel inside. In this Jpanel I draw some shapes. The problem I have is when I scroll up and down. The drawing on the Jpanel appears cut. So I want to redraw the Jpanel every time I drag the scrollbar or click the direction arrows. How can I do it?
(Excuse me for not posting a SCCE, I've generated the interface with netbeans and it's quite complicated)
In this Jpanel I draw some shapes. The problem I have is when I scroll
up and down. The drawing on the Jpanel appears cut. So I want to
redraw the Jpanel every time I drag the scrollbar or click the
direction arrows.
good idea in the case that you repainting concrete Rectangle, Dimension for this Rectangle returns JViewport
I have a Jscrollpane with a Jpanel inside. In this Jpanel I draw some
shapes. The problem I have is when I scroll up and down.
use AdjustmentListener added to JScrollBar (derived directly from JScrollPane or as local variable)

Drawing 2Dcircle in an Jinternal frame

I have a JInternal Frame and I want to draw a circle(using 2Dgraphics) in it and make it flexible. I mean when I change the size of frame the circle become smaller or in making frame larger circle also become larger. Can somebody help?
You would draw in the paintComponent method of a JPanel or JComponent that is held in the JInternalFrame's contentPane, same as you would draw in any other JPanel. I'd get the dimensions of the JPanel at the start of the paintComponent method and use those values to tell how big to draw the circle.
Also, if you add the JPanel directly to the JInternalFrame's contentPane, it will be added by default BorderLayout.CENTER, and so when the JInternalFrame changes size, the JPanel also changes size, it's paintComponent will be called by the JVM, and the new drawing will be resized automatically.
add a WindowListener to the jInteralFrame and redraw whenever the size changes

AutoScroll when Shape is outside a JPanel

I have a JPanel where I draw shapes. this panel is added to a scrollPane. but the scrolling doesn't work when a shape is out of the scene. is there an easy way to autoscroll or I have to do it programmatically.
Thank you
I'm assuming you're using the JPanel as a canvas for custom drawing(i.e. you're not adding any JComponents to it). If that's case, the JScrollPane has no way of knowing how large the JPanel is and will just size it to exactly fill the scrollPane's viewport(which means you'll never actually get scroll bars).
To fix this issue you should override getPreferredSize on your JPanel.
#Override
public Dimension getPreferredSize() {
int height = calculateHeight();
int width = calculateWidth();
return new Dimension(width, height);
}
Edit:
Also, since you're doing custom drawing make sure to call revalidate whenever the shapes you want to draw change. This tells swing that it needs to re-think the layout/size of the JPanel.

Categories