user2133814
user2133814

Reputation: 2651

Class Structure and Location for MVC in Java

I am writing a GUI for a Sudoku program. I am trying to use a MVC framework. The following post was the basis for much of my current code: Building a GUI for a Sudoku Solver (Complete with ASCII Example). My question is where do the action listeners go? I assume they go in the controller, but it that a single class or many? I currently have a single class with a single action listener and if/else logic to deal each swing component which can fire an event. This code smells bad to me though. Is there a preferred way to structure the code? I expect to add quite a few more buttons and components and I want to make sure the path I am doing down in reasonable. I have read quite a few posts and web tutorials but havent found this answer yet.

Thank you

public class SudokuView extends JFrame{
SudokuController controller;
//SudokuEngine sudokuEngine;

private static final int yMar = 100;
private static final int xMar = 25;
private static final int cellSize = 40;
private static final int gridSpace = 3;
private static final int secSpace = 9;
private static final int width = 2*xMar+9*cellSize+2*secSpace+6*gridSpace+10;
private static final int height = 650;

public JButton[][] gridButtons;

public SudokuView() {
    controller = new SudokuController();
    //sudokuEngine = new SudokuEngine();
    gridButtons = new JButton[9][9];
    setTitle("Sudoku Ninja 0.1");
    setSize(width, height);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(null);
    setBackground(Color.GRAY);
    setResizable(false);

    DrawBlankGrid();

    setVisible(true);
}

public void setSudokuImplementation(SudokuEngineInterface listener) {
    controller.setListener(listener);
}

public final void DrawBlankGrid(){
    int y=yMar;
    for (int r=0;r<9;r++){
        int x=xMar;
        for (int c=0;c<9;c++){
            gridButtons[r][c]=new JButton("");
            gridButtons[r][c].setBounds(x,y,cellSize,cellSize);
            gridButtons[r][c].putClientProperty("caller", 1);
            gridButtons[r][c].putClientProperty("column", c);
            gridButtons[r][c].putClientProperty("row", r);
            gridButtons[r][c].addActionListener(new SudokuController());
            add(gridButtons[r][c]);
            if (c==2||c==5){
                x = x+cellSize+secSpace;
            }else{
                x = x+cellSize+gridSpace;
            }
        }
        if (r==2||r==5){
            y = y+cellSize+secSpace;
        }else{
            y = y+cellSize+gridSpace;
        }
    }
}

public class SudokuController implements ActionListener{
SudokuEngineInterface listener;

public void setListener(SudokuEngineInterface listener) {
    this.listener = listener;
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() instanceof JButton){
        JButton gridButtons = (JButton) e.getSource();
        System.out.println("clicked column " + gridButtons.getClientProperty("column") + ", row " + gridButtons.getClientProperty("row"));
    }
}
}

public class RunSudokuNinja implements Runnable{

@Override
public void run() {
    //SudokuEngineInterface sudokuEngine = new SudokuEngine();
    SudokuView sudokuView = new SudokuView();
    //sudokuView.setSudokuImplementation(sudokuEngine);
    sudokuView.setVisible(true);
}


public static void main(String[] args) {
    EventQueue.invokeLater(new RunSudokuNinja());
}
}

Upvotes: 2

Views: 696

Answers (1)

user2133814
user2133814

Reputation: 2651

I got it, http://leepoint.net/notes-java/GUI/structure/40mvc.html was very helpful. Basically inner classes in the controller and addListener methods in the view. Much better organization this way

Upvotes: 1

Related Questions