I have some code with two images under a glass pane. I want to make it so that each image is under its own glass pane, and each glass pane signals of its own mouselistener. Currently I have made both of them under one glass pane, with one mouselistener for the entire glass pane. Both images are in grid layout side by side, so it shouldn't be too hard to split the glass pane in half. Here is the code of just one glass pane, but note that I am trying to make two glass panes, and two seperate mouse listener classes for each image. This is just code with one* mouse listener for **both images:
package Buttons;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Giraffewindow extends JDialog {
public Giraffewindow() {
JDialog giraffewindow = new JDialog();
Icon giraffe = new ImageIcon(getClass().getResource("giraffe.png"));
Icon windows = new ImageIcon(getClass().getResource("windows.png"));
giraffewindow.setLayout(new GridLayout(1, 2, 0, 0));
giraffewindow.add(new JLabel(windows));
giraffewindow.add(new JLabel(giraffe));
giraffewindow.pack();
giraffewindow.setTitle("GIRAFFE!");
giraffewindow.setVisible(true);
giraffewindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JPanel glass = ((JPanel) giraffewindow.getGlassPane());
glass.setVisible(true);
status = new JLabel("I can change");
glass.add(status);
glass.setLayout(null);
giraffemousehandler giraffemouse = new giraffemousehandler();
glass.addMouseListener(giraffemouse);
glass.addMouseMotionListener(giraffemouse); //Can I add mouse motion listener to a picture
// setLayout(null);
}
JLabel status = null;
class giraffemousehandler extends MouseAdapter implements MouseListener, MouseMotionListener { //MouseAdapter makes it so that you don't have to have all 7 implemented mouse listener methods
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
status.setBounds(e.getX(), e.getY(), 50, 60); //Makes JLabel follow mouse
}
@Override
public void mouseEntered(MouseEvent e) {
status.setText("Enter);
}
@Override
public void mouseExited(MouseEvent e) {
status.setText("Exit");
// status.setBounds(e.getX(), e.getY(), 5, 6);
}
}
}
Here is the code on camickr's request, please note there are two separate mouse listeners, I would love to know how to do it otherwise. When the JLabel follows the mouse, 1) it is extremely far from the mouse, 2) it does not show the full JLabel and 3) It does not change after one exit/enter. I really appreciate the help, and here is the code based on camickrs advice:
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
class SSCCE extends JDialog {
public SSCCE() {
JDialog giraffewindow = new JDialog();
Icon giraffe = new ImageIcon(getClass().getResource("giraffe.png"));
Icon windows = new ImageIcon(getClass().getResource("windows.png"));
giraffewindow.setLayout(new GridLayout(1, 2, 0, 0));
JLabel giraffelabel = new JLabel();
JLabel windowlabel = new JLabel();
windowlabel.setIcon(windows);
giraffelabel.setIcon(giraffe);
giraffewindow.add(windowlabel);
giraffewindow.add(giraffelabel);
giraffewindow.setTitle("Title!");
giraffewindow.setSize(1100, 600);
giraffewindow.setVisible(true);
giraffewindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JPanel glass = ((JPanel) giraffewindow.getGlassPane()); //Glasspane
glass.setVisible(true);
status = new JLabel("I can change"); //This is the JLabel which should follow my mouse
glass.add(status);
glass.setLayout(null);
giraffemousehandler giraffemouse = new giraffemousehandler();
windowmousehandler windowmouse = new windowmousehandler();
windowlabel.addMouseListener(windowmouse);
giraffelabel.addMouseMotionListener(giraffemouse); //Can I add mouse motion listener to a picture
// setLayout(null);
}
JLabel status = null;
class giraffemousehandler extends MouseAdapter implements MouseListener, MouseMotionListener { //MouseAdapter makes it so that you don't have to have all 7 implemented mouse listener methods
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
status.setBounds(e.getX(), e.getY(), 50, 60); //Makes JLabel follow mouse
}
@Override
public void mouseEntered(MouseEvent e) {
status.setText("Mouse is on giraffe");
}
@Override
public void mouseExited(MouseEvent e) {
status.setText("Mouse has left giraffe");
// status.setBounds(e.getX(), e.getY(), 5, 6);
}
}
class windowmousehandler extends MouseAdapter implements MouseListener, MouseMotionListener {
public void mouseMoved(MouseEvent event) {
// TODO Auto-generated method stub
status.setBounds(event.getX(), event.getY(), 50, 60); //Makes JLabel follow mouse
}
public void mouseEntered(MouseEvent event) {
status.setText("Mouse is on window");
}
@Override
public void mouseExited(MouseEvent event) {
status.setText("Mouse has left window");
// status.setBounds(e.getX(), e.getY(), 5, 6);
}
}
}
public class Icallsscce {
public static void main(String [] args) {
SSCCE object = new SSCCE();
}
}
Almost every problem can be demonstrated in 10-50 lines of code if you truly understand what you are trying to achieve.
No. A component is a component, you can't split a component into two.
You have a couple of options:
If you add the MouseListener to the GlassPane you will then need to use the mouse point from the event and use the Container.getComponentAt(...)
to determine which component the mouse is currently over. Note that the GlassPane covers the entire frame so the mouse point is relative to the frame which includes the Borders so you will first need to use the SwingUtilities.convertPoint(...)
method to convert the mouse point to a point relative to the panel that you added the labels to.
If you add separate MouseListeners to each label then the mouse point will be relative to the label so you will need to add the "x" value of the label to the point on the glass pane when you move your popup label. I think this approach is easier. Note with this approach you can share the same listener, you just need to use the getSource()
method of the MouseEvent to get the label.
Edit:
how I can add the getSource() method to my mouseevent?
This was already answered 10 days ago: Rollover on JLabel which consists of image within grid layout?.
It get annoying when you don't even look at the code because it is not a complete program. We are not here to write the code for you. When someone takes the time to answer a question, the least you can do it take the time to understand the suggestion.
1) it is extremely far from the mouse,
I already answered that in this question. I stated you will need to add the "x" value of the label..
. You need to do this because mouse events are generated relative to the component the listener is added to.
So for the giraffe label the value of x will start at 0 and increase as the mouse is moved to the right. However, the giraffe starts at the center of the Glass Pane, so you can't use 0 as the location of your popup label. You also need to include the x locaton of the giraffe.
So the basic code should be:
popupLabel.setLocation(e.getX() + giraffe.getLocation().x, e.getY());
Of course you should be using the "source" object (not the giraffe label) of the MouseEvent as demonstrated in the answer above. When you use the source object the code will work for both labels.
2) it does not show the full JLabel
because you are using "magic numbers" in your setBounds() method. Why did you use a number like "60" for the width of the label? Don't hard code numbers. Instead when you change the text of the label you can do:
label.setText(....);
label.setSize(label.getPreferredSize());
Now the label will have the proper width/height. Then when you move the label on the glass pane you just use the setLocation(...)
method to position the label.
It does not change after one exit/enter
You are implement a MouseListener and a MouseMotionListener, so you need to add both of those listeners to each component. So you need:
giraffe.addMouseListener(...); giraffe.addMouseMotionListener(...); window.addMouseListener(...); window.addMouseMotionListener(...);
Again, remember you only need one class to implement the MouseListener and MouseMotionListener as demonstrated 10 days ago. The same instance of the listener can be added to both components which will help clean up your code.