0

I want drag text from jlabel to jtextfield (i have 9 jlabels and 81 jtextfields).

Code look like:

JPanel pole = new JPanel();

pole.add(tf[0][0]);
tf[0][0].addMouseListener(this);
tf[0][0].setName("0,0");
......
......
pole.add(jlab1);
jlab1.addMouseListener(this);
jlab1.setName("1");
.....
.....



@Override
public void mouseReleased(MouseEvent me) {

    //get drag component (that's ok)
    Component c = me.getComponent();


    //get drop component - doesn't work - return null :( 
    Component d = SwingUtilities.getDeepestComponentAt(this.pole, me.getX(), me.getY());

Why this return null? How get this drop component 'd'?

Kara
  • 6,115
  • 16
  • 50
  • 57
  • [JavaDocs](http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#getDeepestComponentAt(java.awt.Component,%20int,%20int)) say *"If parent does not contain the specified location, then null is returned"* – MadProgrammer Jan 30 '15 at 06:07
  • This might be one of the case where actually using the drop-n-drop API is worth the effort – MadProgrammer Jan 30 '15 at 06:08
  • Something like [this](http://stackoverflow.com/questions/11201734/java-how-to-drag-and-drop-jpanel-with-its-components/11443501#11443501) for example - You'd be interested extracting the text from the label, but the basic idea is sound – MadProgrammer Jan 30 '15 at 06:10
  • But there is jtextfield... – Marek Komarek Jan 30 '15 at 06:10
  • Care to provide a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This will result in less confusion and better responses – MadProgrammer Jan 30 '15 at 06:14

1 Answers1

1

MouseEvents are contextual to the component that created them. This means that when you release the mouse button, the source of the event is actually the same component you clicked on.

The location information returned by the MouseEvent is also within the source components context (0x0 is the top left position of the source component).

This means that when you use Component d = SwingUtilities.getDeepestComponentAt(this.pole, me.getX(), me.getY());, the coordinates are not in the pole context, but the source Components, which likely means that pole does not contain those coordinates and the method return's null.

As the JavaDocs says "If parent does not contain the specified location, then null is returned"

You could try translating the MouseEvent location to the context of pole using something like...

MouseEvent evt = SwingUtilities.convertMouseEvent(e.getComponent(), e, textField);

or

Point p = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), textField);

But there is still no guarantee that the point will be within the components bounds.

You could further test the location of the point in relation to the pole using something more like...

Rectangle bounds = textField.getBounds();
bounds.setLocation(0, 0);
if (bounds.contains(p)) {
    // Finally...
}

or possibly...

Point p = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), pole);                    
Component dropPoint = SwingUtilities.getDeepestComponentAt(pole, p.x, p.y);

But this can still return null if you drag beyond the pole's rectangle bounds...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366