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

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?

GridLayout - 64 JLabels is no problem whatsoever.

but is it a sensible solution??

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.