Reputation: 183
I have some code that creates a grid of coloured buttons that I would like to attach mouse listeners to. Ideally, I'd want the program to print a line (e.g. "button entered!") every time my mouse enters any one of these buttons.
However, my current code only prints this message out when the grid itself is entered.
My code is here:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.net.URL;
public class ColorGrid implements MouseListener {
/*
Frame holds the panel
Panel holds the grid framework and the toolbar
*/
JFrame frame = new JFrame();
JLabel[][] grid;
JToolBar toolbar = new JToolBar("Test");
ButtonGroup bgroup = new ButtonGroup();
JPanel panel = new JPanel();
// Example strings for default button implementations - not needed
static final private String PREVIOUS = "previous";
static final private String UP = "up";
static final private String NEXT = "next";
// holds strings that point to URL of button elements
String[] buttonPics = {"images/circle_blue.png", "images/circle_green.png", "images/circle_orange.png", "images/circle_red.png", "images/circle_yellow.png"};
Random rand = new Random();
/*
CONSTRUCTOR
Add buttons to the toolbar
Set layouts for the frame and panel
Populate grid with icons (representing buttons)
*/
public ColorGrid(int width, int length) {
addButtons(bgroup, toolbar);
frame.setLayout(new BorderLayout());
panel.setLayout(new GridLayout(width, length));
grid = new JLabel[width][length];
panel.addMouseListener(this);
for(int y=0; y<length; y++){
for(int x=0; x<width; x++){
int randomColor = rand.nextInt(5);
ImageIcon icon = createImageIcon(buttonPics[randomColor]);
grid[x][y] = new JLabel(icon);
panel.add(grid[x][y]);
}
}
frame.add(toolbar, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/*
Finds URL for button images and returns it
*/
ImageIcon createImageIcon(String path) {
URL imgURL = ColorGrid.class.getResource(path);
if (imgURL != null)
return new ImageIcon(imgURL);
else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
/*
Adds buttons to the ButtonGroup and then add to Toolbar
*/
void addButtons(ButtonGroup bgroup, JToolBar toolbar) {
JToggleButton button = null;
//first button
button = makeNavigationButton("circle_blue", PREVIOUS, "Back to previous something-or-other", "Previous");
bgroup.add(button);
toolbar.add(button);
//second button
button = makeNavigationButton("circle_Green", UP, "Up to something-or-other", "Up");
bgroup.add(button);
toolbar.add(button);
//third button
button = makeNavigationButton("circle_orange", NEXT, "Forward to something-or-other", "Next");
bgroup.add(button);
toolbar.add(button);
button = makeNavigationButton("circle_red", NEXT, "Forward to something-or-other", "Next");
bgroup.add(button);
toolbar.add(button);
button = makeNavigationButton("circle_yellow", NEXT, "Forward to something-or-other", "Next");
bgroup.add(button);
toolbar.add(button);
}
/*
Called by the addButtons() method.
Handles main button creation logic - finds the image and sets the command to the default string commands
*/
JToggleButton makeNavigationButton(String imageName, String actionCommand, String toolTipText, String altText) {
//Look for the image.
String imgLocation = "images/"
+ imageName
+ ".png";
URL imageURL = ColorGrid.class.getResource(imgLocation);
//Create and initialize the button.
JToggleButton button = new JToggleButton();
button.setActionCommand(actionCommand);
button.setToolTipText(toolTipText);
if (imageURL != null) { //image found
button.setIcon(new ImageIcon(imageURL, altText));
} else { //no image found
button.setText(altText);
System.err.println("Resource not found: "
+ imgLocation);
}
return button;
}
public void mouseEntered(MouseEvent e) {
JPanel panel = (JPanel) getComponentAt(e.getPoint());
if (panel != null) {
System.out.println("Button entered!");
}
}
public void mousePressed(MouseEvent e) {
System.out.println("Mouse pressed!");
}
public void mouseReleased(MouseEvent e) {
System.out.println("Mouse released!");
}
public void mouseExited(MouseEvent e) {
System.out.println("Mouse exited!");
}
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked!");
}
public static void main(String[] args) {
new ColorGrid(4,4);//makes new ButtonGrid with 2 parameters
}
}
How can I fix this?
Upvotes: 1
Views: 972
Reputation: 285405
If you want to test for mouse entering a button, add a ChangeListener to the button's ButtonModel and test for isRollover()
Upvotes: 4
Reputation: 347244
I'm not sure you mean "button" as the only thing you're adding to your GridLayout
is JLabel
s...
But basically, you need to register a MouseListener
with each component individuallym for example...
for(int y=0; y<length; y++){
for(int x=0; x<width; x++){
int randomColor = rand.nextInt(5);
ImageIcon icon = createImageIcon(buttonPics[randomColor]);
grid[x][y] = new JLabel(icon);
grid[x][y].addMouseListener(this);
panel.add(grid[x][y]);
}
}
You should also remove the panel.addMouseListener(this);
call as this might just consfuse the issue...
Upvotes: 4
Reputation: 44844
well you are doing
panel.addMouseListener(this);
try changing it to
button.addMouseListener(this);
in the addButtons
method
Upvotes: 0