Search code examples
javaswingjpanelgrid-layoutborder-layout

Referencing a contentPane in an actionPerformed method


I'm creating a load button and want it to populate a 9x9 gridlayout content pane nested inside the CENTER panel of a border layout (main window). This method is located inside the PAGE_START section of the border layout. The question is how can I place the buttons in the Grid Layout of the CENTER section from here?

       //Load Button
    JButton load = new JButton("Load");
    load.addActionListener(new ActionListener() { 
        @Override
        public void actionPerformed (ActionEvent a) {
            //Dialog Box To Locate The Puzzle
                SudokuPuzzle puzzle;
                JFileChooser chooser = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "Text Files", "txt");
                chooser.setFileFilter(filter);
                int returnVal = chooser.showOpenDialog(SudukoGUI.this);
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    String fileLocation = chooser.getSelectedFile().getName();
                    Scanner file = new Scanner(fileLocation);
                    puzzle = new SudokuPuzzle(file, file);

                    //Load Puzzle To Model and draw it
                    try {
                        gridView = new JButton[9][9];
                        int[][] viewArray = puzzle.getArray();

                        for (int row = 0; row < 9; row++) {
                            for (int col = 0; col < 9; col++) {
                                gridView[row][col] = new JButton(String.valueOf(viewArray[row][col])); 

***************THE NEXT LINE REPRESENTS MY PROBLEM***************
                                this.getContentPane().board.add(gridView[row][col]);

                            }
                        }

This is in the constructor

     JPanel board = new JPanel();
    board.setLayout (new GridLayout (9,9));
    board.setPreferredSize(new Dimension(400,400));
    this.getContentPane().add(board, BorderLayout.CENTER);

Solution

  • This

    this.getContentPane().board.add(gridView[row][col]);
    

    Does not make any sense. You added board to the content pane in your constructor, but this does not mean that board is now a field of the content pane. So

    getContentPane().board
    

    Should fail to compile.

    You should probably make board a field of your class, rather than declaring it a local variable in your constructor. Then you will able to refer to board throughout the body of your class.

    Basic example:

    public class Example
    {
        private JPanel board;
    
        public Example()
        {
             board = new JPanel();
             getContentPane().add(board);
             //assuming above code takes place in the constructor
             JButton load = new JButton("load");
             load.addActionListener(new ActionListener()
             {
                  public void actionPerformed(ActionEvent e)
                  {
                        ...lots of code
                        //as board is no longer a local variable this will compile
                        board.add(gridView[row][col]);
                  }
             }
        }
    }
    

    Note that you probably want to add load to your frame at some point.