AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Only numeric text fields

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits
View previous topic :: View next topic  
Author Message
Advait Raut
Guest





PostPosted: Thu Nov 11, 2004 10:20 am    Post subject: Only numeric text fields Reply with quote



How do we inherit the textfield such that it only accepts only
numbers and not text???
please reply wit code or some hint
Back to top
Dirk Gerrit Oort
Guest





PostPosted: Thu Nov 11, 2004 10:31 am    Post subject: Re: Only numeric text fields Reply with quote



see api Interface Document in the package javax.swing The code let i up to
you

--
D.G. Oort
"Advait Raut" <maraut (AT) roltanet (DOT) com> schreef in bericht
news:7f99f092.0411110220.7af7a642 (AT) posting (DOT) google.com...
Quote:
How do we inherit the textfield such that it only accepts only
numbers and not text???
please reply wit code or some hint



Back to top
karlheinz klingbeil
Guest





PostPosted: Thu Nov 11, 2004 4:15 pm    Post subject: Re: Only numeric text fields Reply with quote



Advait Raut schrub am Donnerstag, 11. November 2004
11:20 folgendes:

Quote:
How do we inherit the textfield such that it only
accepts only numbers and not text???
please reply wit code or some hint

here you have a JNUmberField, which displays double
values according to a NumberFormat. The constructor
also needs default, minimum and maximum values to
accept.

--------------------snip----------------------------
/*
* Created on 24.08.2004
*/
package de.lunqual.rzpro.fenster;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.JTextField;


/**
* @author lunqual
* JNumberField stellt ein Textfeld zur Verfügung, das
zwar alles eingeben kann,
* aber nur Zahlen validiert.
*
* Der Inhalt des Feldes wird vollständig selektiert,
wenn das Feld den Focus
* erhält.
*
* Das Feld wird beim Drücken der ENTER-Taste und beim
verlassen des Focus
* validiert
*/
public class JNumberField extends JTextField {

NumberFormat nf;
double defaultValue;
double value;
double min;
double max;
/**
* JNumberField Constructor
* @param aNf Numberformat
* @param nDef Defaultwert, wenn ungültige Eingaben
vorliegen
* @param nMin Minimalwert
* @param nMax Maximalwert
*/
public JNumberField(NumberFormat aNf,double
nDef,double nMin,double nMax) {
super();
defaultValue = nDef; min = nMin;max = nMax;
value = defaultValue;
nf = aNf;
setValue(value);
this.setSelectedTextColor(Color.WHITE);
this.setSelectionColor(Color.BLUE);
this.addKeyListener(new
java.awt.event.KeyAdapter() {
public void
keyReleased(java.awt.event.KeyEvent evt) {
if(evt.getKeyCode() == KeyEvent.VK_ENTER)
{
validateField();
}
}
});
this.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent evt) {
selectAll();
}
public void focusLost(FocusEvent evt) {
validateField();}
});
}
/** holt den Text aus dem Textfeld und versucht diesen
in eine Zahl
* zu wandeln. Wenn das nicht möglich ist, wird der
Defaultwert in den
* Text eingetragen.
* Wenn es eine Zahl ergibt, wird geprüft ob diese
kleiner als der Minimalwert
* oder größer als der Maximalwert ist und diese Werte
werden entsprechend
* angenommen.
* Anschliessend wird die entstandene Zahl wieder in
den Text geschrieben
*/
public void validateField() {
try {
double n =
((Number)nf.parse(this.getText())).doubleValue();
n = checkValue(n);
this.setText(nf.format(n));
value = n;
defaultValue = n;
}catch (ParseException e) {
this.setText(nf.format(defaultValue));
}
}

/** den Wert - nach Überprüfung ins Textfeld setzen
*
* @param aValue
*/
public void setValue(double aValue) {
value = checkValue(aValue);
defaultValue=value;
this.setText(nf.format(value));
}

/** das Feld validieren und den Wert aus dem Textfeld
holen
*
* @return value
*/
public double getValue() {
validateField();
return value;
}

/** den Wert prüfen, ob er größer oder kleiner ist als
* erlaubt
* @param n Wert zum prüfen
* @return n wenn ok, min oder max bei
Wertüberschreitung
*/
private double checkValue(double n) {
if (n < min) {
return min;
}
if(n > max) {
return max;
}
return n;
}
}

-----------------------snip-----------------------------

--
greetz Karlheinz Klingbeil (lunqual)
http://www.lunqual.de oder http:www.lunqual.net

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.