validate() problem

i add some labels on form and i can drag and drop those labels. it works but when i add a new label all other labels go back their first position. i think it is because of validate() method. but how can i refresh form. here is my code...

import java.awt.event.*;

import java.util.*;

import javax.swing.JFrame;

import javax.swing.*;

import java.awt.*;

publicclass ekleextends JFrameimplements MouseMotionListener,MouseListener{

Point location;

MouseEvent pressed;

ArrayList v;

Container c;

int i=0;

public ekle(){

JButton but;

c=getContentPane();

c.setLayout(new FlowLayout());

v=new ArrayList(10);

but=new JButton("Click");

c.add(but);

but.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent event){

JLabel T=new JLabel("deneme label");

addcomp(T);

}});}

publicvoid addcomp(Component Co){

Co.addMouseMotionListener(this);

Co.addMouseListener(this);

v.add(i,Co);

c.add(Co);

c.validate();

i++;

}

publicvoid mousePressed(MouseEvent e)

{

pressed = e;

}

publicvoid mouseClicked(MouseEvent e){}

publicvoid mouseReleased(MouseEvent e){}

publicvoid mouseDragged(MouseEvent e)

{

int j=0;

while(j<v.size()){

Component comp= (Component)v.get(j);

if(e.getSource()==comp){

location = comp.getLocation(location);

int x = location.x - pressed.getX() + e.getX();

int y = location.y - pressed.getY() + e.getY();

comp.setLocation(x,y);

}

j++;

}

}

publicvoid mouseMoved(MouseEvent e){}

publicvoid mouseEntered(MouseEvent e){}

publicvoid mouseExited(MouseEvent e){}

publicstaticvoid main(String args[]){

ekle pen =new ekle();

pen.setSize(500,300);

pen.setVisible(true);

}

}

>

[4332 byte] By [aDiGea] at [2007-11-26 20:06:30]
# 1

If you really want to perform explicit positioning then you will need to set the layout to null, or (better) to a custom manager which supports explicit repositioning. When validate() is called the layout manager lays out the components, setting their bounds and overriding the bounds you set. Calls to setBounds()/setSize()/setLocation() should generally only be called by layout managers.

itchyscratchya at 2007-7-9 23:08:01 > top of Java-index,Desktop,Core GUI APIs...
# 2
which layout manager support explicit repositioning..can you tell me some.. thanks..
aDiGea at 2007-7-9 23:08:01 > top of Java-index,Desktop,Core GUI APIs...
# 3
As I said, you can set it to null or you can use a custom one.
itchyscratchya at 2007-7-9 23:08:01 > top of Java-index,Desktop,Core GUI APIs...
# 4
correct me if i am wrongyou want to say c.setLayout(null);when i did, it doesnt work,it doesnt add labels on the form,Custom one BorderLayout,GridBagLayout,are these?
aDiGea at 2007-7-9 23:08:01 > top of Java-index,Desktop,Core GUI APIs...
# 5

when i did, it doesnt work,it doesnt add labels on the form,

Correct. It doesn't set the bounds, so you will need to do so explicitly yurself. The default bounds are 0,0,0,0 which means the component cannot be seen.

Custom one BorderLayout,GridBagLayout,are these?

No, they are standard ones. By 'custom' I mean one which you write yourself. In this case it would maintain a Map containing Rectangles of the explicit bounds for each component, keyed by the Component itself. It would also be responsible for adding the mouse listener which allowed you to manipulate the component. The add/removeLayoutComponent() methods would handle the addition and removal from the Map as well as adding and removing the listener; addLayoutComponent() would also define the default bounds for a new component (or if you implement LayoutManager2 this can be passed as the constraint parameter); the getPreferredSize() method would simply determine the bounding rectangle required to display all the components; the layoutContainer() method would simply pull rectangles out of the Map and apply them to the children.

It's a common requirement and would only take about an hour to write if you know what you're doing, so you may find a reusable one on the web.

itchyscratchya at 2007-7-9 23:08:01 > top of Java-index,Desktop,Core GUI APIs...