Ola pessoal…
venho desenvolvendo em jgoodies algum tempo…
gostaria de saber se eh possivel tratar as RuntimeException e as Exceptions nos metodos set
dos beans… veja um exemplo de um metodo set…
public class Usuario extends Model {
// Nome das propriedades --------------------------------------------------
public static final String PROPERTY_NOME = "nome";
public static final String PROPERTY_CARGO = "cargo";
public static final String PROPERTY_ADMINISTRADOR = "administrador";
public static final String PROPERTY_CONTROLE_PERMISSAO = "controlePermissao";
// Variaveis que definem o usuario ----------------------------------------
private String nome;
private String cargo;
private boolean isAdministrador;
private boolean isControlePermissao;
public Usuario(String nome, String cargo) {
this(nome, cargo, false);
}
public void setNome(String nome) {
if (nome == null) {
throw new IllegalArgumentException("nome nao pode ser null");
} else if (nome.equals("") || nome.equals(" ")) {
throw new IllegalArgumentException("nome nao pode ser vazio");
}
String valorAntigo = this.nome;
this.nome = nome;
this.firePropertyChange(PROPERTY_NOME, valorAntigo, this.nome);
}
}
qdo o campo do jTextField fica vazio lanca uma IllegalArgumentException…
Exception in thread "AWT-EventQueue-0" com.jgoodies.binding.beans.PropertyAccessException: Failed to set an adapted Java Bean property.
cause=java.lang.IllegalArgumentException: nome nao pode ser vazio
bean=Deodoro da Fonseca
bean type=Usuario
value=
value type=java.lang.String
property name=nome
property type=java.lang.String
property setter=public void Usuario.setNome(java.lang.String)
at com.jgoodies.binding.beans.PropertyAccessException.createWriteAccessException(PropertyAccessException.java:133)
at com.jgoodies.binding.beans.BeanUtils.setValue(BeanUtils.java:627)
at com.jgoodies.binding.beans.BeanAdapter.setValue0(BeanAdapter.java:1191)
at com.jgoodies.binding.beans.BeanAdapter.access$600(BeanAdapter.java:279)
at com.jgoodies.binding.beans.BeanAdapter$SimplePropertyAdapter.setValue(BeanAdapter.java:1382)
at com.jgoodies.binding.adapter.TextComponentConnector.setSubjectText(TextComponentConnector.java:297)
at com.jgoodies.binding.adapter.TextComponentConnector.updateSubject(TextComponentConnector.java:235)
at com.jgoodies.binding.adapter.TextComponentConnector$TextChangeHandler.removeUpdate(TextComponentConnector.java:359)
at javax.swing.text.AbstractDocument.fireRemoveUpdate(AbstractDocument.java:243)
at javax.swing.text.AbstractDocument.handleRemove(AbstractDocument.java:608)
at javax.swing.text.AbstractDocument.remove(AbstractDocument.java:576)
at javax.swing.text.DefaultEditorKit$DeleteNextCharAction.actionPerformed(DefaultEditorKit.java:1081)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1636)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2849)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2884)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2812)
at java.awt.Component.processEvent(Component.java:5818)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:697)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:962)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:834)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:661)
at java.awt.Component.dispatchEventImpl(Component.java:4285)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Caused by: java.lang.IllegalArgumentException: nome nao pode ser vazio
at Usuario.setNome(Usuario.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.jgoodies.binding.beans.BeanUtils.setValue(BeanUtils.java:621)
... 35 more
eu queria tratar essa situacao… obrigado e até mais…