I'm relatively new to Java and OO programming and have, naturally, embarked on a rather big project. I'm creating an application with a small but complicated GUI. I think I understand the concepts behind MVC but I'm having a little trouble actually implementing it. Here is some example code for reference:
So, to get the state of the button I call:Code:// Model class MyModel { private ButtonModel buttonModel = new DefaultButtonModel(); public ButtonModel getButtonModel() { return buttonModel; } public boolean getButtonState() { return buttonModel.isSelected(); } public void setButtonState(boolean x) { buttonModel.setSelected(x); } } // View class MyView { private MyModel myModel; public MyView(MyModel model) { myModel = model; myButton.setModel(myModel.getButtonModel()); } } // Controller class MySubThing { private MyModel model; private MyView view; public MySubThing(MyModel m, MyView v) { model = new MyModel(); view = new MyView(model); } public void getButtonState() { return model.getButtonState(); } } class MyMainThing { private MySubThing thing; public MySubThing() { thing = new MySubThing(); } }
The problem I'm wondering about is that I've got a method getButtonState() that just calls the method of the same name in the model class. If I've got a class that's taking care of multiple GUI components (say a few buttons, a couple checkboxes, and a selection box) then I end up having a bunch of get/set methods in my controller class that are basically just duplicates of the methods in the model class. The other option I was working with is like so:Code:boolean buttonState = thing.getButtonState();
so as not to have get/set methods in the controller that just call the matching ones in the model and instead, in MyMainThing using a call like this:Code:class MySubThing { public MyModel model; public MyView view;
But something tells me this is a bad idea. If you'd REALLY like to help, or you're just confused, the project is on sourceforge under the name 007Designer. Any help or suggestions at all would be appreciated, though. As I said, I'm new to this and don't want to pick up bad habits. I'd like to do it the right way from the start.Code:boolean buttonState = thing.model.getButtonState();
Thanks!


Reply With Quote


Bookmarks