-1

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
user3507600
  • 1,075
  • 9
  • 15
  • 1
    So what is the question? – Ordous Apr 16 '14 at 16:01
  • Each time the button is clicked you call `add(panel, BorderLayout.NORTH);` but there is already a component in there, you have to remove the old component before adding the new ones. Also you probably need to call `repaint()` – DSquare Apr 16 '14 at 16:47
  • This [example](http://stackoverflow.com/a/4151403/230513) may help. – trashgod Apr 16 '14 at 19:31

1 Answers1

0

As mentioned in one of the comments, you're not removing your components before adding more. If you make class variables that contain your buttons and panel, you can remove them and add the new ones. Here's my take on it:

class Gui extends JFrame{
    int cnt = 1;
    String[][] solution;
    int nrRows, nrColumns;
    SudokuContainer sc;
    private JPanel pan = null;
    private JButton nButton = null;
    private JButton pButton = null;

    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);
            }
        }

        if (pan != null) this.remove(pan);
        if (pButton != null) this.remove(pButton);
        if (nButton != null) this.remove(nButton);
        pan = panel;
        pButton = previousButton;
        nButton = nextButton;
        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();
        }
    }
}
user3507600
  • 1,075
  • 9
  • 15