I am making a game in which i have a main menu. I want to load the menu items from an array making it much more efficient to add new items to the menu. I want to have a mouse listener active for each JLabel the for loops creates so i can check if the mouse is hovering over it and to see when it has been clicked. How would i achieve this?
Here's the code i'm using currently, it creates the JLabels fine although the mouse listeners do no work.
private String[] items = { "Exit", "Mods", "Settings", "New Game", "Play Game" };
private String item_hover_left = "[";
private String item_hover_right = "]";
private void createItems() {
GameSound sound = new GameSound();
int initialY = 951;
JLabel[] item = new JLabel[items.length];
for (i = 0; i < items.length; i++) {
int y = initialY - (101 * i);
item[i] = new JLabel();
item[i].setText(items[i]);
item[i].setForeground(ui_colour);
item[i].setFont(new Font("Overseer", Font.PLAIN, 50));
item[i].setHorizontalAlignment(SwingConstants.CENTER);
item[i].setBounds(1582, y, 328, 99);
add(item[i]);
repaint();
item[i].addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent pressed) {
if (pressed.getButton() == MouseEvent.BUTTON1) {
sound.playSound("menu_ok");
}
}
public void mouseEntered(MouseEvent entered) {
item[i].setText(item_hover_left + " " + items[i] + " " + item_hover_right);
sound.playSound("menu_hover");
}
public void mouseExited(MouseEvent exited) {
item[i].setText(items[i]);
}
});
}
}
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 5 at net.fallout.menu.MainMenu$1.mouseEntered(MainMenu.java:87) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEnterExit(Unknown Source) at java.awt.LightweightDispatcher.trackMouseEnterExit(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at > java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at > java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)