adding char grid to jPanel

1.1k views Asked by At

I have two classes, one where the grid is set and the other where a GUI is made. I want the grid from the first class to be inserted into jPanel in the second class. The first grid is a char grid and is filled with numbers that are randomly initiated. I am having trouble working out how to insert the grid into the jpanel as this is more complicated than expected. I have tried many things but havent had any luck..Any help?

//set grid class

    public void setgrid() {
            Random ran = new Random();      
            for (int i = 0; i < X; i++) {
                for (int j = 0; j < Y; j++) {
                    int num = ran.nextInt(10); 
                    if (num == 4) { 
                        grid[i][j] = 'F'; 
                    } else if (num == 9) { 
                        grid[i][j] = 'O'; 
                    } else {
                        grid[i][j] = ' '; 
                    }}}


// Panel Class
    Panel = new JPanel(new GridLayout(X, Y));
            for (int i = 0; i < X; i++) {
                for (int j = 0; j < Y; j++) {
                    world[i][j] = new JPanel();
                    world[i][j].setBackground(Color.white);
                    world[i][j].setBorder(BorderFactory
                            .createLineBorder(Color.black));
                    Panel.add(world[i][j]);

NOT SURE WHAT TO ADD HERE TO ADD THE GRID INTO THE PANEL

1

There are 1 answers

0
Paul Samsotha On BEST ANSWER

"I am having trouble working out how to insert the grid into the jpanel as this is more complicated than expected. I have tried many things but havent had any luck..Any help?"

Not sure what exactly you're trying to or what you're doing wrong, but one thing I can suggest is to use JLabels. They take text as parameters, which show the text in the label. JPanels on the other hand, need to be drawn on (meaning you need to actually draw the text yourself).

Test out this program, to see what I'm talking about

enter image description here

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class DrawGrid {

    public DrawGrid() {
        int[][] grid = getGrid();

        JPanel panel = new JPanel(new GridLayout(10, 10));
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                panel.add(new JLabel(String.valueOf(grid[i][j])));
            }
        }

        JFrame frame = new JFrame();
        frame.add(panel);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);


    }

    public int[][] getGrid() {
        int[][] grid = new int[10][10];
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                grid[i][j] = i;
            }
        }
        return grid;

    }
    public static void main(String[] args) {
        new DrawGrid();

    }

}

Here's the code where I add the JLabels to the JPanel

    JPanel panel = new JPanel(new GridLayout(10, 10));
    for (int i = 0; i < grid.length; i++) {
        for (int j = 0; j < grid[i].length; j++) {
            panel.add(new JLabel(String.valueOf(grid[i][j])));
        }
    }

The same way you add borders and background to the JPanel, you can also do with JLabel