From: Robbo on
Hello,

I need to control content of text field in Swing.
I mean:
- user may enter only digits (0-9) and no other characters,
- there may by no more than 4 digits in text field,
- if value in text field is smaller than 50 or greater than 500,
background of text field should become red -- if value
it correct, background should be green.
All checkings should take place "on-line" (after each new
character(s) or after deleting of character(s)).

I created two working examples with use of a) JTextField
and b) JFormattedTextField.
I just wonder if there is some other (better) way to solve
my problem...?
Since I am new to Swing, I wish to learn correct methods
of solving basic problems. So, please tell me if my code is
good, and what are other possibilities. Thanks in advance!

Regards,
Robbo

------------------------
a)

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;

public class Swing {
public static void main(String[] args) {
new Swing();
}

private static final int MIN = 50;
private static final int MAX = 500;
private static final int MAX_CHARS = 4;

JTextField tf1 = null;

public Swing() {
JFrame f = new JFrame("Swing...");
f.setSize(300, 120);
Container c = f.getContentPane();
c.setLayout(new FlowLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tf1 = new JTextField(10);
MyDocument md = new MyDocument();
tf1.setDocument(md);
md.addDocumentListener(new MyDocumentListener());
c.add(tf1);
f.setVisible(true);
}

class MyDocument extends PlainDocument {
@Override
public void insertString(int ofs, String str,
javax.swing.text.AttributeSet a)
throws
javax.swing.text.BadLocationException {
if (str == null)
return;

char[] t = new char[str.length()];
int l = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isDigit(c))
t[l++] = c;
}

if ((str.length() != l) || (ofs >= MAX_CHARS)) {
Toolkit.getDefaultToolkit().beep();
l = 0;
}

super.insertString(ofs, new String(t, 0, l), a);
}
}

class MyDocumentListener implements DocumentListener {
public void changedUpdate(DocumentEvent e) { }

public void insertUpdate(DocumentEvent e) {
funct();
}

public void removeUpdate(DocumentEvent e) {
funct();
}

private void funct() {
int i = 0;
try {
i = Integer.valueOf(tf1.getText());
} catch (NumberFormatException e) {
tf1.setBackground(Color.red);
return;
}

if ((i < MIN) || (i > MAX)) {
tf1.setBackground(Color.red);
} else
tf1.setBackground(Color.green);
}
}
}
------------------------
b)

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;

public class Swing {
public static void main(String[] args) {
new Swing();
}

private static final int MIN = 50;
private static final int MAX = 500;

JFormattedTextField ftf1 = null;

public Swing() {
JFrame f = new JFrame("Swing...");
f.setSize(250, 100);
Container c = f.getContentPane();
c.setLayout(new BorderLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
ftf1 = new JFormattedTextField(new MaskFormatter("####"));
} catch (java.text.ParseException e) {
System.exit(1);
}

ftf1.getDocument().addDocumentListener(new MyDocumentListener());
c.add(ftf1);
f.setVisible(true);
}

class MyDocumentListener implements DocumentListener {
public void changedUpdate(DocumentEvent e) { }

public void insertUpdate(DocumentEvent e) {
funct();
}

public void removeUpdate(DocumentEvent e) {
funct();
}

private void funct() {
int i = 0;
try {
i = Integer.valueOf(ftf1.getText().trim());
} catch (NumberFormatException e) {
ftf1.setBackground(Color.red);
return;
}

if ((i < MIN) || (i > MAX)) {
ftf1.setBackground(Color.red);
} else
ftf1.setBackground(Color.green);
}
}
}


