如何使用DocumentFIlter从另一个类设置JTextField的文本?

时间:2015-03-14 21:06:11

标签: java swing getter-setter documentfilter

在以下示例中,我创建了两个texField。在写入第一个文本时,如果用户输入的空格或数字,则应在另一个textField_1上显示消息。但是一旦用户输入数字/空格,它就会给出java.lang.NullPointerException。

public class NewDemo extends JFrame {
private JPanel p1;
private JTextField textField,textField_1;
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                NewDemo frame = new NewDemo();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public NewDemo() {
    setTitle("New Demo");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 500, 500);
    p1 = new JPanel();
    p1.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(p1);
    p1.setLayout(null);

    textField = new JTextField();
    textField.setBounds(70, 71, 86, 20);
    p1.add(textField);
    textField.setColumns(10);

    JLabel lblNewLabel = new JLabel("");
    lblNewLabel.setBounds(70, 96, 86, 14);
    p1.add(lblNewLabel);

    textField_1 = new JTextField();
    textField_1.setBounds(204, 71, 86, 20);
    p1.add(textField_1);
    textField_1.setColumns(10);

     System.out.println("before calling");

     ((AbstractDocument) textField.getDocument()).setDocumentFilter(new MyDocumentFilter());

        System.out.println("AfterCalling");
}
public JTextField getTextField_1() {
    return textField_1;
}}

这是第二个类MyDocumentFilter,其中在replace方法的else块中发生了java.lang.NullPointerException错误。

class MyDocumentFilter extends DocumentFilter {
private NewDemo n1;
@Override
public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
    System.out.println("Starting: replace Method");


    for (int n = string.length(); n > 0; n--) 
        char c = string.charAt(n - 1);
        System.out.println(c);
        if (Character.isAlphabetic(c)) {
          System.out.println("In if: replace method");
            super.replace(fb, i, i1, String.valueOf(c), as);
        } else {
            System.out.println("Not allowed:BEFORE");
            n1.getTextField_1().setText("not allowed");//***HERE IS THE ERROR
            System.out.println("Not allowed:AFTER");

        }
    }
}

@Override
public void remove(FilterBypass fb, int i, int i1) throws BadLocationException {
    System.out.println("In :remove method");
    super.remove(fb, i, i1);
}

@Override
public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
   System.out.println("In: insterString Method");
    super.insertString(fb, i, string, as);
 }}

1 个答案:

答案 0 :(得分:5)

您的DocumentFilter需要对显示的NewDemo GUI对象的引用。不要像另一个人建议的那样创建一个new NewDemo实例,因为这将创建一个未显示的单独实例,而是传递适当的显示引用。

如,

  ((AbstractDocument) textField.getDocument())
        .setDocumentFilter(new MyDocumentFilter(this)); //!!

class MyDocumentFilter extends DocumentFilter {
   private NewDemo n1;

   public MyDocumentFilter(NewDemo n1) {
      this.n1 = n1;
   }

但是,也可以遵循MadProgrammer的任何建议,因为他知道他的Swing和Java后退和前锋。此外,您应该避免使用空布局并使用setBounds(...)进行组件放置,因为这会使非常不灵活的GUI虽然在一个平台上看起来不错但在大多数其他平台或屏幕分辨率上看起来很糟糕很难更新和维护。


使用MadProgrammer的建议

编辑,考虑允许您的GUI将PropertyChangeListener添加到DocumentFilter。通过这种方式,任何班级都可以监听"状态" DocumentFilter。在这里,我创建了一个名为"有效"的布尔状态。当布尔变量发生变化时,它会通知监听器。

例如:

import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;
import javax.swing.text.*;

public class NewDemo extends JPanel {
   private static final long serialVersionUID = 1L;
   private JTextField textField, textField_1;

   private static void createAndShowGui() {
      NewDemo mainPanel = new NewDemo();

      JFrame frame = new JFrame("New Demo");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }

   public NewDemo() {
      setLayout(new GridLayout(1, 0, 5, 5));
      setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      textField = new JTextField();
      textField.setColumns(10);
      add(textField);

      textField_1 = new JTextField();
      textField_1.setColumns(10);
      add(textField_1);

      MyDocumentFilter docFilter = new MyDocumentFilter(); // !!

      ((AbstractDocument) textField.getDocument()).setDocumentFilter(docFilter); // !!
      docFilter.addPropertyChangeListener(MyDocumentFilter.VALID,
            new PropertyChangeListener() {

               @Override
               public void propertyChange(PropertyChangeEvent evt) {
                  String text = ((Boolean) evt.getNewValue()) ? "Allowed"
                        : "Not Allowed";
                  textField_1.setText(text);
               }
            });
   }
}

class MyDocumentFilter extends DocumentFilter {
   public static final String VALID = "valid";
   private SwingPropertyChangeSupport pcSupport = new SwingPropertyChangeSupport(
         this);
   private boolean valid = true;

   @Override
   public void replace(FilterBypass fb, int i, int i1, String string,
         AttributeSet as) throws BadLocationException {    
      for (int n = string.length(); n > 0; n--) {
         char c = string.charAt(n - 1);
         System.out.println(c);
         if (Character.isAlphabetic(c)) {
            super.replace(fb, i, i1, String.valueOf(c), as);
            setValid(true);
         } else {
            setValid(false);
         }
      }
   }

   @Override
   public void remove(FilterBypass fb, int i, int i1)
         throws BadLocationException {
      super.remove(fb, i, i1);
   }

   @Override
   public void insertString(FilterBypass fb, int i, String string,
         AttributeSet as) throws BadLocationException {
      super.insertString(fb, i, string, as);
   }

   public boolean isValid() {
      return valid;
   }

   public void setValid(boolean valid) {
      boolean oldValue = this.valid;
      boolean newValue = valid;
      this.valid = valid;
      pcSupport.firePropertyChange(VALID, oldValue, newValue);
   }

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      pcSupport.addPropertyChangeListener(listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener listener) {
      pcSupport.removePropertyChangeListener(listener);
   }

   public void addPropertyChangeListener(String propertyName,
         PropertyChangeListener l) {
      pcSupport.addPropertyChangeListener(propertyName, l);
   }

   public void removePropertyChangeListener(String propertyName,
         PropertyChangeListener l) {
      pcSupport.removePropertyChangeListener(propertyName, l);
   }
}