im currenty wokring on a program that read a txt inlcuding sudoku puzzle, and then the programs solves it and show all the solutions. And im almost done, but ran into a prob. I have a next button that is suppose to, when clicked, update the button text and the update/get the next solution(the program saves every solution).Here is my code:
class Gui extends JFrame{
int cnt = 1;
String[][] solution;
int nrRows, nrColumns;
SudokuContainer sc;
Gui(SudokuContainer sc, int nrRows, int nrColumns){
this.sc = sc;
this.nrRows = nrRows;
this.nrColumns = nrColumns;
createBoard(sc,nrRows,nrColumns);
finnishingVows();
}
void finnishingVows(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
void createBoard(SudokuContainer sc, int nrRows, int nrColumns) {
JPanel panel = new JPanel();
JLabel label;
JButton previousButton;
JButton nextButton;
previousButton = new JButton("Previous (" + cnt + "/" + sc.nrSolutions + ")");
nextButton = new JButton("Next (" + cnt + "/" + sc.nrSolutions + ")");
solution = sc.get(cnt);
nextButton.addActionListener(new Listener());
panel.setLayout(new GridLayout(nrRows*nrColumns,nrRows*nrColumns));
previousButton.addActionListener(new Listener2());
for(int i = 0; i <solution.length;i++){
for(int j = 0; j < solution[i].length; j++){
String values = " "+solution[i][j]+" ";
if(values.equals(" . ")) values = "";
label = new JLabel(values);
if((i+1)%2 != 0)
if(j == nrRows){
label.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black)); //FIKSER PÅ STREKENE
}
else if(j == nrColumns){
label.setBorder(BorderFactory.createMatteBorder(1, 3, 1, 1, Color.black));//FIKSER PÅ STREKENE
}
else
label.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));
if((i+1)%2 == 0)
if(j == nrRows){
label.setBorder(BorderFactory.createMatteBorder(1, 1, 3, 1, Color.black));
}
else if(j == nrColumns){
label.setBorder(BorderFactory.createMatteBorder(1, 3, 3, 1, Color.black));
}
else label.setBorder(BorderFactory.createMatteBorder(1, 1, 3, 1, Color.black));
label.setFont(new Font("SansSerif", Font.BOLD, 32));
panel.add(label);
}
}
add(panel, BorderLayout.NORTH);
add(nextButton, BorderLayout.EAST);
add(previousButton, BorderLayout.WEST);
}
class Listener implements ActionListener{
public void actionPerformed(ActionEvent e){
cnt++;
createBoard(sc,nrRows,nrColumns);
finnishingVows();
}
}
class Listener2 implements ActionListener{
public void actionPerformed(ActionEvent e){
cnt--;
createBoard(sc,nrRows,nrColumns);
finnishingVows();
}
}
} //class Gui slutt