Search code examples
javaswingnull-layout-manager

What are the things you have to do to make a panel work with a null layout manager?


I was having problems getting a null layout manager working, and I found this great page with things to remember when using a null layout manager. One of them was my issue, and I quickly moved on.

I'm now having similar problems with another panel using a null layout manager. However, I cannot find this page anywhere! So what are the things?

In the process of asking this question, I figured out my problem. I still want to add this knowledge to the site, since I couldn't find this question anywhere. It might be helpful to someone else.

To clarify, I already have determined that I need to use a null layout manager for this panel, due to dragging and animation requirements. That is not the question.


Solution

  • I found the link I was looking for:

    http://download.oracle.com/javase/tutorial/uiswing/layout/none.html

    It gives these three things:

    • Set the container's layout manager to null by calling Container.setLayout(null).
    • Call Component.setBounds() for each of the container's children.
    • Call Component.repaint()

    As it happens, though, my problem was that I wasn't doing one additional thing:

    • Add each component to the container using Container.add(Component)