Note that there are some explanatory texts on larger screens.

plurals
  1. POJLabel images array
    primarykey
    data
    text
    <p>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.</p> <p>How can I store the same JLabel position in the pieces array into more than one JLabel in the boardLabels array.</p> <p>Thanks :)</p> <pre><code>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); } </code></pre> <p>This works </p> <pre><code> boardLabels[0] = new JLabel(pieces[1]); boardLabels[1] = new JLabel(pieces[1]); </code></pre> <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> <p><em><strong>edit</em></strong> I tried this before but it throws a null pointer exception...</p> <pre><code> boardLabels[0].setIcon(pieces[1]); boardLabels[1].setIcon(pieces[1]); boardPanel.add(boardLabels[0]); boardPanel.add(boardLabels[1]); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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