Matrix in Forms

3

I'm having trouble creating an array within a JPanel in NetBeans, I've tried customizing the code by putting the code inside the NetBeans customization:

           jPanel1.setLayout(new GridLayout(15,15));
           JLabel[][] grid = new JLabel[15][15]; 
           for(int y=0; y<15; y++){
                   for(int x=0; x<15; x++){
                          grid[x][y]=new JLabel("("+x+","+y+")");    
                          jPanel1.add(grid[x][y]);
                   }
           }

    
asked by anonymous 11.11.2014 / 17:47

1 answer

4

As you said that the printa nothing program does not seem to be a problem with the Layout Manager choice, you should be missing your JPanel to your JFrame, like this:

setContentPane(jPanel1);

However, it is my suggestion that whenever you are in doubt about which LayoutManager to use, go from MigLayout, because it is more modern than the others, more flexible and that generates less code, making it easier for you to tinker with code lines instead just drag the components. Source: MigLayout - Java Layout Manager

Using MigLayout instead of GridLayout your code looks like this:

jPanel1 = new JPanel();
jPanel1.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(jPanel1);
jPanel1.setLayout(new MigLayout("", "[][]", "[]"));
JLabel[][] grid = new JLabel[15][15];
for(int y=0; y<15; y++){
    for(int x=0; x<15; x++){
        grid[x][y]=new JLabel("("+x+","+y+")");    
        jPanel1.add(grid[x][y], "cell " + x  + " " + y);
    }
}

Result:

    
11.11.2014 / 17:55