blast7
blast7

Reputation: 45

setLocation() does not always work with JLabel objects inside a panel with no layout manager, is this a bug?

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:

  1. The size of this component never changes
  2. This size of the child components never change once they have been added to the parent
  3. Only the number and location of child components change when a user action dictates they should
  4. This code is not localized
  5. I can't post code examples as the code is production software and would be too lengthy for this forum

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

Answers (2)

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

Azad
Azad

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

Related Questions