JButton Arrays ActionEvent problem, please help

I'm trying to create a simplified version of Suduko using a few arrays of JButtons, one dimensional and two dimensional. Finally got everything to compile and managed to overcome the NullPointerException, but now the buttons simply don't do anything. JGrasp doesn't report any errors. I suspect the ActionListener isn't detecting what Button is being pressed or something.

Anyone know how to solve this?

Here's the code, I hope it comes out nice in the forum...

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass Sudukoextends JFrame

{

private Container pane;

private JButton[][] gameB;

private JButton[] numberB;

private JButton resetB, solveB;

private SolveButtonHandler sbHandler;

private ResetButtonHandler rbHandler;

private GameButtonHandler gbHandler;

private NumberButtonHandler nbHandler;

private JLabel[] blankL;

private String storedNum;

private String[][] solvedPuzzle ={{"1","5","8","7","9","3","6","4","2"},

{"3","9","2","4","6","5","8","1","7"},

{"7","6","4","8","2","1","9","3","5"},

{"6","8","7","3","5","4","2","9","1"},

{"5","1","9","2","7","6","3","8","4"},

{"2","4","3","1","8","9","7","5","6"},

{"9","7","6","5","1","8","4","2","3"},

{"8","3","1","6","4","2","5","7","9"},

{"4","2","5","9","3","7","1","6","8"}};

privatestaticfinalint WIDTH = 650;

privatestaticfinalint HEIGHT = 650;

public Suduko()

{

pane = getContentPane();

//got part of the idea for this part of the code from

//http://www.pembinatrails.ca/fortrichmondcollegiate/courses/compsci/cprsci30s/new/java/arrays_of_objects.htm

//Couldn't figure out how to get what I had to work

gameB =new JButton[9][9];

for(int i = 0; i < 9; i++)

for(int j = 0; j < 9; j++)

{

gameB[i][j] =new JButton("?");

pane.add(gameB[i][j]);

gbHandler =new GameButtonHandler();

gameB[i][j].addActionListener(gbHandler);

}

gameB[0][0].setText("1");

gameB[0][0].setEnabled(false);

gameB[0][1].setText("5");

gameB[0][1].setEnabled(false);

gameB[0][4].setText("9");

gameB[0][4].setEnabled(false);

gameB[0][6].setText("6");

gameB[0][6].setEnabled(false);

gameB[1][3].setText("4");

gameB[1][3].setEnabled(false);

gameB[2][0].setText("7");

gameB[2][0].setEnabled(false);

gameB[2][4].setText("2");

gameB[2][4].setEnabled(false);

gameB[2][5].setText("1");

gameB[2][5].setEnabled(false);

gameB[3][0].setText("6");

gameB[3][0].setEnabled(false);

gameB[3][1].setText("8");

gameB[3][1].setEnabled(false);

gameB[4][0].setText("5");

gameB[4][0].setEnabled(false);

gameB[4][2].setText("9");

gameB[4][2].setEnabled(false);

gameB[4][4].setText("7");

gameB[4][4].setEnabled(false);

gameB[4][5].setText("6");

gameB[4][5].setEnabled(false);

gameB[4][6].setText("3");

gameB[4][6].setEnabled(false);

gameB[5][2].setText("3");

gameB[5][2].setEnabled(false);

gameB[5][4].setText("8");

gameB[5][4].setEnabled(false);

gameB[5][5].setText("9");

gameB[5][5].setEnabled(false);

gameB[6][3].setText("5");

gameB[6][3].setEnabled(false);

gameB[7][1].setText("3");

gameB[7][1].setEnabled(false);

gameB[7][2].setText("1");

gameB[7][2].setEnabled(false);

gameB[7][7].setText("7");

gameB[7][7].setEnabled(false);

gameB[7][8].setText("9");

gameB[7][7].setEnabled(false);

gameB[8][0].setText("4");

gameB[8][0].setEnabled(false);

gameB[8][1].setText("2");

gameB[8][1].setEnabled(false);

gameB[8][8].setText("8");

gameB[8][8].setEnabled(false);

for(int i = 0; i < 3; i++)

for(int j = 0; j < 3; j++)

{

gameB[i][j].setBackground(Color.blue);

}

for(int i = 6; i < 9; i++)

for(int j = 0; j < 3; j++)

{

gameB[i][j].setBackground(Color.blue);

}

for(int i = 3; i < 6; i++)

for(int j = 3; j < 6; j++)

{

gameB[i][j].setBackground(Color.blue);

}

for(int i = 0; i < 3; i++)

for(int j = 6; j < 9; j++)

{

gameB[i][j].setBackground(Color.blue);

}

for(int i = 6; i < 9; i++)

for(int j = 6; j < 9; j++)

{

gameB[i][j].setBackground(Color.blue);

}

JLabel[] blankL =new JLabel[9];

for(int i = 0; i < 9; i++)

{

blankL[i] =new JLabel("");

pane.add(blankL[i]);

}

numberB =new JButton[9];

for(int i = 0; i < 9; i++)

{

numberB[i] =new JButton(String.valueOf(i+1));

pane.add(numberB[i]);

nbHandler =new NumberButtonHandler();

numberB[i].addActionListener(nbHandler);

}

resetB =new JButton("Reset");

rbHandler =new ResetButtonHandler();

resetB.addActionListener(rbHandler);

solveB =new JButton("Solve");

sbHandler =new SolveButtonHandler();

solveB.addActionListener(sbHandler);

setTitle("Suduko");

pane.setLayout(new GridLayout(12, 9));

pane.add(resetB);

pane.add(solveB);

setSize(WIDTH, HEIGHT);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

privateclass GameButtonHandlerimplements ActionListener

{

publicvoid actionPerformed(ActionEvent e)

{

for(int i = 0; i < 9; i++)

for(int j = 0; j < 0; j++)

{

if(gameB[i][j].isSelected())

{

gameB[i][j].setText(storedNum);

if(gameB[i][j].getText() == solvedPuzzle[i][j])

{

gameB[i][j].setEnabled(false);

}

}

}

}

}

privateclass NumberButtonHandlerimplements ActionListener

{

publicvoid actionPerformed(ActionEvent e)

{

for(int i = 0; i < 9; i++)

{

if(numberB[i].isSelected())

{

storedNum = numberB[i].getText();

System.out.print(storedNum);

}

}

}

}

privateclass SolveButtonHandlerimplements ActionListener

{

publicvoid actionPerformed(ActionEvent e)

{

for(int i = 0; i < 9; i++)

for(int j = 0; j < 0; j++)

{

storedNum.equals(solvedPuzzle[i][j]);

gameB[i][j].setText(storedNum);

gameB[i][j].setEnabled(false);

}

}

}

privateclass ResetButtonHandlerimplements ActionListener

{

publicvoid actionPerformed(ActionEvent e)

{

for(int i = 0; i < 9; i++)

for(int j = 0; j < 9; j++)

{

gameB[i][j].setText("?");

gameB[i][j].setEnabled(true);

}

gameB[0][0].setText("1");

gameB[0][0].setEnabled(false);

gameB[0][1].setText("5");

gameB[0][1].setEnabled(false);

gameB[0][4].setText("9");

gameB[0][4].setEnabled(false);

gameB[0][6].setText("6");

gameB[0][6].setEnabled(false);

gameB[1][3].setText("4");

gameB[1][3].setEnabled(false);

gameB[2][0].setText("7");

gameB[2][0].setEnabled(false);

gameB[2][4].setText("2");

gameB[2][4].setEnabled(false);

gameB[2][5].setText("1");

gameB[2][5].setEnabled(false);

gameB[3][0].setText("6");

gameB[3][0].setEnabled(false);

gameB[3][1].setText("8");

gameB[3][1].setEnabled(false);

gameB[4][0].setText("5");

gameB[4][0].setEnabled(false);

gameB[4][2].setText("9");

gameB[4][2].setEnabled(false);

gameB[4][4].setText("7");

gameB[4][4].setEnabled(false);

gameB[4][5].setText("6");

gameB[4][5].setEnabled(false);

gameB[4][6].setText("3");

gameB[4][6].setEnabled(false);

gameB[5][2].setText("3");

gameB[5][2].setEnabled(false);

gameB[5][4].setText("8");

gameB[5][4].setEnabled(false);

gameB[5][5].setText("9");

gameB[5][5].setEnabled(false);

gameB[6][3].setText("5");

gameB[6][3].setEnabled(false);

gameB[7][1].setText("3");

gameB[7][1].setEnabled(false);

gameB[7][2].setText("1");

gameB[7][2].setEnabled(false);

gameB[7][7].setText("7");

gameB[7][7].setEnabled(false);

gameB[7][8].setText("9");

gameB[7][7].setEnabled(false);

gameB[8][0].setText("4");

gameB[8][0].setEnabled(false);

gameB[8][1].setText("2");

gameB[8][1].setEnabled(false);

gameB[8][8].setText("8");

gameB[8][8].setEnabled(false);

}

}

publicstaticvoid main(String[] args)

{

Suduko sudukoObject =new Suduko();

}

}

[19682 byte] By [Shmraa] at [2007-10-2 9:47:38]
# 1

I've discovered the problems with it myself.

Accidently had it set to j < 0 in the loop, used the getSource method, and for some reason I had to move the statements outside the loop so it now looks like this...

private class GameButtonHandler implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

int ROW = 0;

int COL = 0;

JButton pressedB;

//loop through the buttons until selected one is found and set the stored text

pressedB = (JButton) e.getSource();

for(int i = 0; i < 9; i++)

{//really need to keep an eye on that j < #, spent a few hours trying to discover

//my error only to see that I had that set to j < 0 by mistake

for(int j = 0; j < 9; j++)

{

if(gameB[i][j] == pressedB)

{

ROW = i;

COL = j;

}

}

}

//got the idea to bring these outside the loop from people.uis.edu/mlovi1/SwingLecture2.ppt

gameB[ROW][COL].setText(storedNum);

//if it matches the text in the solved puzzle, disable the button

if(gameB[ROW][COL].getText().equals(solvedPuzzle[ROW][COL]))

{

gameB[ROW][COL].setEnabled(false);

}

}

}

private class NumberButtonHandler implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

int ROW = 0;

JButton pressedB;

//loop through the buttons to find the selected button and get the text from it

pressedB = (JButton) e.getSource();

for(int i = 0; i < 9; i++)

{

if(numberB[i] == pressedB)

{

ROW = i;

}

}

storedNum = numberB[ROW].getText();

System.out.print(storedNum);

}

}

Thanks to anyone who may have been trying to solve this issue for me, and to whoever made the power point which I have linked in my code, which helped me to resolve this issue.

Shmraa at 2007-7-16 23:52:58 > top of Java-index,Security,Event Handling...