Najpierw przykład zastosowania:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JSumator extends JFrame implements ActionListener
{
private JInputInteger pierwsza,druga,trzecia;
private JInputInteger suma=new JInputInteger();
//------------------------
public static void main(String args[])
{
new JSumator();
}
//------------------------
public JSumator()
{
super("Wprowadzanie liczb całkowitych");
setLayout(new BorderLayout());
JButton przycisk=new JButton("Sumowanie");
przycisk.setBackground(Color.lightGray);
przycisk.addActionListener(this);
add("North",przycisk);
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(4,2,10,10));
add("Center",panel);
JLabel label=new JLabel("Wpisz dowolną liczbę całkowitą");
panel.add(label);
pierwsza=new JInputInteger();
pierwsza.addActionListener(this);
panel.add(pierwsza);
label=new JLabel("Wpisz liczbę z zakresu od 0 do 88");
panel.add(label);
druga=new JInputInteger(0,88);
druga.addActionListener(this);
panel.add(druga);
label=new JLabel("Wpisz liczbę z zakresu od -100 do 100");
panel.add(label);
trzecia=new JInputInteger(-100,100);
trzecia.addActionListener(this);
panel.add(trzecia);
panel.add(new JLabel("Suma"));
suma.setEditable(false);
panel.add(suma);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
pierwsza.setFocus();
}
//------------------------
public void actionPerformed(ActionEvent ae)
{
int s=0;
s+=pierwsza.getValue();
s+=druga.getValue();
s+=trzecia.getValue();
suma.setText(""+s);
}
}
Teraz klasa JInputInteger
/* Klasa usługowa do wprowadzania liczb całkowitch (typu int).
* Liczby są wprowadzane w polu tekstowym umieszonym na panelu.
* Panel zmienia kolor w zależności od tego czy pole tekstowe
* ma fokus, czy wpisany ciąg jest liczbą całkowitą i czy ta
* liczba mieści się w podanym zakresie.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class JInputInteger extends JPanel
{
private JTextField tf;
private int gr_dolna,gr_gorna,szer=12;
private Color nofocus=new Color(211,211,211);
private Color focus=new Color(203,203,250);
private Color error=new Color(235,176,105);
//------------------------
public JInputInteger(int gr_dolna,int gr_gorna,Color nofocus,Color focus,Color error,String startValue)
{
super();
this.gr_dolna=gr_dolna;
this.gr_gorna=gr_gorna;
this.nofocus=nofocus;
this.focus=focus;
this.error=error;
setBackground(nofocus);
tf=new JTextField(startValue,szer);
tf.setEditable(true);
JIntControler sprawdzacz=new JIntControler(gr_dolna,gr_gorna,nofocus,focus,error);
tf.addFocusListener(sprawdzacz);
AbstractDocument doc=(AbstractDocument) tf.getDocument();
doc.setDocumentFilter(new IsInteger(gr_dolna,gr_gorna,nofocus,focus,error,tf));
add(tf);
}
//------------------------
public JInputInteger(int gr_dolna,int gr_gorna,Color nofocus,Color focus,Color error)
{
this(gr_dolna,gr_gorna,nofocus,focus,error,"");
}
//------------------------
public JInputInteger(int gr_dolna,int gr_gorna)
{
this(gr_dolna,gr_gorna,new Color(211,211,211),new Color(203,203,250),new Color(235,176,105),"");
}
//------------------------
public JInputInteger(int gr_dolna,int gr_gorna,String startValue)
{
this(gr_dolna,gr_gorna,new Color(211,211,211),new Color(203,203,250),new Color(235,176,105),startValue);
}
//------------------------
public JInputInteger()
{
this(Integer.MIN_VALUE,Integer.MAX_VALUE);
}
//------------------------
public int getValue()
{
String txt=tf.getText();
if (txt.equals(""))
{
return 0;
}
else
{
int i=0;
try
{
i=Integer.parseInt(txt);
}
catch (NumberFormatException e)
{
}
return i;
}
}
//------------------------
public void addActionListener(ActionListener al)
{
tf.addActionListener(al);
}
//------------------------
public void addKeyListener(KeyListener kl)
{
tf.addKeyListener(kl);
}
//------------------------
public void setEditable(boolean isEditable)
{
tf.setEditable(isEditable);
}
//------------------------
public void setFocus()
{
tf.requestFocus();
}
//------------------------
public void requestFocus()
{
tf.requestFocus();
}
//------------------------
public void setText(String s)
{
tf.setText(s);
}
//------------------------
public String getText()
{
return tf.getText();
}
//------------------------
public void selectAll()
{
tf.selectAll();
}
//------------------------
public void setActionCommand(String cmd)
{
tf.setActionCommand(cmd);
}
}
//------------------------------------------------
class JIntControler implements FocusListener
{
private int gr_dolna,gr_gorna;
private Color nofocus,focus,error;
//------------------------
JIntControler(int grd,int grg,Color nofocus,Color focus,Color error)
{
gr_dolna=grd;
gr_gorna=grg;
this.nofocus=nofocus;
this.focus=focus;
this.error=error;
}
//------------------------
public void focusGained(FocusEvent fe)
{
JTextField tf=(JTextField) fe.getSource();
Container cont=tf.getParent();
if (!isGood(tf))
{
cont.setBackground(error);
}
else
{
cont.setBackground(focus);
}
cont.repaint();
}
//------------------------
public void focusLost(FocusEvent fe)
{
JTextField tf=(JTextField) fe.getSource();
Container cont=tf.getParent();
if (!isGood(tf))
{
cont.setBackground(error);
}
else
{
cont.setBackground(nofocus);
}
cont.repaint();
}
//------------------------
private boolean isGood(JTextField tf)
{
String old=tf.getText();
int i=0;
Container cont=tf.getParent();
if (old.equals(""))
{
if(0>=gr_dolna && 0<=gr_gorna)
{
return true;
}
else
{
cont.getToolkit().beep();
cont.setBackground(error);
cont.repaint();
tf.requestFocus();
return false;
}
}
try
{
i=Integer.parseInt(old);
}
catch (NumberFormatException e)
{
cont.getToolkit().beep();
cont.setBackground(error);
cont.repaint();
tf.requestFocus();
return false;
}
if (i>=gr_dolna && i<=gr_gorna)
{
return true;
}
else
{
cont.getToolkit().beep();
cont.setBackground(error);
cont.repaint();
tf.requestFocus();
return false;
}
}
}
Jeszcze klasa IsInteger wykorzystywana przy kontroli co jest kopiowane ze schowka
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
public class IsInteger extends DocumentFilter
{
private int minValue;
private int maxValue;
private Color nofocus;
private Color focus;
private Color error;
private JTextField tf;
//------------------------
public IsInteger(int minValue,int maxValue,Color nofocus,Color focus,Color error,JTextField tf)
{
this.minValue=minValue;
this.maxValue=maxValue;
this.nofocus=nofocus;
this.focus=focus;
this.error=error;
this.tf=tf;
}
//------------------------
private boolean isOk(String s)
{
int i=0;
if (s.equals(""))
{
return true;
}
try
{
i=Integer.parseInt(s);
}
catch (NumberFormatException e)
{
return false;
}
if (i<minValue || i>maxValue)
{
return false;
}
return true;
}
//------------------------
public void insertString(FilterBypass fb,int offs,String str,AttributeSet a)
throws BadLocationException
{
super.insertString(fb,offs,str,a);
skontroluj();
}
//------------------------
public void replace(FilterBypass fb,int offs,int length,String str,AttributeSet a)
throws BadLocationException
{
super.replace(fb,offs,length,str,a);
skontroluj();
}
//------------------------
public void remove(FilterBypass fb,int offs,int length) throws BadLocationException
{
super.remove(fb,offs,length);
skontroluj();
}
//------------------------
private void skontroluj()
{
Container parent=tf.getParent();
String s=tf.getText();
if (isOk(s))
{
parent.setBackground(focus);
}
else
{
parent.setBackground(error);
}
}
}