// UpperCaseTextField.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class UpperCaseTextField extends JFrame
{
private JTextField upperCaseTextField;
public UpperCaseTextField()
{
Container container = getContentPane();
container.setLayout(new FlowLayout());
upperCaseTextField = new JTextField(15);
upperCaseTextField.addKeyListener(new UpperCaseTextFieldHandler());
container.add(upperCaseTextField);
setSize(200, 100);
setVisible(true);
}
class UpperCaseTextFieldHandler implements KeyListener
{
public void keyPressed(KeyEvent ke)
{
}
public void keyReleased(KeyEvent ke)
{
}
public void keyTyped(KeyEvent ke)
{
if(!Character.isUpperCase(ke.getKeyChar()))
ke.consume();
}
}
public static void main(String args[])
{
UpperCaseTextField application = new UpperCaseTextField();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}