I am doing a project where I need some custom swing components. So far I have created a new button with a series of images (the appearance of Java Metal does not match my user interface). Ive implemented MouseListenerin this new component, and this is where my problem arises. My widget changes the image on hover, clicks, etc., Except that my MouseListenercaptures mouse input into the entire container GridLayoutinstead of the image. Thus, I have an image about 200 * 100 in size, and the surrounding container is about 400 * 200, and the method mouseEnteredstarts when it enters this sectionGridLayout(even empty parts of space) instead of an image. How can I make it so that it only works when you hover over the image? Ive tried to set size, borders, and other attributes to no avail.
EDIT: My problem is demonstrated here. As you can see (view, the colors are very similar), the lower right button is highlighted simply by entering its section GridlLayout. I want this to be emphasized when I'm above the image, and not in the section GridLayout.

I will not add methods MouseListenerbecause they simply include the display of the displayed image.
public customWidget()
{
this.setLayout(new FlowLayout());
try {
imageDef=ImageIO.read(new File("/home/x101/Desktop/buttonDef.png"));
imageClick=ImageIO.read(new File("/home/x101/Desktop/buttonClick.png"));
imageHover=ImageIO.read(new File("/home/x101/Desktop/buttonHover.png"));
current=imageDef;
} catch (IOException e)
{
e.printStackTrace();
}
this.addMouseListener(this);
}
protected void paintComponent(Graphics g)
{
super.paintComponents(g);
g.drawImage(current, 0, 0, current.getWidth(), current.getHeight(), null);
}
EDIT: code section added
source
share