I am trying to load the same jlabel stored image twice into a gridlayout panel, however instead of creating two instances of the image, the image is only displayed once then moved.
How can I store the same JLabel position in the pieces array into more than one JLabel in the boardLabels array.
Thanks :)
public static JPanel boardPanel = new JPanel(new GridLayout(4, 0));
public static JLabel pieces[] = new JLabel[2];
private static JLabel[] boardLabels = new JLabel[4];
public MainFrame() {
pieces[0] = new JLabel(new ImageIcon(System.getProperty("user.dir") + "/images/piece1.png"));
pieces[1] = new JLabel(new ImageIcon(System.getProperty("user.dir") + "/images/piece2.png"));
this.add(boardPanel);
displayGUIboard();
}
public static void displayGUIboard() {
//ERROR - the label in pieces[0] is not copied into both boardLabels [0] and [1]
boardLabels[0] = pieces[0];
boardLabels[1] = pieces[0];
boardPanel.add(boardLabels[0]);
boardPanel.add(boardLabels[1]);
}
public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.setVisible(true);
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
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.
edit I tried this before but it throws a null pointer exception...
boardLabels[0].setIcon(pieces[1]);
boardLabels[1].setIcon(pieces[1]);
boardPanel.add(boardLabels[0]);
boardPanel.add(boardLabels[1]);
For comparison, I've re-factored @HFOE's example so that
Ground implements Icon
and indexes the array returned byvalues()
. Asvalue
is an implementation detail,int[][] MAP
could instead beGround[][] MAP
.Update: This variation illustrates
Ground[][] MAP
and addsTexturePaint
.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:
As an aside: note that none of your variables should be static.
Edit: regarding your recent edit:
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:
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:
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:
Which shows a GUI grid: