954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

2D Array of JLabels

Hi guys,

I'm trying to create a 2D array of JLabels my first problem is actually creating them.

my code atm is =

package test;

import java.io.File;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class testing extends javax.swing.JFrame {

    /** Creates new form testing */
    public testing() {
        initComponents();
        for(int i = 0; i < 10; i++)
        {
            for(int j = 0; j < 10; j++)
            {
                jPanel1.add(new javax.swing.JLabel("I:" + i + " J:" + j));
            }
        }
        this.repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 459, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 355, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 459, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 355, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new testing().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JPanel jPanel1;
    // End of variables declaration

}


this is not displaying anything.

also once i have got them working how will i access them? will there default name be jLabel1 ...etc

ajst
Junior Poster
121 posts since Nov 2010
Reputation Points: 30
Solved Threads: 3
 

Keep GroupLayout for GUI builder but if you still want to use them
the JLabel should not be added to the JPanel (using the JPanel add method) as you do but to the horizontalGroup and verticalGroup you created

http://download.oracle.com/javase/6/docs/api/javax/swing/GroupLayout.html

pbl
Junior Poster
121 posts since Dec 2010
Reputation Points: 32
Solved Threads: 20
 

So I would still have to create the labels i need but add them via the groups to allign them as a 2D array?

I might rephrase my question now then. whats the best way of displaying a chess board with out using images? because I was thinking of using a 2D array of Labels with boarder. but is that going to be to many lables on the screen?

ajst
Junior Poster
121 posts since Nov 2010
Reputation Points: 30
Solved Threads: 3
 

GridLayout - 64 JLabels is no problem whatsoever.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

but is it a sensible solution??

ajst
Junior Poster
121 posts since Nov 2010
Reputation Points: 30
Solved Threads: 3
 

Last time I wrote a chess program (yes, really) that's what I did and it worked very well - clear code and efficient running. I used ImageIcons in the JLables to show the pieces. I can share some bits of code if anyone needs convincing!
ps: Sorry - small correction - I just checked and it was actually a 8x8 array of JButtons - a bit easier to handle mouse clicks, that's all

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

thank you i thought i was silly of thinking of using 64-100 labels for my board but if its comman way for chess/board games ill do it.

ajst
Junior Poster
121 posts since Nov 2010
Reputation Points: 30
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: