infoSyStem
infoSyStem

Reputation: 145

how to make JPanel's length and width extendible automatically?

I have a JScrollPane and inside it I placed a JPanel. There is also a button, when pressed, a rectangle of random length and width will be drawn on the panel. So each time this button is pressed, the repaint method of the JPanel is called.

The problem is that sometimes the length of the rectangle is high and so the JPanel does not display it all, just part of it that already fits in the panel.

How can I make the JPanel display what is drawn inside it nomatter what the dimensions of the rectangle are?

Upvotes: 2

Views: 251

Answers (2)

mre
mre

Reputation: 44250

One option is to draw the rectangle onto the Graphics object of a BufferedImage of similar dimensions, and then use this image as the Icon of a JLabel. Add this label to the panel at the arbitrary coordinates and then revalidate the JScrollPane and issue a repaint request.

Upvotes: 2

Johnydep
Johnydep

Reputation: 6277

your ScrollPane is limiting the size, default panel will stretch to accommodate it's child components. ScrollPane is fixed, that is why it is called ScrollPane, what you can do is programatically calculate the size of rectangle and repaint scrollpane with that size at each call of button press or better yet, you should know the possible sizes and make your GUI appropriate enough to make up for all such sizes.

Upvotes: 1

Related Questions