Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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 <strong><em>ImageIcon</em></strong>. ImageIcons can be used more than once with ease:</p> <pre><code>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]); } </code></pre> <p>As an aside: note that <em>none</em> of your variables should be static.</p> <p><strong>Edit:</strong> regarding your recent edit:</p> <blockquote> <p>This works</p> </blockquote> <pre><code>boardLabels[0] = new JLabel(pieces[1]); boardLabels[1] = new JLabel(pieces[1]); </code></pre> <blockquote> <p>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."</p> </blockquote> <p><strong>Solution</strong><br> 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 <code>setIcon(...)</code> method.</p> <p><strong>Edit</strong><br> 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 <em>JLabel object</em> more than once to a container.</p> <p><strong>Edit</strong> You state: </p> <blockquote> <p>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.</p> </blockquote> <p>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.</p> <p><strong>Edit</strong> regarding:</p> <blockquote> <p>edit I tried this before but it throws a null pointer exception.</p> </blockquote> <p>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.</p> <p><strong>Edit</strong><br> for example:</p> <pre><code>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 &lt; labelGrid.length; r++) { for (int c = 0; c &lt; 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; } } </code></pre> <p>Which shows a GUI grid:<br> <img src="https://i.stack.imgur.com/gGNtj.jpg" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload