java - Referencing a contentPane in an actionPerformed method -


i'm creating load button , want populate 9x9 gridlayout content pane nested inside center panel of border layout (main window). method located inside page_start section of border layout. question how can place buttons in grid layout of center section here?

       //load button     jbutton load = new jbutton("load");     load.addactionlistener(new actionlistener() {          @override         public void actionperformed (actionevent a) {             //dialog box locate 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 model , draw                     try {                         gridview = new jbutton[9][9];                         int[][] viewarray = puzzle.getarray();                          (int row = 0; row < 9; row++) {                             (int col = 0; col < 9; col++) {                                 gridview[row][col] = new jbutton(string.valueof(viewarray[row][col]));   ***************the next line represents problem***************                                 this.getcontentpane().board.add(gridview[row][col]);                              }                         } 

this in constructor

     jpanel board = new jpanel();     board.setlayout (new gridlayout (9,9));     board.setpreferredsize(new dimension(400,400));     this.getcontentpane().add(board, borderlayout.center); 

this

this.getcontentpane().board.add(gridview[row][col]); 

does not make sense. added board content pane in constructor, not mean board field of content pane.

getcontentpane().board 

should fail compile.

you should make board field of class, rather declaring local variable in constructor. able refer board throughout body of class.

basic example:

public class example {     private jpanel board;      public example()     {          board = new jpanel();          getcontentpane().add(board);          //assuming above code takes place in constructor          jbutton load = new jbutton("load");          load.addactionlistener(new actionlistener()          {               public void actionperformed(actionevent e)               {                     ...lots of code                     //as board no longer local variable compile                     board.add(gridview[row][col]);               }          }     } } 

note want add load frame @ point.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -