Don't do this since you can't add the same component more than once to a visualized container. Better to use multiple JLabels but have them use the same ImageIcon. ImageIcons can be used more than once with ease:
public MainFrame() {
pieceIcon[0] = new ImageIcon(System.getProperty("user.dir") +
"/images/piece1.png");
pieceIcon[1] = new ImageIcon(System.getProperty("user.dir") +
"/images/piece2.png");
this.add(boardPanel);
displayGUIboard();
}
public void displayGUIboard() {
boardPanel.add(new JLabel(pieceIcon[0]);
boardPanel.add(new JLabel(pieceIcon[0]);
}
As an aside: note that none of your variables should be static.
Edit: regarding your recent edit:
This works
boardLabels[0] = new JLabel(pieces[1]);
boardLabels[1] = new JLabel(pieces[1]);
when using ImageIcons, but I want to avoid this since to update the board I will have to remove then reload the JLabels. I would prefer to just update the already loaded labels."
Solution
No you don't have to change JLabels at all. Keep your JLabels where they are, but simply swap the icons that they hold using the JLabel setIcon(...)
method.
Edit
Also, don't confuse variables with objects. Even if you create a bunch of JLabel variables, if they all refer to the same JLabel object, you still can't add a JLabel object more than once to a container.
Edit You state:
The code is a part of the display function for a game. An array of integers will represent the board which is interpreted (but not in the above code) and the correct Jlabel images will be placed into a gridlayout panel to display the gui of the board. I have gotten the display code to work fine, but in my current version it removes the jlabels from the board then creates new JLabels(piece...)... but i would prefer it to update itself from the integer array rather than removing the labels, reading the array, then recreating the labels.
So create a JPanel that uses GridLayout and fill it with unchanging JLabels. Then simply change the icons held by the JLabels based on the values held by the int array. You could create a method that simplifies and automates this process.
Edit regarding:
edit I tried this before but it throws a null pointer exception.
Then solve this as you would any NPE. Find out which line throws the NPE, check the variables on the line, at least one is null, and then fix it so that you initialize the variable before trying to use it.
Edit
for example:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import javax.swing.*;
@SuppressWarnings("serial")
public class GridExample extends JPanel {
public static final int[][] MAP = {
{1, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2},
{1, 1, 0, 0, 2, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 0, 2, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 0, 0, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 1, 0, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 2},
{1, 1, 0, 0, 0, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 2},
{1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 2},
{1, 1, 1, 1, 1, 0, 0, 0, 2, 2, 2},
{1, 1, 1, 1, 1, 1, 0, 0, 0, 2, 2}
};
public static final Color[] COLORS = {};
private JLabel[][] labelGrid = new JLabel[MAP.length][MAP[0].length];
public GridExample() {
setLayout(new GridLayout(MAP.length, MAP[0].length));
for (int r = 0; r < labelGrid.length; r++) {
for (int c = 0; c < labelGrid[r].length; c++) {
labelGrid[r][c] = new JLabel();
labelGrid[r][c].setIcon(Ground.getGround(MAP[r][c]).getIcon());
add(labelGrid[r][c]);
}
}
}
private static void createAndShowGui() {
GridExample mainPanel = new GridExample();
JFrame frame = new JFrame("GridExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
enum Ground {
DIRT(0, new Color(205,133, 63)), GRASS(1, new Color(0, 107, 60)),
WATER(2, new Color(29, 172, 214));
private int value;
private Color color;
private Icon icon;
private Ground(int value, Color color) {
this.value = value;
this.color = color;
icon = createIcon(color);
}
private Icon createIcon(Color color) {
int width = 24; // how to use const in enum?
BufferedImage img = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.setColor(color);
g.fillRect(0, 0, width, width);
g.dispose();
return new ImageIcon(img);
}
public int getValue() {
return value;
}
public Color getColor() {
return color;
}
public Icon getIcon() {
return icon;
}
public static Ground getGround(int value) {
for (Ground ground : Ground.values()) {
if (ground.getValue() == value) {
return ground;
}
}
return null;
}
}
Which shows a GUI grid:
user.dir
is very fragile. Since these images are apparently inherent to the app., they should be added to the Jar as an embedded resource. – Rocha