DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Swing JComponent Validation with JGoodies Bindings Framework

Swing JComponent Validation with JGoodies Bindings Framework

Anees Ur-rehman user avatar by
Anees Ur-rehman
·
Oct. 08, 09 · Interview
Like (0)
Save
Tweet
Share
16.83K Views

Join the DZone community and get the full member experience.

Join For Free

For Validating the JComponents i.e JTextField, JTextArea etc. there are many validation libraries available. Being software engineers, we use different libraries for binding and different libraries for validation. Many of us use JGoodies binding framework for binding the  components and then use the Hibernate validation framework or JGoodies validation framework for validating the inputs to these components.

I am working on the JGoodies binding framework for binding my UI Swing widgets and in doing so I found one very important class i.e. AbstractVetoableValueModel. AbstractVetoableValueModel is the best class which can be used with JGoodies ValueModel for binding and validation. AbstractVetoableValueModel will veto any input that is against the validation grammar defined by the implemented class of it. The best example is to Allow Alpha or Numeric or Alpha-Numeric, size of the input data, whether special characters are allowed or not.

I want to follow with one small example that will ease the understanding

/**
*
* @author Anees
*
*/
public class MapVetoableValueModelAdaptor extends AbstractVetoableValueModel {

private static final long serialVersionUID = 1L;

private MapValueModel valueModel;

private JComponent component;

private PropertyConstraintsHolder constraintHolder;

private final String ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private final String NUMERIC = "1234567890.";
private final String ALPHA_NUMERIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890.";
private final String BADCHARACTERS = "`~!@#$%^&*()_+=\\|\"':;?/>.<,";

public MapVetoableValueModelAdaptor(MapValueModel subject,
JComponent component, PropertyConstraintsHolder constraintHolder) {
super(subject);
this.valueModel = subject;
this.component = component;
this.constraintHolder = constraintHolder;
}

@Override
public boolean proposedChange(final Object oldValue, Object proposedNewValue) {
final boolean flag;
if (proposedNewValue.toString().length() <= constraintHolder
.getMaxLength()) {
if (checkForInputCharactor(proposedNewValue, constraintHolder
.getTextInputDataType())) {
flag = true;
} else
flag = false;
} else {

flag = false;
}

if (!flag) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
UpdateJComponent updateComponent = new UpdateJComponent(
component, oldValue);
updateComponent.processWithOldValue();
}
});
}
return flag;
}

/**
*
* @param proposedNewValue
* @return
*/
private boolean checkForInputCharactor(Object proposedNewValue,
TextFiledInputDataType inputDataType) {

if (TextFiledInputDataType.ONLYALPHA.equals(inputDataType)) {

return processOnlyAlpha(proposedNewValue);

} else if (TextFiledInputDataType.ONLYNUMBER.equals(inputDataType)) {

return processOnlyForNumeric(proposedNewValue);

} else
return processForAlphaNumeric(proposedNewValue);

}

/**
*
* @param proposedNewValue
* @return
*/
private boolean processForAlphaNumeric(Object proposedNewValue) {

boolean flag = true;

String str = proposedNewValue.toString();

int length = str.length();

for (int i = 0; i < length; i++) {

if (-1 == ALPHA_NUMERIC.indexOf(str.charAt(i))) {
flag = false;
}
}
return flag;

}

/**
*
* @param proposedNewValue
* @return
*/
private boolean processOnlyForNumeric(Object proposedNewValue) {

boolean flag = true;

String str = proposedNewValue.toString();

int length = str.length();

for (int i = 0; i < length; i++) {

if (-1 == NUMERIC.indexOf(str.charAt(i))) {
flag = false;
}
}
return flag;
}

/**
*
* @param proposedNewValue
* @return
*/
private boolean processOnlyAlpha(Object proposedNewValue) {

boolean flag = true;

String str = proposedNewValue.toString();

int length = str.length();

for (int i = 0; i < length; i++) {

if (-1 == ALPHA.indexOf(str.charAt(i))) {
flag = false;
}
}
return flag;
}

/**
*
* @author Anees
*
*/
private class UpdateJComponent {

private Object value;

private JComponent component;

public UpdateJComponent(JComponent component, Object oldValue) {

this.component = component;

this.value = oldValue;
}

/**
*
*/
public void processWithOldValue() {

if (component instanceof JTextComponent) {

JTextComponent txtComp = (JTextComponent) component;

if (null != value)
txtComp.setText(value.toString());
else
txtComp.setText("");
}
}
}
}

After creating this adapter we will bind it with swing JComponents rather than binding ValueModel.
Example usage is :

JTextField textField = new JTextField();

// Getting the ValueModel by using PresentationModel or creating ValueModel programmtically

ValueModel valueModel = .......

MapVetoableValueModelAdaptor vetoableAdapter =
new MapVetoableValueModelAdaptor(valueModel, textField, constraintHolder);

Bindings.bind(textField, vetoableAdapter);

By this way we can avoid the experimentation needs to bedone with other validation libraries and can save a lot of time by enjoying at beach

Binding (linguistics) Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Scaling Your Testing Efforts With Cloud-Based Testing Tools
  • Java Concurrency: LockSupport
  • A Beginner’s Guide To Styling CSS Forms
  • Demystifying Multi-Cloud Integration

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: