I m doing Program for sudoku. The blank grid displayed but how to pass random integer values to that grid. here is my code:

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.JFormattedTextField;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

@SuppressWarnings("empty-statement")
public class Board 
{
  
    private static final Dimension FIELD_SIZE = new Dimension(60, 60);
	private static final int EMPTY = 0;
	private JPanel grid = new JPanel();
	private JPanel[][] subgrid = new JPanel[3][3];
	private JFormattedTextField[][] cell = new JFormattedTextField[9][9];
	private static int num=0,col=9,row=9;
    private int Value=0;
    private String strValue;

    private int GridBoard[][] = {{0, 6, 0, 1, 0, 4, 0, 5, 0},
                          {0, 0, 8, 3, 0, 5, 6, 0, 0},
                          {2, 0, 0, 0, 0, 0, 0, 0, 1},
                          {0, 0, 6, 0, 0, 0, 3, 0, 0},
                          {7, 0, 0, 9, 0, 1, 0, 0, 4},
                          {5, 0, 0, 0, 0, 0, 0, 0, 2},
                          {0, 0, 7, 2, 0, 6, 9, 0, 0},
                          {0, 4, 0, 5, 0, 8, 0, 7, 0}};
    int GridArray[][] = null;

	public Board()
	{
	  grid.setBorder(BorderFactory.createLineBorder(Color.black, 3));
	  grid.setLayout(new GridLayout(3, 3));
    
	  for (int i = 0; i < subgrid.length; i++)
           {
                     for (int j = 0; j < subgrid[i].length; j++)
		 {
			 subgrid[i][j] = new JPanel(new GridLayout(3, 3));
			 subgrid[i][j].setBorder(BorderFactory.createLineBorder(Color.black, 1));
			 grid.add(subgrid[i][j]);
		 }
       }

	  for (int i = 0; i < cell.length; i++)
	  {
		for (int j = 0; j < cell[i].length; j++)
		{
		   cell[i][j] = new JFormattedTextField(new DefaultFormatterFactory(new NumberFormatter()));
           cell[i][j].setValue(new Integer(Value));
          
           cell[i][j].setPreferredSize(FIELD_SIZE);
	       cell[i][j].setFont( new Font("sans-serif", Font.BOLD, 20));
		   cell[i][j].setHorizontalAlignment(JFormattedTextField.CENTER);
		   subgrid[i/3][j/3].add(cell[i][j]);
	    }
	  }
	}
    
    Board(int[][] result)
    {
       this.grid =grid;
    }
    public JComponent getPanel() //return type of getPanel() is JComponent
	{
	  return grid;
	}

    public void setCellValue(int num, int row, int col)
	 {
        num=0;
		for (int r = 1;r<=9 ;r++ )
		  {
			for (int c = 1;c<=9 ; c++)
			{
				cell[r][c] = new JFormattedTextField();
                strValue=GridBoard.toString().replace("0", " ");
              
				String text = (num == EMPTY) ? " " : String.valueOf(num);
	            cell [r][c].setText(text);
               
		    }
          }
    }

   public int getCellValue(int row, int col)
    {
       int cellval=0;
       try
        {
          for (int r = 0;r<cell.length ;r++ )
		  {
			for (int c = 0;c<cell[r].length ; c++)
			{
                cellval = Integer.parseInt(cell[r][c].getText());   
			}
		  }
        }
        catch (NumberFormatException e)
        {
                cellval = EMPTY;
        }
        return cellval;
  }

 public void clearBoard()
 {
       for (int r = 0; r < 9 ; r++ )
		{
			for (int c = 0; c < 9 ; c++)
			{
                             cell[r][c] = new JFormattedTextField();
			}
       }
 }

 public static boolean isValid(boolean[] choices){
    for (int i = 1; i <= 9; i++)
    {
      if (choices[i]) return true;
    }
    return false;
  }


 protected boolean checkRow( int num )
   {
       // loop through row and check values
       for (int i = 0; i < cell[row-1].length; i++)
        {
           if(num == Integer.parseInt(cell[row-1][i].getText()));
           {
               return true;
           }
        }
       return false;
 }

 protected boolean checkCol( int num )
   {
       // loop through row and check values
       for (int i = 0; i < cell[col-1].length; i++)
        {
           if(num == Integer.parseInt(cell[col-1][i].getText()))
           {
               return true;
           }
        }
       return false;
   }

  protected boolean inBox(int number)
  {
     for(int i = 0; i < subgrid.length; i++)
      {
        for(int j = 0 ; j < subgrid[i].length; j++)
         {
           if(number == Integer.parseInt(cell[i][j].getText()))
              return true;
         }
       }
       return false;
  }

 private static void createAndShowGUI()
  {
	  JFrame frame = new JFrame("Sudoku");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new Board().getPanel());
      frame.pack();
	  frame.setLocationRelativeTo(null);
	  frame.setVisible(true);
  }
      

 public static void main(String[] args)
  {
    Board board= new Board();
    javax.swing.SwingUtilities.invokeLater(new Runnable()
     {
       public void run()
       {
           createAndShowGUI();
       }
     });
  }
}

Thanks for suggestion. I done that but random number does not displayed in the grid. can you give source code for that.

hello
can anybody give me idea of how to validate numbers in sudoku and how to apply color for that particular no and cell

This question has already been answered. Start a new discussion instead.