Hi,
I have a problem with different behaviour in form with defaultButton and
valuechange listener on textfield. In my test application I've attached, I'm
trying to validate text input after valuechange on textfield1. When value is
not valid, it is reseted with the default value and exception is thrown. The
exception is then catched, alert message is displayed, textfield is
refreshed and focus is set on to correct input field. Ok button
(defaultButton) revalidate the all form inputs and display error alert
messages too.
Behaviour is different when I type invalid input (e.g. "1") and invoke
action on "Ok" button by hitting Enter or by mouse click.
First scenario (enter) notify both listeners (action and valuechange) and
display 2 alert messages. This is the output on console:
changed field value: 1
refreshed bean value:
changed field value:
ok performed
That dialogs have another issues. Only first have default action on Ok
button and set ESC keystroke to close message. After first dialog close the
main frame react on ESC keystroke and defaultButton.
Second scenario (button click) blocks action listener and notify valuechange
listener on textfield only.
changed field value: 1
refreshed bean value:
changed field value:
I'd like to achieve behaviour from second scenario in both cases but I can't
find any solution to achieve that. Any ideas are welcome.
Thanks.
Vlado
--------------------------------------------------------------------------
Tato sprava a vsetky pripojene subory su doverne a urcene vyhradne osobam
alebo organizaciam, ktorym boli adresovane. Ak ste dostali tento e-mail
omylom, prosim, upovedomte Chemosvit, a.s. ([EMAIL PROTECTED]).
This email and any files transmitted are confidential and intended
solely for the use of the individual or entity to which they are
addressed. If you have received this email in error, please notify
Chemosvit, a.s. ([EMAIL PROTECTED]).
---------------------------------------------------------------------------
import java.io.Serializable;
import java.util.Observable;
import java.util.Observer;
import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.ULCAlert;
import com.ulcjava.base.application.ULCBoxPane;
import com.ulcjava.base.application.ULCButton;
import com.ulcjava.base.application.ULCComponent;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCLabel;
import com.ulcjava.base.application.ULCTextField;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.application.event.IActionListener;
import com.ulcjava.base.application.event.KeyEvent;
import com.ulcjava.base.application.event.ValueChangedEvent;
import com.ulcjava.base.application.event.serializable.IValueChangedListener;
import com.ulcjava.base.application.util.KeyStroke;
public class DefaultButtonAndValueChanged extends AbstractApplication implements Serializable, Observer {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
com.ulcjava.base.development.DevelopmentRunner.setApplicationClass(DefaultButtonAndValueChanged.class);
com.ulcjava.base.development.DevelopmentRunner.setUseGui(false);
com.ulcjava.base.development.DevelopmentRunner.run();
}
private ULCTextField field1;
private Bean bean;
public void start() {
final ULCFrame frame = new ULCFrame();
frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);
bean = new Bean();
bean.addObserver(this);
ULCBoxPane mainPane = new ULCBoxPane(true);
ULCLabel label1 = new ULCLabel("Value 1:");
field1 = new ULCTextField(20);
field1.setText(bean.getValue());
field1.addValueChangedListener(new IValueChangedListener() {
private static final long serialVersionUID = 1L;
public void valueChanged(ValueChangedEvent event) {
try {
System.out.println("changed field value: " + field1.getText());
bean.setValue(field1.getText());
} catch (Exception e) {
ULCAlert alert = new ULCAlert(field1.getRootPane(), "Error", e.getMessage(), "Ok");
alert.show();
System.out.println("refreshed bean value: " + bean.getValue());
field1.setText(bean.getValue());
field1.requestFocusInWindow();
}
}
});
ULCLabel label2 = new ULCLabel("Value 2:");
ULCTextField field2 = new ULCTextField(20);
ULCBoxPane contentPane = new ULCBoxPane(2, 2);
contentPane.add(label1);
contentPane.add(field1);
contentPane.add(label2);
contentPane.add(field2);
ULCButton okBtn = new ULCButton("OK");
okBtn.addActionListener(new ExitOK());
ULCButton cancelBtn = new ULCButton("Cancel");
ExitCancel cancelListener = new ExitCancel();
cancelBtn.registerKeyboardAction(cancelListener, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, KeyEvent.VK_UNDEFINED),
ULCComponent.WHEN_IN_FOCUSED_WINDOW);
cancelBtn.addActionListener(new ExitCancel());
ULCBoxPane buttonPane = new ULCBoxPane(2, 1);
buttonPane.add(okBtn);
buttonPane.add(cancelBtn);
mainPane.add(ULCBoxPane.BOX_EXPAND_EXPAND, contentPane);
mainPane.add(ULCBoxPane.BOX_RIGHT_BOTTOM, buttonPane);
frame.setDefaultButton(okBtn);
frame.add(mainPane);
frame.setSize(600, 400);
frame.setVisible(true);
}
private String validate() {
return bean.validate();
}
public void update(Observable o, Object arg) {
// System.out.println("updated bean value: " + bean.getValue());
// field1.setText(bean.getValue());
}
class ExitOK implements IActionListener {
public void actionPerformed(ActionEvent event) {
System.out.println("ok performed");
String error = validate();
if (error.length() > 0) {
ULCAlert alert = new ULCAlert(field1.getRootPane(), "Error", error, "Ok");
alert.show();
} else {
System.out.println("ok performed : bean value = " + bean.getValue());
}
}
}
class ExitCancel implements IActionListener {
public void actionPerformed(ActionEvent event) {
System.out.println("cancel performed");
}
}
private class Bean extends Observable {
String value = "";
String getValue() {
return value;
}
private void setValue(String newValue) throws Exception {
if (!value.equals(newValue)) {
if (newValue.length() == 2) {
this.value = newValue;
} else {
this.value = "";
throw new Exception("Bad value = " + newValue);
}
setChanged();
notifyObservers();
}
}
String validate() {
if (value.length() != 2) {
return "OK Bad value = " + value;
}
return "";
}
}
}