Skip to main content

Adding a JPanel to the center (both x and y axis) of an JInternalFrame?

8 replies [Last post]
samynix
Offline
Joined: 2010-04-17
Points: 0

I got a JPanel with an image in it, And i want to add this JPanel to the direct center of an JInternalFrame. (So when i resize the InternalFrame the image stays in the center, Like photoshop)

Iv tried alot of stuff, but just cant get it to work.

Any help would be highley appreciated!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dhaneshkinfolk
Offline
Joined: 2014-01-02
Points: 0

i hope the attached code helps
it is designed using jform designer (uses grouplayout) - it uses horizontally and vertically resizing jpanels(of equal size) on 4 sides of the center panel which is of fixed size (it is implemented in a jframe, must work on JInternalFrame as well)

rturnbull
Offline
Joined: 2005-08-27
Points: 0

If you get the InternalFrame ContentPane, set to BorderLayout, then add your panel to
the CENTER, it should stay in the middle
or,
set BorderLayout on yor panel and add the image using BorderLayout.CENTER

You may need to add dummy components NORTH, SOUTH, EAST
and WEST, e.g a blank label, to force centering.

An alternative to try is GRIDBAGLAYOUT with anchor CENTER

Message was edited by: rturnbull

kirillcool
Offline
Joined: 2004-11-17
Points: 0

If the image is painted in the top-left corner of that panel, putting the panel in CENTER is not going to help.

samynix
Offline
Joined: 2010-04-17
Points: 0

I worked around it by placing the image in the middle of the panel, but now i get another problem. I want my MouseMotionListener only to function ontop of the image, and get image cords based on that. I worked around this aswell with some math, But the problem comes when i have an image thats bigger then the screen and i get scrollbars. I will try with dummy labels now.

Tnx for feedback

samynix
Offline
Joined: 2010-04-17
Points: 0

Thanks for the help, The Dummy labels did the trick :=)

kirillcool
Offline
Joined: 2004-11-17
Points: 0

Either override your paintComponent() to paint the image in the middle of the panel, or set a custom layout manager on the internal frame content pane to place your panel at the center of its container.

samynix
Offline
Joined: 2010-04-17
Points: 0

I actualy think the problem is that the JPanel automaticly fills the whole JInternalFrame whatever i want or not :/

Wim1981
Offline
Joined: 2014-01-20
Points: 0

Hi,

you can customize the filling of the JPanel with the Weightx and Weighty variables. If they aren't initialized there value is always 0. You put them tho non-zero and they won't fill the whole space.