Hi all i need to update a containers data for a sudoku game i am creating but am struggling to get the field to update with the new data.the container uses a grid layout and each cell contains a button with the appropriate number for sudoku in it and i need to know how to update the text in these buttons. any help with what methods i could use or any help in general would be greatly appreciated. please see the code i currently have to try and update below i know its probably messy and completely on the wrong track but ive just been trying to mess with stuff hoping it would work.
public void update(int[][] grid2)throws NullPointerException{
myGrid = new Container();
try{
for(int i = 0; i<9; i++){
for(int j = 0; j<9; j++){
String appropriateNumber = convertSimple(grid2[i][j]);
JButton button = new JButton(appropriateNumber);
button.addActionListener(new sudokuListener());
myGrid.add(button);
}
}
}
catch(NullPointerException e){
}
myGrid.setLayout(new GridLayout(9, 9));
myGrid.setPreferredSize (new Dimension(400, 400));
add(myGrid);
puzzleGUI.update();
}
puzzlGUI.update simply contains puzzle.validate() (puzzle being the GUI)
[edit]
ok i have changed things around a bit and used the JButton arrya as suggested and i have been able to set the text of the buttons in this array (i know through a system.out.print) but this does not update the text which is in the actual GUI. aaaaahhh coding is not fun at the end of a semester please help anyone
To re-iterate, if you have a grid of JButtons, why re-create them every time? Why not simply iterate through the components that are already in the grid and update their state, such as perhaps the text showing on the JButtons?