Search code examples
javaswingjlabel

Displaying JLabel matrix


Can somebody tell me why after calling method getContentPane().add(grid[i][j]) I am not able to display the matrix of JLabels. There's only one "e" label displayed.

public class SudokuFrame extends JFrame implements ActionListener {

JButton generateButton;
JLabel[][] grid;

public SudokuFrame(){
    setSize(300, 300);
    setTitle("Sudoku");
    setLayout(null);
    generateButton = new JButton("Generate");
    generateButton.setBounds(90, 220, 100, 30);
    add(generateButton);
    generateButton.addActionListener(this);

    grid = new JLabel[9][9];
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            grid[i][j] = new JLabel("e");
            grid[i][j].setBounds(100, 100, 30, 30);
            getContentPane().add(grid[i][j]);
        }
    }
}

public static void main(String[] args){
    SudokuFrame frame = new SudokuFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

Solution

  • You're giving every JLabel the exact same bounds -- same size and same position and so every new label is placed right smack dab on top of the previously added ones.

    Solution: don't use null layout. Why use this when the problem is perfectly suited for a GridLayout? In general you want to avoid using null layouts and setBounds as the layout managers will make your coding and your GUI much easier to manage. Let the layouts do the heavy lifting for you.

    e.g.,

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.GridLayout;
    import javax.swing.*;
    
    public class SimpleSudoku extends JPanel {
        private static final int GAP = 1;
        private static final Font LABEL_FONT = new Font(Font.DIALOG, Font.PLAIN, 24);
        private JLabel[][] grid = new JLabel[9][9];
    
        public SimpleSudoku() {
            JPanel sudokuPanel = new JPanel(new GridLayout(9, 9, GAP, GAP));
            sudokuPanel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
            sudokuPanel.setBackground(Color.BLACK);
            for (int row = 0; row < grid.length; row++) {
                for (int col = 0; col < grid[row].length; col++) {
                    grid[row][col] = new JLabel("     ", SwingConstants.CENTER);
                    grid[row][col].setFont(LABEL_FONT); // make it big
                    grid[row][col].setOpaque(true);
                    grid[row][col].setBackground(Color.WHITE);
                    sudokuPanel.add(grid[row][col]);
                }
            }
    
            JPanel bottomPanel = new JPanel();
            bottomPanel.add(new JButton("Regenerate"));
    
            setLayout(new BorderLayout());
            add(sudokuPanel, BorderLayout.CENTER);
            add(bottomPanel, BorderLayout.PAGE_END);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                createAndShowGui();
            });
        }
    
        private static void createAndShowGui() {
            SimpleSudoku mainPanel = new SimpleSudoku();
            JFrame frame = new JFrame("SimpleSudoku");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.add(mainPanel);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    }