0

I am working on a JAVA Gui development.

I have a Jpanel, and I can add a Jlabel to it and drag it to somewhere else inside the panel. Then I can create a new Jlabel, and so on. The problem I can't solve is that when a new JLabel is created and added to the panel, and the panel call repaint(). The existing labels and the new JLabel are lined up. I want the existing labels to stay in their locations when a new label is added.

Any advice? Thank you!

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
zhuangr
  • 51
  • 6
  • You're fighting the layout manager, which defaults to `FlowLayout`. I would suggest maybe having a look at [this example](http://stackoverflow.com/questions/11819669/absolute-positioning-graphic-jpanel-inside-jframe-blocked-by-blank-sections/11822601#11822601) which uses a custom layout manager to position components at specific points, it wouldn't be hard to modify to allow you to change the "virtual point" value for the component as you updated it. This will tie into the rest of the API and reduce the overall number of issues – MadProgrammer Apr 20 '17 at 04:53

1 Answers1

0

To position components at a random location you need to either:

  1. use a null layout (not recommended)
  2. use a custom layout manager

Check out the Drag Layout. It is a custom layout manager designed to provide layout management support while letting you manage the location of each component.

camickr
  • 321,443
  • 19
  • 166
  • 288