Alignment of JTextArea in OverlayLayout

From: anu (maryanu_at_rediffmail.com)
Date: 10/30/04


Date: 30 Oct 2004 04:22:42 -0700

Hi,

I have a panel, say WorkPanel for which the OverlayLayout is set.
I have placed one image into it. And when I click on any area of the
image i want to add a textarea (JTextArea) over the image. (ie for
adding comments to the image)

Now when i add textarea its getting added to the correct position. But
when I start typing on the first textarea, all the textareas are moved
to the centre of the image, overlapping each other. I am including the
code here

public class WorkPanel extends JPanel
{
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
                
            //Draw image
            g.drawImage(img, sx, sy, this);
            repaint();
        }

...
...
    OverlayLayout overlay = new OverlayLayout(this);
    this.setLayout(overlay);
        
    this.addMouseListener(new ImageMotionListener(this));
    this.addMouseMotionListener(new ImageMotionListener(this);
...
...
}

On mouse click event

public void mouseClicked(MouseEvent me)
{
 
        int x = me.getX();
        int y = me.getY();

                                
        JTextArea cb = new JTextArea("comm :");
                
        Dimension dim = new Dimension(60, 50);
        cb.setSize(dim);
        cb.setMinimumSize(dim);
            cb.setMaximumSize(dim);
        cb.setPreferredSize(dim);
        cb.setLocation(x,y);

        workPanel.add(cb);
}

Also when i type on the textarea its not getting expanded as and when
i enter contents into it. For this I removed "cb.setMaximumSize(dim)".
Then immedietly when i start typing the textarea got expanded to the
fullsize of the workpanel.
Also when i removed "cb.setSize(dim)" textarea was not visible.

what could be the problem?

regards
anu.