Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mouslistener with 3 jtables

Status
Not open for further replies.

andreas57

Technical User
Sep 29, 2000
110
CH
hi there,

i've got 3 jtables and have added by each one a mouslistener which reaktes to mousepressed & -released. the problem is that when i press the mouse on table1 and let go on tabel2 it does whate is in tabel1 mousereleased. how can i fix this?

see u
:)

andreas owen
aowen@swissonline.ch
 
A mouse released event is gauranteed to occur in the same component as the mouse pressed event. It sounds like you are trying to implement some sort of drag and drop function. You may want to think about using the standard dnd ability.

Now... you might be able to cheat by adding a MouseListener to the Container of the 2 tables and then using "getComponentAt(int x, int y)" to check the component under the event.

Hope this helps...

Josh Castagno
 
Here is some demo code...


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

/**
* <pre>
* Test
* </pre>
*/
public class Test {
public static void main(String[] args) {
JFrame test = new JFrame(&quot;Test Window&quot;);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.getContentPane().setLayout(new BorderLayout(0, 0));

final JLabel lbl1 = new JLabel(&quot;DRAG HERE&quot;);
lbl1.setBorder(new LineBorder(Color.black, 1));
final JLabel lbl2 = new JLabel(&quot;DROP HERE&quot;);
lbl2.setBorder(new LineBorder(Color.black, 1));

final JPanel p = new JPanel();
p.add(lbl1);
p.add(lbl2);
p.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
Component c = p.getComponentAt(e.getX(), e.getY());
System.out.println(&quot;IS DRAG? &quot; + (lbl1 == c));
}

public void mouseReleased(MouseEvent e) {
Component c = p.getComponentAt(e.getX(), e.getY());
System.out.println(&quot;IS DROP? &quot; + (lbl2 == c));
}
});

test.getContentPane().add(p);

test.pack();
test.setVisible(true);
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top