3

I'm trying to make a solved sudoku puzzle show up in a window with 81 boxes. I did this:

import java.awt.GridLayout;
import java.awt.*;

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


public class GraphicSolver extends JFrame {

GraphicSolver(int[][] spelplan) {

    Panel panel = new Panel(new GridLayout(9,9));

    for(int i=9;i<9;i++){
        for(int x=0;x<9;x++){
            panel.add(new JLabel(""+spelplan[i][x]));
        }
    }

    Frame frame = new Frame();
    frame.add(panel);


    frame.setVisible(true);

}
}

However, it only gives me an empty window without any numbers. I'd be pleased if someone could point me in the right direction.

Ashkan Aryan
  • 3,504
  • 4
  • 30
  • 44
Filip Hedman
  • 407
  • 2
  • 12

4 Answers4

7

The outer loop should start at zero:

for(int i=0;i<9;i++){
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Oh, simple stupid mistake, thanks for pointing it out! I accept this as the answer, as it was the main thing that broke the program. – Filip Hedman Sep 22 '11 at 10:31
  • Glad to help; also consider up-voting related answers you found helpful. – trashgod Sep 22 '11 at 10:54
  • See also [`CellTest`](http://stackoverflow.com/questions/4148336/jformattedtextfield-is-not-properly-cleared/4151403#4151403). – trashgod Sep 22 '11 at 17:00
4

Try calling frame.pack (), this will pack all the components into the frame to be displayed after computing the correct size with the panels. Also, follow the fix suggested by @trashgod above will solve the fact that no panels were added, and the fix by @Ashkan Aryan will make your code a bit more reasonable (although it should work without it, but then there is no point in inheriting from JFrame).

The code below works for me:

GraphicSolver(int[][] spelplan) {
    Panel panel = new Panel(new GridLayout(9,9));

    for(int i=0;i<9;i++){
        for(int x=0;x<9;x++){
            panel.add(new JLabel(""+spelplan[i][x]));
        }
    }

    this.add(panel);
    this.pack ();
    this.setVisible(true);
}
Barak Itkin
  • 4,872
  • 1
  • 22
  • 29
4

You seem to have two Frames. 1 is the JFrame (the class GrpahicSolver itself) and the other a frame that you are creating within it.

I suggest you replace frame.addPanel() with this.addPanel() and it should work.

Ashkan Aryan
  • 3,504
  • 4
  • 30
  • 44
4

Graphic Solver

import java.awt.GridLayout;
import javax.swing.*;

public class GraphicSolver {

    GraphicSolver(int[][] spelplan) {
        // presumes each array 'row' is the same length
        JPanel panel = new JPanel(new GridLayout(
            spelplan.length,
            spelplan[0].length,
            8,
            4));

        for(int i=0;i<spelplan.length;i++){
            for(int x=0;x<spelplan[i].length;x++){
                panel.add(new JLabel(""+spelplan[i][x]));
            }
        }

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.pack();

        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                int[][] plan = new int[4][7];
                for (int x=0; x<plan.length; x++) {
                    for (int y=0; y<plan[x].length; y++) {
                        plan[x][y] = (x*10)+y;
                    }
                }
                new GraphicSolver(plan);
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433