0

I'm trying to dsiplay a 9x9 Grid from a text file, the 2D array and toString(); come out fine but I have no idea why my Grid isn't even popping up. I even tried calling a very simple event button from another class to see if it will work and I still get nothing ( I am calling it from the main method). However when I run this simple event button in the other class it works fine, I have no idea why it does not work.

    import static org.junit.Assert.assertEquals;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.PrintWriter;
    import java.util.InputMismatchException;
    import java.util.Scanner;
    import javax.swing.JFileChooser;
    import javax.swing.JTextField;

    import java.applet.Applet;
    import java.awt.GridLayout;

    public class SudokuBrdManager extends Applet implements SudokuBoardManager 
    {

    private static SudokuBrdManager myBoard;
    private static ButtonGrid button;
    private int [][] Board= new int[9][9];
    private String output;


    public static void main(String[] args)
    {
        myBoard = new SudokuBrdManager();
            try {
                //myBoard.setBoard();
            } catch (InputOutOfRangeException e) {
                 TODO Auto-generated catch block
                //e.printStackTrace();
            } catch (ValueNotValidException e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
            }    
        //System.out.println(myBoard.toString());   
    }

    public void setBoard () throws InputOutOfRangeException, ValueNotValidException
    {           
        JFileChooser chooser = new JFileChooser();
        int status;

        chooser.setDialogTitle("Select Sudoku Game File");
        status = chooser.showOpenDialog(null);

        if(status == JFileChooser.APPROVE_OPTION)
        {
            try
            {
                File inFile = chooser.getSelectedFile();
                myBoard.newGame(inFile);    
            }
            catch(InputMismatchException e)
            {
                e.printStackTrace();
            } catch (NumberFormatException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        Scanner scanner = new Scanner(myBoard.toString()).useDelimiter(",|\r\n");
        for (int i=0; i < 9; i++) 
        {
               for (int j=0; j < 9; j++) 
               {
                   myBoard.setValueAt(i, j, scanner.nextInt());
                   add(new JTextField(String.valueOf(Board[i][j])));
               }
        }
    }        

    }
    @Override
    public void setValueAt(int r, int c, int v) throws InputOutOfRangeException, ValueNotValidException 
    {
        Board[r][c] = v;
    }

    @Override
    public int getValueAt(int r, int c) throws InputOutOfRangeException 
    {
        return 0;
    }

    @Override
    public int[] displayPossibleValues(int r, int c)throws InputOutOfRangeException 
    {
        return null;
    }

    public String toString()
    {
            return output;
    }

    @Override
    public void newGame(File gameFile) 
    {
        {
            try
            {
                output = new Scanner(gameFile).useDelimiter("\\Z").next();
            }
                 catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                 }
            }
    }    
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 3
    Probably I'm really inattentive, but the only part of GUI I see in this code is the FileChooser. – svz Sep 16 '12 at 07:16
  • `//e.printStackTrace();` Not helpful with broken code! Change that to `e.printStackTrace(); // report helpful advice` – Andrew Thompson Sep 16 '12 at 07:50
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Sep 16 '12 at 07:57
  • Se also [`CellTest`](http://stackoverflow.com/a/4151403/230513). – trashgod Sep 16 '12 at 13:56
  • Well for now I am just trying to display the grid, the gui is add(new JTextField(String.valueOf(Board[i][j]))); I just trying to display at the moment a 9x9 grid of text boxes. I also changed back e.printStackTrace(); I even just try to display a single text box but with no pop up applet occurs – user1672282 Sep 16 '12 at 14:15
  • You are subclassing Applet here, so are you trying to view this in a web page? What does your HTML look like? If not, you need a frame to hold the applet/panel.I also don't see in this code where you ever make the call to the method **setBoard** that would be creating the text fields. Also, as a side note, you never appear to change the layout manager to **GridLayout** though you import it so the default will be a **FlowLayout** and all fields will be in a single line. – Jere Sep 16 '12 at 14:51

1 Answers1

2

Because static void main() doesn't work for Applets, they have different entry points and life cycle than standart application. Move initialization logic to void init().

Refer to http://docs.oracle.com/javase/6/docs/api/java/applet/Applet.html and https://www.google.com/search?q=java+applet+lifecycle

Also, you're mixing AWT and Swing here. Should use JApplet instead of Applet I think.

user1410657
  • 1,284
  • 9
  • 5