Hallo zusammen,
irgendwie komme ich nicht weiter.
Ich möchte aus einem jformattedtextfield einen Betrag mit Nachkommastelle auslesen (z.B. 1234,56)
und diesen in ein Double Feld einlesen um damit zu rechnen.
Leider bekomme ich sobald ich ein Komma eingeben eine Exception.
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1234,5"
Anbei der Code:
Hat jemand von Euch vielleicht eine Idee, wie ich einen Betrag aus einem JFormattedTextField in ein Double Field bekomme?
Vielen Dank im Voraus,
Viele Grüße
Jochen
irgendwie komme ich nicht weiter.
Ich möchte aus einem jformattedtextfield einen Betrag mit Nachkommastelle auslesen (z.B. 1234,56)
und diesen in ein Double Feld einlesen um damit zu rechnen.
Leider bekomme ich sobald ich ein Komma eingeben eine Exception.
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1234,5"
Anbei der Code:
Java:
import java.awt.*;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.event.*;
import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;
/*
* Created by JFormDesigner on Fri May 27 10:13:04 CEST 2011
*/
/**
* @author
*/
public class test extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
test frame = null;
frame = new test();
frame.setDefaultCloseOperation(test.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public test() {
initComponents();
}
private void EingabeCaretUpdate(CaretEvent e) {
double tmp_eingabe; String test_eingabe = Eingabe.getText();
test_eingabe.replace(",", ".");
if(test_eingabe.equals("")) {
tmp_eingabe = 0;
} else {
tmp_eingabe = Double.parseDouble(Eingabe.getText());
double tmp_ausgabe = tmp_eingabe * 1.95583;
Ausgabe.setText((NumberFormat.getCurrencyInstance().format( Double.valueOf(tmp_ausgabe) )));
}
System.out.println("Eingabe: "+ tmp_eingabe);
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
dialogPane = new JPanel();
contentPanel = new JPanel();
label1 = new JLabel();
Eingabe = new JFormattedTextField();
label2 = new JLabel();
Ausgabe = new JFormattedTextField();
buttonBar = new JPanel();
okButton = new JButton();
//======== this ========
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
//======== dialogPane ========
{
dialogPane.setBorder(Borders.DIALOG_BORDER);
dialogPane.setLayout(new BorderLayout());
//======== contentPanel ========
{
contentPanel.setLayout(new FormLayout(
"default, $lcgap, 81dlu",
"2*(default, $lgap), default"));
//---- label1 ----
label1.setText("Eingabeformat:");
contentPanel.add(label1, CC.xy(1, 1));
//---- Eingabe ----
Eingabe.setHorizontalAlignment(SwingConstants.RIGHT);
Eingabe.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
EingabeCaretUpdate(e);
}
});
contentPanel.add(Eingabe, CC.xy(3, 1));
//---- label2 ----
label2.setText("Ausgabebetrag:");
contentPanel.add(label2, CC.xy(1, 5));
//---- Ausgabe ----
Ausgabe.setHorizontalAlignment(SwingConstants.RIGHT);
Ausgabe.setEnabled(false);
contentPanel.add(Ausgabe, CC.xy(3, 5));
}
dialogPane.add(contentPanel, BorderLayout.CENTER);
//======== buttonBar ========
{
buttonBar.setBorder(Borders.BUTTON_BAR_GAP_BORDER);
buttonBar.setLayout(new FormLayout(
"$glue, $button",
"pref"));
//---- okButton ----
okButton.setText("OK");
buttonBar.add(okButton, CC.xy(2, 1));
}
dialogPane.add(buttonBar, BorderLayout.SOUTH);
}
contentPane.add(dialogPane, BorderLayout.CENTER);
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JPanel dialogPane;
private JPanel contentPanel;
private JLabel label1;
private static JFormattedTextField Eingabe;
private JLabel label2;
private JFormattedTextField Ausgabe;
private JPanel buttonBar;
private JButton okButton;
// JFormDesigner - End of variables declaration //GEN-END:variables
}
Hat jemand von Euch vielleicht eine Idee, wie ich einen Betrag aus einem JFormattedTextField in ein Double Field bekomme?
Vielen Dank im Voraus,
Viele Grüße
Jochen