In the following example I have created two texFields. While writing on first text filed if the space or number typed by user then it should show message on another textField_1. But once user entered number/space it giving 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;
}}
Here is the second class MyDocumentFilter where the java.lang.NullPointerException error occurring in the else block of replace method.
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);
}}
Your DocumentFilter needs a reference to the displayed NewDemo GUI object. Don't just create a new NewDemo
instance as another has suggested, since that will create a non-displayed separate instance, but rather pass in the appropriate displayed reference.
e.g.,
((AbstractDocument) textField.getDocument())
.setDocumentFilter(new MyDocumentFilter(this)); //!!
and
class MyDocumentFilter extends DocumentFilter {
private NewDemo n1;
public MyDocumentFilter(NewDemo n1) {
this.n1 = n1;
}
But yeah, also follow any recommendation from MadProgrammer as he knows his Swing and Java backwards and forwards. Also You should avoid use of null layout and use of setBounds(...)
for component placement as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain.
Edit using MadProgrammer's suggestion, consider allowing your GUI to add PropertyChangeListener to the DocumentFilter. This way, any class can listen for changes in the "state" of the DocumentFilter. Here I've created a boolean state called "valid" that notifies listeners any time the boolean variable changes.
For example:
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);
}
}