Question

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.

Was it helpful?

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)

OTHER TIPS

Adding a Component to a Container is something you do whether you use a null layout or a LayoutManager.

The 3 things that I believe you need to remember when using a null layout are:

  1. set the size of the component (usually the preferred size)

  2. set the location of the component

  3. set the preferred size of the Container containing the components. Most time your code will work if you don't set the preferred size, but try adding the panel to a scroll pane and it won't work. The reason for this is that scrollbars appear automatically when the preferred size of the Container is greater than the size of the scrollpane. With a null layout the preferred size is 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top