0

i have a problem with a grid i created. the buttons of this smallerGrid will not appear until i hover over them with the mouse. each smallerGrid consists of a 3x3 grid which fits into a biggerGrid also 3x3 grid that conatins the smaller grids. that is a brief explanation of how i am constructing the sudoku grid. You will find the code below. Thank you in advance.

import java.awt.*;
import javax.swing.*;


public class MyGridLayout {
    private int filledFields = 0;
    private JFrame mainFrame;
    private JPanel panelForSolvingButton;
    private JPanel smallerGridPanel;
    private boolean solvingButtonAppeared = false;
    SudokuCell[][] biggerGrid = new SudokuCell[10][10];


    MyGridLayout() {
        mainFrame = new JFrame("sudoku-solver");
        mainFrame.setLayout(new BorderLayout());

        smallerGridPanel = new JPanel(new GridLayout(3, 3));
        mainFrame.add(smallerGridPanel, BorderLayout.CENTER);

        panelForSolvingButton = new JPanel();
        panelForSolvingButton.setLayout(new FlowLayout(FlowLayout.RIGHT));
        mainFrame.add(panelForSolvingButton, BorderLayout.SOUTH);

        mainFrame.pack();
        mainFrame.setSize(600,600);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        mainFrame.setResizable(false);

        addRegularButtons();
    }

    void addRegularButtons() {
        int currentCol = 0;
        int currentRow = 0;
        int smallerGridFirstCol = 0;
        int firstRowOfLayer = 0;

        for (int layerOfGrids = 0; layerOfGrids < 3; ++layerOfGrids) {
            for (int gridOfLayer = 0; gridOfLayer < 3; ++gridOfLayer) {
                JComponent smallerGrid = new JPanel(new GridLayout(3, 3));
                smallerGrid.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                smallerGridPanel.add(smallerGrid);

                for (int j = 0; j < 3; ++j) {
                    for (int k = 0; k < 3; ++k) {
                        var cell = new SudokuCell(currentRow, currentCol);
                        smallerGrid.add(cell);
                        biggerGrid[currentRow][currentCol++] = cell;
                        cell.addActionListener(new CellActionListener(this));
                    }
                    currentRow++;
                    currentCol = smallerGridFirstCol;
                }
                smallerGridPanel.revalidate();

                currentRow = firstRowOfLayer;
                smallerGridFirstCol += 3;
                currentCol = smallerGridFirstCol;
            }

            firstRowOfLayer += 3;
            currentRow = firstRowOfLayer;
            smallerGridFirstCol = currentCol = 0;
        }
        mainFrame.revalidate();
        mainFrame.setVisible(true);
    }


    // checking if the solving process can begin
    // (filled fields must at least reach 17)
    void makeSolveButtonAppear() {
        JButton solveButton = new JButton();
        solveButton.setBackground(Color.white);
        solveButton.setFont(new Font("Arial", Font.PLAIN, 20));
        solveButton.setOpaque(false);
        solveButton.setText("Solve ?");
        panelForSolvingButton.add(solveButton);
    }

    public boolean isSolvingButtonAppeared() {
        return solvingButtonAppeared;
    }

    public void setSolvingButtonAppeared(boolean solvingButtonAppeared) {
        this.solvingButtonAppeared = solvingButtonAppeared;
    }

    public int getFilledFields() {
        return filledFields;
    }

    public void setFilledFields(int filledFields) {
        this.filledFields = filledFields;
    }

    public JPanel getPanelForSolvingButton() {
        return panelForSolvingButton;
    }

    public static void main(String[] args) {
        var gridLayout = new MyGridLayout();
    }
}

package mainFrame;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;

public class SudokuCell extends JButton {
    private int x;
    private int y;

    public SudokuCell(int x, int y) {
        this.x = x;
        this.y = y;
        setBackground(Color.white);
        setOpaque(true);
        setFont(new Font("Arial", Font.PLAIN, 20));
    }

    @Override
    public int getX() {
        return x;
    }

    @Override
    public int getY() {
        return y;
    }
}

package mainFrame;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CellActionListener implements ActionListener {
    private int clicked = 0;
    MyGridLayout currentGrid;

    CellActionListener(MyGridLayout currentGrid) {
        this.currentGrid = currentGrid;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(clicked ==0) currentGrid.setFilledFields(currentGrid.getFilledFields()+1);

        //clicked (number of clicks) must remain between 0 and 9
        clicked = (clicked % 9) + 1;
        ((JButton) e.getSource()).setText(Integer.toString(clicked));

        //first click on the button means an entry has been made
        //which means one less needed filledField out of 17
        if(!currentGrid.isSolvingButtonAppeared() && currentGrid.getFilledFields() >= 17) {
            currentGrid.makeSolveButtonAppear();
            currentGrid.setSolvingButtonAppeared(true);
        }
    }
}```
  • 1
    1) `public int getX() {` Don't override these methods. They have existing and necessary functionality. 2) `public class SudokuCell extends JButton {` Don't extend components, windows or containers unless there is good cause. (From a casual look at the code) There isn't, here. 3) `mainFrame.setSize(600,600);` That's just a guess and does not account for frame decoartions. Call `pack()` instead. – Andrew Thompson Mar 28 '20 at 11:13
  • 4) `new Font("Arial", Font.PLAIN, 20)` a) a `Font` can be reused, so only instantiate it once. b) But OS X systems will not have Arial installed. Better to use logical fonts like `Font.SANS_SERIF`. 5) Coming back to *"Don't extend components .. unless there is good cause."* There might be 'good cause' here if the buttons need to be square, rather than rectangular. If so, override the `getPreferredSize()` method to return a `Dimension` where the largest of the super method's width or height is used for both width **&** height. 6) See [an example](https://stackoverflow.com/a/43790348/418556). – Andrew Thompson Mar 28 '20 at 11:51
  • I cannot reproduce the problem. The buttons appear properly with no text and a white color as you set them to do. Then the *solve* button pops up when 17 at least buttons have text. – gthanop Mar 31 '20 at 01:57
  • I had a problem with the SudokuCell class. Somehow they didn’t appear but now i’ve changed it to normal JButton. Thnaks to all of you guys ! – fraj yassine Lakhal Apr 01 '20 at 08:08

0 Answers0