user2999593
user2999593

Reputation:

Java - Moving position of JPanel

I have this program, and what I'm trying to do is simulate a door in a wall. The idea is to give the user the possibilty of interacting with the interface so he can drag the door left and right. I thought of using a JPanel for the wall and another JPanel for the door. So the question is, how can i move a JPanel to another position?

Upvotes: 0

Views: 607

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347332

Normally the container is under the control of a layout manager, which is making decisions about where and what size components need to be.

You have a few options...

You Could

Use a null layout (or absolute layout), but you become responsible for managing the size and position of the components within the container.

There is a lot of work involved with this, as not only do you need to make decisions about the components, but you need to monitor the changes to the parent container as well...

Take a look at Doing Without a Layout Manager (Absolute Positioning)

You Could

Write your own layout manager.

This is a lot of work, but because the Swing API is designed with layout managers in mind, it becomes easier to manage changes to the parent state.

Basically, you would need to devise a series of constraints for your layout manager which described how each component is to be laid out (size and position)

Take a look at Laying Out Components Within a Container for some ideas

Upvotes: 1

Related Questions