Java jLabel on top of another listener
I want to read if one jLabel is on top of another. I drag jLabel1 on top
of jLabel2 and drop it there and do something if it is on top. Code:
//Listening if jLabel1 is pressed for moving it
private void jLabel1MousePressed(java.awt.event.MouseEvent evt) {
Component comp = (Component)evt.getSource();
initialLoc = comp.getLocation();
initialLocOnScreen = evt.getLocationOnScreen();
}
//Listening if jLabel1 was realeased and leave it there
private void jLabel1MouseReleased(java.awt.event.MouseEvent evt) {
Component comp = (Component)evt.getSource();
Point locOnScreen = evt.getLocationOnScreen();
int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x;
int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y;
comp.setLocation(x, y);
OptionsDrop = true; //This should be true if it's dropped
}
//Listener for moving jLabel1
private void jLabel1MouseDragged(java.awt.event.MouseEvent evt) {
Component comp = (Component)evt.getSource();
Point locOnScreen = evt.getLocationOnScreen();
int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x;
int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y;
comp.setLocation(x, y);
OptionsDrag = true; //This should be enabled if it was moved
}
//And so if mouse was on top and both bools are true it should set visible
a panel in my case named Login_Panel
private void Preview_1MouseEntered(java.awt.event.MouseEvent evt) {
if(OptionsDrag == true && OptionsDrop == true){
MainMenu.Login_Panel.setVisible(true);
}
}
I can move jLabel1 but when it's dropped on other label named Preview_1 it
does nothing.
No comments:
Post a Comment