Hallo liebe Forum-Gemeinde,
habe ein kleines Problem bei einem Informatikprojekt für mein Studium, als Chemiker hat man halt wenig Ahnung von Info.
Bei meinem TextfieldFilter muss ich noch ergänzen, dass wenn das Textfeld nicht gefüllt bzw. leer ist das Programm eine Null zurückgeben soll, damit in einer anderen Klasse ohne Fehlermeldung weiter gerechnet werden soll, andernfalls bekomme ich immer die Fehlermeldung:
Hier mal der Code zu meinem Textfieldfilter den ich aus einem Forum habe:
[JAVA=42]
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package formular.util;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
/**
*
* @author Sebastian
*/
public class TextFieldFilter extends PlainDocument {
public static final String NUMERIC = "0123456789";
protected String acceptedChars = null;
protected boolean negativeAccepted = false;
public TextFieldFilter() {
this(NUMERIC);
}
public TextFieldFilter(String acceptedchars) {
acceptedChars = acceptedchars;
}
public void setNegativeAccepted(boolean negativeaccepted) {
if (acceptedChars.equals(NUMERIC)) {
negativeAccepted = negativeaccepted;
acceptedChars += "-";
}
}
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
if (str == null)
return;
for (int i = 0; i < str.length(); i++) {
if (acceptedChars.indexOf(str.valueOf(str.charAt(i))) == -1)
return;
}
if (negativeAccepted) {
if (str.indexOf(".") != -1) {
if (getText(0, getLength()).indexOf(".") != -1) {
return;
}
}
}
if (negativeAccepted && str.indexOf("-") != -1) {
if (str.indexOf("-") != 0 || offset != 0) {
return;
}
}
super.insertString(offset, str, attr);
}
}
[/code]
Hat da jemand ne Idee, wie das Problem schnell gelöst werden kann? Wäre super
habe ein kleines Problem bei einem Informatikprojekt für mein Studium, als Chemiker hat man halt wenig Ahnung von Info.
Bei meinem TextfieldFilter muss ich noch ergänzen, dass wenn das Textfeld nicht gefüllt bzw. leer ist das Programm eine Null zurückgeben soll, damit in einer anderen Klasse ohne Fehlermeldung weiter gerechnet werden soll, andernfalls bekomme ich immer die Fehlermeldung:
Code:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
Hier mal der Code zu meinem Textfieldfilter den ich aus einem Forum habe:
[JAVA=42]
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package formular.util;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
/**
*
* @author Sebastian
*/
public class TextFieldFilter extends PlainDocument {
public static final String NUMERIC = "0123456789";
protected String acceptedChars = null;
protected boolean negativeAccepted = false;
public TextFieldFilter() {
this(NUMERIC);
}
public TextFieldFilter(String acceptedchars) {
acceptedChars = acceptedchars;
}
public void setNegativeAccepted(boolean negativeaccepted) {
if (acceptedChars.equals(NUMERIC)) {
negativeAccepted = negativeaccepted;
acceptedChars += "-";
}
}
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
if (str == null)
return;
for (int i = 0; i < str.length(); i++) {
if (acceptedChars.indexOf(str.valueOf(str.charAt(i))) == -1)
return;
}
if (negativeAccepted) {
if (str.indexOf(".") != -1) {
if (getText(0, getLength()).indexOf(".") != -1) {
return;
}
}
}
if (negativeAccepted && str.indexOf("-") != -1) {
if (str.indexOf("-") != 0 || offset != 0) {
return;
}
}
super.insertString(offset, str, attr);
}
}
[/code]
Hat da jemand ne Idee, wie das Problem schnell gelöst werden kann? Wäre super