Unterschiede
Hier werden die Unterschiede zwischen der gewählten und der aktuellen Version gezeigt.
swing:jtextfield-numeric 2007/11/26 12:29 | swing:jtextfield-numeric 2020/01/22 20:59 aktuell | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | [[swing:swing|Java - AWT/Swing]]\\ | ||
+ | ====== JTextField, begrenzen auf numerische Eingabe ====== | ||
+ | Im folgenden Code-Beisiel werden nur numerische Eingaben akzeptiert:\\ | ||
+ | Dazu wird ein eigenes Document "NumberDocument" definiert.\\ | ||
+ | \\ | ||
+ | <html> | ||
+ | <script type="text/javascript"><!-- | ||
+ | google_ad_client="pub-9681858985507948"; | ||
+ | google_ad_width = 468; | ||
+ | google_ad_height = 60; | ||
+ | google_ad_format = "468x60_as"; | ||
+ | google_ad_type = "text"; | ||
+ | google_ad_channel = ""; | ||
+ | google_color_border = "cccccc"; | ||
+ | google_color_bg = "FFFFFF"; | ||
+ | google_color_link = "1d2d8c"; | ||
+ | google_color_text = "000000"; | ||
+ | google_color_url = "1d2d8c"; | ||
+ | //--> | ||
+ | </script> | ||
+ | <script type="text/javascript" | ||
+ | src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> | ||
+ | </script> | ||
+ | </html> | ||
+ | \\ | ||
<code java> | <code java> | ||
package com.sowas.snippets.jtextfield; | package com.sowas.snippets.jtextfield; | ||
Zeile 12: | Zeile 37: | ||
- | public class NumberDocument extends PlainDocument{ | + | public class NumberDocument extends PlainDocument { |
- | @override | + | @Override |
public void insertString (final int offset, final String text, | public void insertString (final int offset, final String text, | ||
final AttributeSet attributeSet) throws BadLocationException { | final AttributeSet attributeSet) throws BadLocationException { | ||
Zeile 25: | Zeile 50: | ||
if (text.length() > 0){ | if (text.length() > 0){ | ||
// Versuchen eine Zahl zu erzeugen: | // Versuchen eine Zahl zu erzeugen: | ||
- | new BigDecimal (toTest); | + | new BigDecimal(text); |
} | } | ||
return true; | return true; | ||
Zeile 36: | Zeile 61: | ||
public static void main (final String [] ignored){ | public static void main (final String [] ignored){ | ||
JFrame frame = new JFrame ("Numerisches JTextField"); | JFrame frame = new JFrame ("Numerisches JTextField"); | ||
- | frmae.getContentPane().setLayout (new BorderLayout (0,0)); | + | frame.getContentPane().setLayout (new BorderLayout (0,0)); |
frame.getContentPane().add(new JTextField (new NumberDocument (), "", 0), BorderLayout.NORTH); | frame.getContentPane().add(new JTextField (new NumberDocument (), "", 0), BorderLayout.NORTH); | ||
frame.pack (); | frame.pack (); |