Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTextArea setLocation problem?

843806Sep 12 2008 — edited Sep 12 2008
The JTextArea setLocation method is not sticking after the call to repaint(). The JTextArea is going back to it's original location; the repaint() method is acting like the pack() method.
I've written a test application to show what I'm talking about. (The test Application is in two files.)

The following program draws a circle and has 5 JTextAreas, you can drag the text areas anywhere on the screen. On the repaint() method call the text areas get reset to their original positions. If you remark the repaint() calls out the text areas stay where you place them. Try to place one of the text areas over the circle with and without the repaint calls commented out, the circle gets erased. This is why I need to call repaint.

This behavior only occurs if the following program is an application. The setLocation method works fine if this is placed in an Applet. This behavior was discovered when I tried to make my Applet an Application. If anyone can provide a work around I'd appreciate it.

Thanks,
Andrew

package test;

import java.awt.Toolkit;
import javax.swing.*;
import javax.swing.UIManager;
import java.awt.Dimension;

public class Test {
boolean packFrame = false;

public static void main(String[] args) {
JFrame frame = new Frame1();

frame.setTitle("Application");
frame.setSize(600, 550);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation( (d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
frame.setVisible(true);
frame.setDefaultCloseOperation(3);
frame.setResizable(false);
}
}

---------------------New File---------------------------

package test;

import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import javax.swing.border.LineBorder;

public class Frame1
extends JFrame implements MouseMotionListener {
Image aFrame = null;

JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JLabel statusBar = new JLabel();
JTextArea[] display = null;
JPanel centerPanel = null;
JPanel moviePanel = null;

public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
statusBar.setText(" ");
contentPane.add(statusBar, BorderLayout.SOUTH);

centerPanel = new JPanel(new BorderLayout());
moviePanel = new JPanel(new GridBagLayout());
display = new JTextArea[5];

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx=gbc.RELATIVE;
gbc.gridy=gbc.RELATIVE;
for (int i=0; i < display.length; i++) {
display[i] = new JTextArea(" ",3,15);
moviePanel.add(display, gbc);
display[i].setBorder(new LineBorder(Color.lightGray));
display[i].addMouseMotionListener(this);
}
centerPanel.add(moviePanel, BorderLayout.CENTER);
contentPane.add(centerPanel, BorderLayout.CENTER);


moviePanel.addMouseMotionListener(this);
centerPanel.addMouseMotionListener(this);
contentPane.addMouseMotionListener(this);
}

public void update(Graphics g) {
paint(g);
}

public void paint(Graphics g) {
for (int i=0; i < display.length; i++) {
display[i].setText("i: "+Integer.toString(i));
}

Graphics2D offscreenG = ((Graphics2D)moviePanel.getGraphics());
int w = centerPanel.getWidth();
int h = centerPanel.getHeight();
background(w,h);

offscreenG.drawImage(aFrame,0,0,this);
}

public void background(int width, int height) {
aFrame = createImage(width, height);
Graphics2D aFrameG = ((Graphics2D) aFrame.getGraphics());
aFrameG.setColor(Color.cyan);
aFrameG.fillOval(5,5,width/2,height/2);
}

public void mouseDragged(MouseEvent e) {
if (e.getSource() instanceof JTextArea) {
JTextArea temp = ((JTextArea)e.getSource());
int x = e.getX();
int y = e.getY();

((JTextArea)e.getSource()).setLocation(temp.getX() + x, temp.getY() + y);
repaint(); // Remark me out
}
}

public void mouseMoved(MouseEvent e) {
repaint(); // Remark me out
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 10 2008
Added on Sep 12 2008
4 comments
773 views