public void mouseClicked(java.awt.event.MouseEvent evt)
{
headerLabel.setText("Inventory Slot Clicked");
inventorySlotLabels[labelNum].setBorder(BorderFactory.createLineBorder(Color.WHITE));
}
Don't depend on a label number variable. Get the label from the MouseEvent:
public void mouseClicked(java.awt.event.MouseEvent evt)
{
headerLabel.setText("Inventory Slot Clicked");
//inventorySlotLabels[labelNum].setBorder(BorderFactory.createLineBorder(Color.WHITE));
JLabel label = (JLabel)evt.getSource();
label.setBorder( ... );
}
Edit:
Actually, there is no reason to create 28 MouseListeners. Before your loop the code should be more like:
MouseListener ml = new MouseListener()
{
public void mouseClicked(java.awt.event.MouseEvent evt)
{
headerLabel.setText("Inventory Slot Clicked");
//inventorySlotLabels[labelNum].setBorder(BorderFactory.createLineBorder(Color.WHITE));
JLabel label = (JLabel)evt.getSource();
label.setBorder( ... );
}
};
Then in your loop you just use:
inventorySlotLabels[labelNum].addMouseListener( ml );
Whether you use an annoymouse inner class or an inner class, the better design approach is to use the source object from the MouseEvent, there is no need to create 28 MouseListener just to store a variable to index the label in the Array. In fact you don't even need to keep an Array when you use this approach which will simplify your code even more because now you are no longer restricted to a hardcoded value to determine the size of your array.
Also, there is no need to read the Icon 28 times. Just read the Icon once outside the loop and add the Icon to each label. You also don't need to create 28 Borders. Just create the Border once and share it.
Finally, you should NOT be using setBounds(...) to position and size the label. Use an appropriate layout manager, probably GridLayout, to display the labels in a grid.