Cinch makes MVC in Swing easy. Cinch is a Java library used by developers to simplify writing certain types of GUI code.
When developing Swing applications it's very easy to fall into the trap of not separating out Models and Controllers. It's all too easy to just store the state of that boolean in the checkbox itself, or that String in the JTextField. The design goal behind Cinch was to make it easier to apply MVC than to not by reducing much of the typical Swing friction and boilerplate.
Cinch uses Java annotations to reflectively wire up Models, Views, and Controllers.
- The Wiki has all the project documentation.
- API docs are available here
- Mailing lists are hosted on Google Groups:
- Please file issues on the Github Issue Tracker
- Email project admin: Ari Gesher
Example: IntroCinchMVC.java
Believe it or not, this is a complete Swing program (note the lack of anonymous listener objects) - full source is available in IntroCinchMVC.java:
publicclassIntroCinchMVC {
publicstaticclassIntroModelextendsDefaultBindableModel {
privateStringto = "";
privateStringsubject = "";
privateStringbody = "";
publicStringgetBody() {
returnbody;
}
publicvoidsetBody(Stringbody) {
this.body = body;
update();
}
publicStringgetSubject() {
returnsubject;
}
publicvoidsetSubject(Stringsubject) {
this.subject = subject;
update();
}
publicStringgetTo() {
returnto;
}
publicvoidsetTo(Stringto) {
this.to = to;
update();
}
publicStringgetCurrentMessage() {
if (Strings.isNullOrEmpty(to)) {
return"Fill out 'To' field.";
} if (Strings.isNullOrEmpty(subject)) {
return"Fill out 'Subject' field.";
} if (Strings.isNullOrEmpty(body)) {
return"Fill out 'Body'.";
} return"Ready to send.";
}
publicbooleanisReady() {
return !Strings.isNullOrEmpty(to) && !Strings.isNullOrEmpty(subject) && !Strings.isNullOrEmpty(body);
}
@OverridepublicStringtoString() {
return"IntroModel [to=" + to + ", subject=" + subject + ", body=" + body + "]";
}
}
publicstaticclassIntroController {
privatefinalIntroModelmodel;
publicIntroController(IntroModelmodel) {
this.model = model;
}
publicvoidsendEmail() {
System.out.println("Send: " + model);
}
publicvoidyell() {
model.setBody(model.getBody().toUpperCase());
}
}
privatefinalJPanelpanel = newJPanel();
privatefinalBindingsbindings = newBindings();
privatefinalIntroModelmodel = newIntroModel();
@SuppressWarnings("unused")
@BindableprivatefinalIntroControllercontroller = newIntroController(model);
@Bound(to = "to")
privatefinalJTextFieldtoField = newJTextField();
@Bound(to = "subject")
privatefinalJTextFieldsubjectField = newJTextField();
@Bound(to = "body")
privatefinalJTextAreabodyArea = newJTextArea();
@Action(call = "yell")
privatefinalJButtonyellButton = newJButton("YELL!");
@Action(call = "sendEmail")
@EnabledIf(to = "ready")
privatefinalJButtonsendButton = newJButton("Send");
@Bound(to = "currentMessage")
privatefinalJLabelmessageLabel = newJLabel("");
publicIntroCinchMVC() {
initializeInterface();
bindings.bind(this);
}
privatevoidinitializeInterface() {
// swing layout code cut for brevity// ...
}
publicJComponentgetDisplayComponent() {
returnpanel;
}
// main removed
}Cinch is made available under the Apache 2.0 License.
Copyright 2011 Palantir Technologies
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