From: Knute Johnson on
On 3/20/2010 10:58 AM, Robbo wrote:
> Hello,
>
> I need to control content of text field in Swing.
> I mean:
> - user may enter only digits (0-9) and no other characters,
> - there may by no more than 4 digits in text field,
> - if value in text field is smaller than 50 or greater than 500,
> background of text field should become red -- if value
> it correct, background should be green.
> All checkings should take place "on-line" (after each new
> character(s) or after deleting of character(s)).
>
> I created two working examples with use of a) JTextField
> and b) JFormattedTextField.
> I just wonder if there is some other (better) way to solve
> my problem...?
> Since I am new to Swing, I wish to learn correct methods
> of solving basic problems. So, please tell me if my code is
> good, and what are other possibilities. Thanks in advance!
>
> Regards,
> Robbo
>
> ------------------------
> a)
>
> import javax.swing.*;
> import javax.swing.event.*;
> import javax.swing.text.*;
> import java.awt.*;
> import java.awt.event.*;
>
> public class Swing {
> public static void main(String[] args) {
> new Swing();
> }
>
> private static final int MIN = 50;
> private static final int MAX = 500;
> private static final int MAX_CHARS = 4;
>
> JTextField tf1 = null;
>
> public Swing() {
> JFrame f = new JFrame("Swing...");
> f.setSize(300, 120);
> Container c = f.getContentPane();
> c.setLayout(new FlowLayout());
> f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> tf1 = new JTextField(10);
> MyDocument md = new MyDocument();
> tf1.setDocument(md);
> md.addDocumentListener(new MyDocumentListener());
> c.add(tf1);
> f.setVisible(true);
> }
>
> class MyDocument extends PlainDocument {
> @Override
> public void insertString(int ofs, String str,
> javax.swing.text.AttributeSet a)
> throws
> javax.swing.text.BadLocationException {
> if (str == null)
> return;
>
> char[] t = new char[str.length()];
> int l = 0;
> for (int i = 0; i< str.length(); i++) {
> char c = str.charAt(i);
> if (Character.isDigit(c))
> t[l++] = c;
> }
>
> if ((str.length() != l) || (ofs>= MAX_CHARS)) {
> Toolkit.getDefaultToolkit().beep();
> l = 0;
> }
>
> super.insertString(ofs, new String(t, 0, l), a);
> }
> }
>
> class MyDocumentListener implements DocumentListener {
> public void changedUpdate(DocumentEvent e) { }
>
> public void insertUpdate(DocumentEvent e) {
> funct();
> }
>
> public void removeUpdate(DocumentEvent e) {
> funct();
> }
>
> private void funct() {
> int i = 0;
> try {
> i = Integer.valueOf(tf1.getText());
> } catch (NumberFormatException e) {
> tf1.setBackground(Color.red);
> return;
> }
>
> if ((i< MIN) || (i> MAX)) {
> tf1.setBackground(Color.red);
> } else
> tf1.setBackground(Color.green);
> }
> }
> }
> ------------------------
> b)
>
> import javax.swing.*;
> import javax.swing.event.*;
> import javax.swing.text.*;
> import java.awt.*;
> import java.awt.event.*;
>
> public class Swing {
> public static void main(String[] args) {
> new Swing();
> }
>
> private static final int MIN = 50;
> private static final int MAX = 500;
>
> JFormattedTextField ftf1 = null;
>
> public Swing() {
> JFrame f = new JFrame("Swing...");
> f.setSize(250, 100);
> Container c = f.getContentPane();
> c.setLayout(new BorderLayout());
> f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> try {
> ftf1 = new JFormattedTextField(new MaskFormatter("####"));
> } catch (java.text.ParseException e) {
> System.exit(1);
> }
>
> ftf1.getDocument().addDocumentListener(new MyDocumentListener());
> c.add(ftf1);
> f.setVisible(true);
> }
>
> class MyDocumentListener implements DocumentListener {
> public void changedUpdate(DocumentEvent e) { }
>
> public void insertUpdate(DocumentEvent e) {
> funct();
> }
>
> public void removeUpdate(DocumentEvent e) {
> funct();
> }
>
> private void funct() {
> int i = 0;
> try {
> i = Integer.valueOf(ftf1.getText().trim());
> } catch (NumberFormatException e) {
> ftf1.setBackground(Color.red);
> return;
> }
>
> if ((i< MIN) || (i> MAX)) {
> ftf1.setBackground(Color.red);
> } else
> ftf1.setBackground(Color.green);
> }
> }
> }
>
>

How about just using a JSpinner?

--

Knute Johnson
email s/nospam/knute2010/

From: John B. Matthews on
In article <ho32hs$bsf$1(a)atlantis.news.neostrada.pl>,
"Robbo" <nie.mam(a)yle.com> wrote:

> I need to control content of text field in Swing.
> I mean:
> - user may enter only digits (0-9) and no other characters,
> - there may by no more than 4 digits in text field,
> - if value in text field is smaller than 50 or greater than 500,
> background of text field should become red -- if value
> it correct, background should be green.
> All checkings should take place "on-line" (after each new
> character(s) or after deleting of character(s)).
>
> I created two working examples with use of a) JTextField and b)
> JFormattedTextField. I just wonder if there is some other (better)
> way to solve my problem...? Since I am new to Swing, I wish to learn
> correct methods of solving basic problems. So, please tell me if my
> code is good, and what are other possibilities. Thanks in advance!

I've seen DocumentListener recommended for this; it's one sure way to
get per-keystroke checking, and it handles the color requirement.
Alternatively, you might also look at MaskFormatter as a way to
simplify the code:

<http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html#maskformatter>

Also look at InputVerifier as a means to validate range and control
focus traversal:

<http://java.sun.com/javase/6/docs/api/javax/swing/InputVerifier.html>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
From: Robbo on
> I've seen DocumentListener recommended for this; it's one sure way to
> get per-keystroke checking, and it handles the color requirement.
> Alternatively, you might also look at MaskFormatter as a way to
> simplify the code:

Actually I used DocumentListener in my example a) and b).
Also I used MaskFormatter in example b). MaskFormatter is great for
simple masking, but when we need some complex checking,
method used in a) might be better -- of course in my examples there is
only simple checking and MaskFormatter is enough.

> Also look at InputVerifier as a means to validate range and control
> focus traversal:

It looks like InputVerifier works only when loosing focus of text field.
Probably it is not possible to verify text field content every key stroke.

Regards,
Robbo


From: John B. Matthews on
In article <ho574v$cbm$1(a)nemesis.news.neostrada.pl>,
"Robbo" <nie.mam(a)yle.com> wrote:

> > I've seen DocumentListener recommended for this; it's one sure way to
> > get per-keystroke checking, and it handles the color requirement.
> > Alternatively, you might also look at MaskFormatter as a way to
> > simplify the code:
>
> Actually I used DocumentListener in my example a) and b).
> Also I used MaskFormatter in example b). MaskFormatter is great for
> simple masking, but when we need some complex checking,
> method used in a) might be better -- of course in my examples there is
> only simple checking and MaskFormatter is enough.

Oops, I drove right past that. Looking back, a) appears more flexible,
while MaskFormatter makes b) a little shorter and easier to follow.

> > Also look at InputVerifier as a means to validate range and control
> > focus traversal:
>
> It looks like InputVerifier works only when loosing focus of text field.
> Probably it is not possible to verify text field content every key stroke.

Yes, returning false from InputVerifier's shouldYieldFocus() method
effectively vetoes changing focus. Done carelessly, the field can appear
broken. Conditioning the field's tooltip text or updating a status pane
mitigates this somewhat.

May I reiterate Knute Johnson's question, "How about just using a
JSpinner?" I like the easy keyboard control.

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>