Commit e9e532f7 authored by Christophe Demarey's avatar Christophe Demarey

Handle Number format exceptions and use formatted text fields.

parent 93ba7f05
......@@ -30,13 +30,19 @@ import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import org.jdesktop.layout.GroupLayout;
import org.jdesktop.layout.LayoutStyle;
import org.objectweb.util.explorer.swing.gui.api.DialogBox;
import org.objectweb.util.explorer.swing.gui.lib.DefaultDialogBox;
import org.ow2.frascati.examples.reconfig.converter.CurrencyConverter;
import org.ow2.frascati.explorer.gui.AbstractSelectionPanel;
......@@ -50,8 +56,10 @@ public class ConverterPanel extends AbstractSelectionPanel<CurrencyConverter> {
private NumberFormat nf; // used to format double display
private JLabel jLabelDollar;
private JLabel jLabelEuro;
private JTextField jTextFieldDollar;
private JTextField jTextFieldEuro;
private JFormattedTextField jTextFieldDollar;
private JFormattedTextField jTextFieldEuro;
private NumberFormat euroFormat;
private NumberFormat dollarFormat;
/**
* Default constructor creates the panel.
......@@ -64,8 +72,10 @@ public class ConverterPanel extends AbstractSelectionPanel<CurrencyConverter> {
jLabelEuro = new JLabel();
jLabelDollar = new JLabel();
jTextFieldEuro = new JTextField();
jTextFieldDollar = new JTextField();
euroFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);
dollarFormat = NumberFormat.getCurrencyInstance(Locale.US);
jTextFieldEuro = new JFormattedTextField(euroFormat);
jTextFieldDollar = new JFormattedTextField(dollarFormat);
jLabelEuro.setText("Euro");
jLabelEuro.setName("jLabelEuro");
......@@ -116,12 +126,18 @@ public class ConverterPanel extends AbstractSelectionPanel<CurrencyConverter> {
public void keyTyped(KeyEvent e) { }
public void keyReleased(KeyEvent e) {
if ( jTextFieldEuro.getText().length() > 0 ) {
double value = Double.parseDouble( jTextFieldEuro.getText().replaceAll(",", ".") );
double result = selected.euroToDollar(value);
jTextFieldDollar.setText( nf.format(result) );
if ( e.getKeyCode() == KeyEvent.VK_ENTER ) {
String text = jTextFieldEuro.getText();
if (text.length() > 0) {
Double value = parseDouble(text);
if (value != null) {
jTextFieldEuro.setValue(value);
Double result = selected.euroToDollar(value);
jTextFieldDollar.setValue(result);
}
}
}
}
}
}
);
jTextFieldDollar.addKeyListener( new KeyListener() {
......@@ -130,10 +146,14 @@ public class ConverterPanel extends AbstractSelectionPanel<CurrencyConverter> {
public void keyTyped(KeyEvent e) { }
public void keyReleased(KeyEvent e) {
if ( jTextFieldDollar.getText().length() > 0 ) {
double value = Double.parseDouble( jTextFieldDollar.getText().replaceAll(",", ".") );
double result = selected.dollarToEuro(value);
jTextFieldEuro.setText( nf.format(result) );
if ( e.getKeyCode() == KeyEvent.VK_ENTER ) {
String text = jTextFieldDollar.getText();
if (text.length() > 0) {
Double value = parseDouble(text);
jTextFieldDollar.setValue(value);
Double result = selected.dollarToEuro(value);
jTextFieldEuro.setValue(result);
}
}
}
}
......@@ -154,7 +174,15 @@ public class ConverterPanel extends AbstractSelectionPanel<CurrencyConverter> {
}
}
);
}
private Double parseDouble(String s) {
try {
return Double.parseDouble(s);
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(this, "Input type must be a double value!");
}
return null;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment