Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. KeyListener keyListener = new KeyListener() {
  2. public void keyPressed(KeyEvent e) {
  3. jt.setText("Example");
  4. }
  5. }
  6.  
  7. import java.awt.EventQueue;
  8. import java.awt.GridBagLayout;
  9. import javax.swing.JFrame;
  10. import javax.swing.JTextField;
  11. import javax.swing.UIManager;
  12. import javax.swing.UnsupportedLookAndFeelException;
  13. import javax.swing.text.AbstractDocument;
  14. import javax.swing.text.AttributeSet;
  15. import javax.swing.text.BadLocationException;
  16. import javax.swing.text.DocumentFilter;
  17.  
  18. public class TextFieldExample {
  19.  
  20. public static void main(String[] args) {
  21. new TextFieldExample();
  22. }
  23.  
  24. public TextFieldExample() {
  25. EventQueue.invokeLater(new Runnable() {
  26. @Override
  27. public void run() {
  28. try {
  29. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  30. } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
  31. }
  32.  
  33. JTextField field = new JTextField(20);
  34. ((AbstractDocument)field.getDocument()).setDocumentFilter(new ExampleExpandingDocumentFilter());
  35.  
  36. JFrame frame = new JFrame("Testing");
  37. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38. frame.setLayout(new GridBagLayout());
  39. frame.add(field);
  40. frame.pack();
  41. frame.setLocationRelativeTo(null);
  42. frame.setVisible(true);
  43. }
  44. });
  45. }
  46.  
  47. public class ExampleExpandingDocumentFilter extends DocumentFilter {
  48.  
  49. @Override
  50. public void insertString(FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
  51. System.out.println("I" + text);
  52. super.insertString(fb, offset, text, attr);
  53. }
  54.  
  55. @Override
  56. public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
  57. if ("e".equalsIgnoreCase(text)) {
  58. text = "example";
  59. }
  60. super.replace(fb, offset, length, text, attrs);
  61. }
  62.  
  63. }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement