Search code examples
javaswingactionlistenerjtextfielddocumentlistener

Modify multiple JTextFields with a DocumentListener


This is my very first java swing application in school. We had to do a caesar coder.(http://en.wikipedia.org/wiki/Caesar_cipher)

It should know to crypt and decrypt and it should do it automaticly even if you don't press the button. (FocusListener)

It works like a dream, until I only have to do it with the "Code!" button. Then we had to add a DocumentListener, which I created, as shown below. It works if I use it only to crypt or only to decrypt. The problem occurs, when I add both JTextField to the same DocumentListener. I get an IllegalStateException with an "AWT-EventQueue-0" label. It calls the setText function two times, and the second one is fatal. I think it is because if I type a character to the first JTextField it is an insert, then when I set the content of the second JTextField with setText it is an insert too. Our prof said it can be fixed easily with a flag, but I'm failing. Can you help me please?

Application.java

    package javaswinglabor;

    public class Application {
        public static void main(String[] args) {
        CaesarFrame caesarFrame = new CaesarFrame();
        }
    }

CaesarProgram.java

package javaswinglabor;

public class CaesarProgram {

    /**
     * Caesar kodolo
     * @param input
     * @param offset
     * @return kod
     */
    static String caesarCoder(String input, char offset){
        input = input.toUpperCase();
        String kod="";
        for(int i=0; i < input.length(); i++){
        if(input.charAt(i) >= 'A' && input.charAt(i) <= 'Z' ) {
            kod += (char)(65 + ((input.charAt(i) - 'A' + offset - 'A')%26) );
        }
        }
        return kod;
    }

    /**
     * Caesar dekodolo
     * @param input
     * @param offset
     * @return kod
     */
    static String caesarDecoder(String input, char offset){
    input = input.toUpperCase();
    String kod="";
    for(int i=0; i < input.length(); i++){
        if(input.charAt(i) >= 'A' && input.charAt(i) <= 'Z' ) {
        kod += (char)(65 + (input.charAt(i) - offset + 26)%26 );
        }
    }
    return kod;
    }
}

CaesarFrame.java

 package javaswinglabor;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class CaesarFrame extends JFrame{



    //TOP JPanel
    private JComboBox cb;
    private JTextField t1;
    private JButton b;

    //DOWN JPanel
    private JTextField t2;

    //encrypt/decrypt
    private boolean encrypt;



    public CaesarFrame() throws HeadlessException {
    super("SwingLab");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(400, 110);
    this.setResizable(true);
    this.setLayout(new GridLayout(2, 3));

    JPanel pFelso = new JPanel(new FlowLayout(FlowLayout.LEFT));
    JPanel pAlso = new JPanel(new FlowLayout(FlowLayout.LEFT));
    this.add(pFelso);
    this.add(pAlso);

    //TOP Jpanel
    cb = new JComboBox(fillObject());
    t1 = new JTextField(20);
    b = new JButton("Code!");
    pFelso.add(cb);
    pFelso.add(t1);
    pFelso.add(b);

    //DOWN JPanel
    t2 = new JTextField(20);
    //t2.setEditable(false);
    pAlso.add(new JLabel("Output:"));
    pAlso.add(t2);

    //ButtonListener
    OkButtonActionListener obl = new OkButtonActionListener();
    b.addActionListener(obl);

    //DocumentListeners
    CoderDocumentFilter cdf = new CoderDocumentFilter();
    DecoderDocumentFilter ddf = new DecoderDocumentFilter();
    ((AbstractDocument)(t1.getDocument())).setDocumentFilter((cdf));
    ((AbstractDocument)(t2.getDocument())).setDocumentFilter((ddf));

    //InputFieldKeyListener ifkl = new InputFieldKeyListener();
    //t1.addKeyListener(ifkl);

    //SpecialDocumentListener t1_dl = new SpecialDocumentListener();
    //t1.getDocument().addDocumentListener(t1_dl);
    //t2.getDocument().addDocumentListener(t1_dl);

    //FocusListener
    JTextFieldFocusListener jtfl = new JTextFieldFocusListener();
    t1.addFocusListener(jtfl);
    t2.addFocusListener(jtfl);

    this.pack();
    this.setVisible(true);
    }



    private void Coder(){
    Character c = (Character)cb.getSelectedItem();
    t2.setText( CaesarProgram.caesarCoder(t1.getText(), c.charValue()) );
    }
    private void Decoder(){
    Character c = (Character)cb.getSelectedItem();
    t1.setText( CaesarProgram.caesarDecoder(t2.getText(), c.charValue()) );
    }



    private class CoderDocumentFilter extends DocumentFilter{
    @Override public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
        super.insertString(fb, offset, text.toUpperCase(), attr);
        if (encrypt) {Coder();}
    }
    @Override public void replace (DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr) throws BadLocationException {
        super.replace(fb, offset, length, text.toUpperCase(), attr);
        if (encrypt) {Coder();}
    }
    @Override public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException{
        super.remove(fb, offset, length);
        if (encrypt) {Coder();}
    }
    }



    private class DecoderDocumentFilter extends DocumentFilter{
    @Override public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
        super.insertString(fb, offset, text.toUpperCase(), attr);
        if (!encrypt) {Decoder();}
    }
    @Override public void replace (DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr) throws BadLocationException {
        super.replace(fb, offset, length, text.toUpperCase(), attr);
        if (!encrypt) {Decoder();}
    }
    @Override public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException{
        super.remove(fb, offset, length);
        if (!encrypt) {Decoder();}
    }
    }



    /**
     * Egy 26-elemu Object tombot tolt fel az angol ABC nagybetuivel.
     * @return object[]
     */
    private static Object[] fillObject() {
    Object[] object = new Object[26];
    for(int i=0; i < 26; i++){
        object[i] = (char)(i+65);
    }
    return object;
    }



    //Button listener
    private class OkButtonActionListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent ae) {
        if(encrypt){Coder();}
        else{Decoder();}
    }
    }



    //FocusListener
    private class JTextFieldFocusListener implements FocusListener {

    @Override public void focusGained(FocusEvent fe) {
        if(t1.isFocusOwner()) {encrypt = true;}
        else if(t2.isFocusOwner()){encrypt = false;}
    }

    @Override public void focusLost(FocusEvent fe) {}
    }



    //Previous task
    /*
    private class InputFieldKeyListener extends KeyAdapter {
    @Override public void keyPressed(KeyEvent ke) {
        super.keyPressed(ke);
        Character c = (Character)cb.getSelectedItem();
        String s = String.valueOf(ke.getKeyChar());
        t2.setText(t2.getText() + CaesarProgram.caesarCoder(s, c.charValue()) );
    }
    }
    */
}

I solved the problem, thank you all, for your useful help. I'm sure it isn't the most elegant or the fastest solution, but it works finally. I would be glad, if somebody would like to make it more efficent or easier/cleaner.


Solution

  • From How to Write a Document Listener:

    Document listeners should not modify the contents of the document; The change is already complete by the time the listener is notified of the change.

    To fix this problem, use a DocumentFilter rather than a DocumentListener.

    See this example Implementing a Document Filter