michael nesterenko
michael nesterenko

Reputation: 14449

manual swing data binding

I have rather big swing interface (several textboxes, comboboxes, checkboxes, custom popup dialogs etc) and a data model that has to be changed when ui control changes: new text is entered into text box, check box is clicked, etc.

The question is: what is the best practice to organize update+validation of input values.

Unfortunately I can not use binding framework like beansbinding.

Upvotes: 2

Views: 674

Answers (2)

Robin
Robin

Reputation: 36621

Combine the answer of JB Nizet with validation in your components, for example by using JFormattedTextField (or an enhanced version of this). You can use the JFormattedTextField also as editor for JComboBox instances. You can add validation to JSlider instances.

In short, provide immediate feedback to the user when he types in an invalid value. That combined with validation on the model side makes a good application.

This can be compared to a modern website: client-side validation with javascript to give the user immediate feedback + server-side validation for validation which does not go through the UI, or to avoid nasty users bypassing your client-side validation

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 692261

Add appropriate listeners to the components, and update the model when the events are fired.

Or design your UI so that everything is saved to the model only when a Save or OK button is clicked. This also helps with validation, because you just need to validate everything at once, when the button is clicked.

Upvotes: 2

Related Questions