I am trying to move a JTextArea by clicking and dragging. I have the basic concept down but for some reason, when I drag it, it is showing up along the path I drag. It is easiest to explain by showing you:
I'm not sure what is wrong because I'm not creating a new JTextArea on a Mouse Drag, I am using: component.setLocation(x, y);
Why is this happening?
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.event.MouseInputAdapter;
public class Editor {
public static void main(String[] args) {
JFrame frame = new Window();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(30, 30, 1000, 700);
frame.getContentPane().setBackground(Color.white);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class Window extends JFrame {
MyPanel myPanel = new MyPanel();
private static final long serialVersionUID = 1L;
public Window() {
addMenus();
}
public void addMenus() {
getContentPane().add(myPanel);
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel {
private static final long serialVersionUID = 1L;
public MyPanel() {
JTextArea textArea = new JTextArea("Some text\nSome other text");
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
add(textArea);
DragListener drag = new DragListener();
textArea.addMouseListener(drag);
textArea.addMouseMotionListener(drag);
}
@Override
protected void paintComponent(Graphics g) {
}
public class DragListener extends MouseInputAdapter {
Point location;
MouseEvent pressed;
public void mousePressed(MouseEvent me) {
pressed = me;
}
public void mouseDragged(MouseEvent me) {
Component component = me.getComponent();
location = component.getLocation(location);
int x = location.x - pressed.getX() + me.getX();
int y = location.y - pressed.getY() + me.getY();
component.setLocation(x, y);
}
}
}
Painting in Swing is made up a chain of (complex) method calls. If you choose to break this chain, the you had better be ready to take over the work that this methods do...
The Graphics
context is a shared resource, that is, the same Graphics
context will be passed to each component involved in a given paint cycle. It is the responsibility of each component to first clear the Graphics
context before performing any custom painting
You've overriden paintComponent
...
@Override
protected void paintComponent(Graphics g) {
}
but you have failed to honour it's responsibilities, one of which is to clear the Graphics
context before painting.
If you intend to do some custom painting, I would highly recommend calling super.paintComponent
first...
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}
or, if you have no intention of performing custom painting, remove the method altogether.
Take a look at
for more details
You should also have a look at Initial Threads and make sure you are intialising your UIs within the context of the Event Dispatching Thread