Jump to content

MVC vs ActionListener vs Exceptions

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Shaddix

Shaddix

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
I have a problem implementing an MVC architecture for my project

my controller class looks like this:


public class MainController {

	private MainFrame mainFrame;

	

	public MainController(MainFrame view){

		mainFrame = view;

		mainFrame.addStandingsListener(new StandingListener());

	}

	

	private class StandingListener implements ActionListener{

		public void actionPerformed(ActionEvent e){

			StandingFrame sf = StandingFrame.getInstance();

			TeamDB tdb = TeamDB.getInstance();

                        mainFrame.setLabel(tdb.getLabelContent);

		}

	}

}

now this won't compile because of my actionlistener
the object of tdb that I use throws an SQLException which I need to catch
the only thing that java lets me do is to put a try catch block around it, that would mean I interact with the user in my controller class, which I don't think is proper programming

does anyone have a solution for this problem?
in it I want

#2
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
Basically, you put your model in the controller, no? The actual handling of an event is made in model. So take the actual implementation out of the controller class and put it in the model part. There you can catch the exception and update the view accordingly. What you should do de-facto, in my opinion, is to make StandingListener public and take its implementation out of MainController. This way controller part wouldn't be aware of the implementation of the model.

#3
Shaddix

Shaddix

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
well catching an exception in the model is even more wrong I think, because most of the time you show the error to your user so he knows something went wrong, but you can't interact with your user in the model
the coding I did in the controller should be in the controller because it's the interaction with model and view
in my controller I say what needs to be in the view, and I use data generated in my model

I think I found a good solution

I added a public method in my view that shows the error to my user
	public void showException(String title, String message){
		JOptionPane.showMessageDialog(this, message, title, JOptionPane.ERROR_MESSAGE);
	}

I have added a try catch in my controller, and in the catch block I call the method showException("Error", "An error occured.")

#4
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
I didn't say interact with user in the model. I said catch the exception and UPDATE THE VIEW ACCORDINGLY - let the user know something went wrong.