I want to be able to input only capital letters to my GUI and so I just need the easiest way to cap/stop people from entering lowercase.
JTextField jt = new JTextField(12);
I want to be able to input only capital letters to my GUI and so I just need the easiest way to cap/stop people from entering lowercase.
JTextField jt = new JTextField(12);
Use a DocumentFilter
Take a look at Implementing a Document Filter for details and MDP's Weblog for examples
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class UppercaseTest {
public static void main(String[] args) {
new UppercaseTest();
}
public UppercaseTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JTextField field = new JTextField(20);
((AbstractDocument)field.getDocument()).setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
string = string.toUpperCase();
super.insertString(fb, offset, string, attr);
}
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
text = text.toUpperCase();
super.replace(fb, offset, length, text, attrs);
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(field);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Try the following:
inputValue.toUpperCase();
This way you will capitalize everything before processing the data.
DocumentFilter
Do Like this example
DocumentFilter dfilter = new UpcaseFilter();
JTextField jt = new JTextField();
((AbstractDocument) jt.getDocument()).setDocumentFilter(dfilter);
One more example of DocumentFilter
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class Main extends JFrame {
public Main() throws HeadlessException {
setSize(200, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
DocumentFilter filter = new UppercaseDocumentFilter();
JTextField firstName = new JTextField(10);
((AbstractDocument) firstName.getDocument()).setDocumentFilter(filter);
JTextField lastName = new JTextField(10);
((AbstractDocument) lastName.getDocument()).setDocumentFilter(filter);
add(firstName);
add(lastName);
}
public static void main(String[] args) {
new Main().setVisible(true);
}
}
class UppercaseDocumentFilter extends DocumentFilter {
public void insertString(DocumentFilter.FilterBypass fb, int offset, String text,
AttributeSet attr) throws BadLocationException {
fb.insertString(offset, text.toUpperCase(), attr);
}
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text,
AttributeSet attrs) throws BadLocationException {
fb.replace(offset, length, text.toUpperCase(), attrs);
}
}
You create a JTexfield, then you need add the event keyTyped and add this code:
JTextField txtFirstName = new JTextField();
txtFirstName.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent arg0) {
char charecter = arg0.getKeyChar();
if (Character.isLowerCase(charecter)) {
arg0.setKeyChar(Character.toUpperCase(charecter));
}
}
});