Reputation: 45
So I've search these forums for an answer to this question and can't seem to find one.
First let me state that I completely understand why going with a null layout manager is a bad idea and in this case I basically have no choice. I'm working with a legacy code base and it would be very time consuming to try and write a custom layout manager to perform the correct layout. Some relevant info:
The code flow is as follows:
One of the types of child components are 1 or more JLabel objects. This is the object I'm having problems with. In certain cases the call to the JLabel's setLocation() function will have values of x= 52 and y = 0 yet the JLabel gets put at the location x = 0, y = 0. I've already confirmed that the location isn't being reset to 0,0 by some other area of the code so now I'm left with the thought that maybe there is some obscure bug in the Java implementation that is causing the problem. Otherwise, why would it not respect the call to setLocation()?
I am going to confirm the setBounds() isn't being called on the JLabel by any other area of the code. Are there any other calls that could affect the position of a component when a layout manager is NOT being used?
Upvotes: 1
Views: 675
Reputation: 93
I have done some experiments with setLocation() in java https://dl.dropboxusercontent.com/u/27670533/ani_test.rar
this are the src file take a good look at the them maybe it helps
Upvotes: 0
Reputation: 5055
With absolute positioning you have to consider three steps, you've already done the first step by setting the layout to null
.
The next step is whenever you add a component to the container, you've to use setBounds(x,y,width,height)
method on that component.
You only used setLocation()
method without setting the size of the button, you have to use setSize(width,height)
method also if you want the button to be displayable.
After all, don't forget to call the Component class's repaint()
method.
Upvotes: 1