DocumentListener not working properly

1.4k Views Asked by At

im trying to code a DocumentListener in order to catch the change into a textField.

I have a panel which include another panel in which i put a textField, i have tried a lot of formulas but it didnt work

This is the panel

public JPanelTASAS() {
    initComponents();
    txtTASA.getDocument().addDocumentListener(new BecomingYellowDocumentListener(txtTASA));
}

private static class BecomingYellowDocumentListener implements DocumentListener {

    private utilesGUIx.JTextFieldCZ textField;

    public BecomingYellowDocumentListener(utilesGUIx.JTextFieldCZ textField) {
        this.textField = textField;
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        textField.setBackground(Color.yellow);
        System.out.println("Prueba");
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        textField.setBackground(Color.yellow);
        System.out.println("Prueba");
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        textField.setBackground(Color.yellow);
        System.out.println("Prueba");
    }
}

The next one is the principal Panel where the other panel is included

public JPanelTRANSMISIONES() {
    initComponents();
    anadirPaneles();
}

With this code inside initComponents

jPanelTASAS1 = new gestionTrafico.forms.JPanelTASAS();

And for the record the code of utilesGUIx.JTextFieldCZ

public JTextFieldCZ() {
    super();
    enableEvents(AWTEvent.FOCUS_EVENT_MASK);
    enableEvents(AWTEvent.KEY_EVENT_MASK);
    setDocument(createDefaultModel());
}

public void setDocument(Document doc) {
    if (doc != null) {
        doc.putProperty("filterNewlines", Boolean.TRUE);
    }
    super.setDocument(doc);
    }

Just to be clear, if if apply this listener to a JTextField of the Principal Panel it works, I think that the problem is adding a document listener to a panel inside another panel. Is it possible ?

Thank you very much in advance for your help

EDIT: I realice that if i change the value of the textfield hardcoding, the documentlistener works. But if i change the value of the textfield in the panel it doesnt.

3

There are 3 best solutions below

4
GhostCat On

Guessing: your problem is simply that just changing the background doesn't automatically trigger a repaint of the affected UI element.

In other words: you will probably see those messages on stdout; but in order for your UI to change, you should call repaint() on your frame or panel.

See here for some common solutions to common painting problems.

But given your latest comment, you are not even there yet. I guess you have to look into the details of using a DocumentListener more carefully, for example by studying this here.

1
user85421 On

Yes, it is definitively possible to add a document listener to a document of a text field located on a panel inside a second panel.

You are probably adding the listener to the wrong document and/or text field; but hard to say without seeing how you are doing it, could be something related to how the panels are constructed/added...

Example (SSCC), quick&dirty just to show that it is possible:

package test;

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class Panels {

    public static void main(String[] args) {
        new Panels();
    }

    private JFrame frame;
    private JPanel firstPanel;
    private JPanel secondPanel;

    private Panels() {
        initGUI();

        frame.setVisible(true);
    }

    private void initGUI() {
        JTextField secondField = new JTextField(20);
        secondField.getDocument().addDocumentListener(new ColoringListener(secondField));

        secondPanel = new JPanel();
        secondPanel.setBorder(new TitledBorder("Second"));
        secondPanel.add(secondField);

        JTextField firstField = new JTextField(20);
        firstField.getDocument().addDocumentListener(new ColoringListener(firstField));

        firstPanel = new JPanel();
        firstPanel.setBorder(new TitledBorder("First"));
        firstPanel.add(firstField);
        firstPanel.add(secondPanel);

        frame = new JFrame();
        frame.add(firstPanel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
    }

    private static class ColoringListener implements DocumentListener {

        private final JTextField field;

        ColoringListener(JTextField field) {
            this.field = field;
        }
        @Override
        public void insertUpdate(DocumentEvent e) {
            field.setBackground(Color.GREEN);
        }
        @Override
        public void removeUpdate(DocumentEvent e) {
            field.setBackground(Color.RED);
        }
        @Override
        public void changedUpdate(DocumentEvent e) {
        }
    }
}

EDIT: I tried to use the same structures as used in the question. I don't like to have to pass the field to the listeners constructor and to add the listener to the field - error prone!

0
AMB On

I got the answer.

The problem is that I am adding DocumentListener in the constructor, I change it and I call getDocument().addDocumentListener when the textField gain the Focus (txtTASAFocusGained).

Now it works properly.