Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- KeyListener keyListener = new KeyListener() {
- public void keyPressed(KeyEvent e) {
- jt.setText("Example");
- }
- }
- 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 TextFieldExample {
- public static void main(String[] args) {
- new TextFieldExample();
- }
- public TextFieldExample() {
- 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 ExampleExpandingDocumentFilter());
- 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);
- }
- });
- }
- public class ExampleExpandingDocumentFilter extends DocumentFilter {
- @Override
- public void insertString(FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
- System.out.println("I" + text);
- super.insertString(fb, offset, text, attr);
- }
- @Override
- public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
- if ("e".equalsIgnoreCase(text)) {
- text = "example";
- }
- super.replace(fb, offset, length, text, attrs);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement