Search code examples
javaswingif-statementjtextfieldmorse-code

How to set list of symbles converted to alphabet?


I'm making a system to convert Morse cord to English alphabet. I'm using JTextfeild called "write" to type text and another JTextfeild call "View" to view which is typed on write.

But I can only set one Morse cord for one time.

As an example if I type A on "Write" textfeild it it is only printing ".-" . And when I type "B" again then view textfeild set "-..." . I want to print number of letters.

Given below is my source cord.

private void writeKeyReleased(java.awt.event.KeyEvent evt) {                                  

    if (evt.getKeyCode() == 65) {
        view.setText(".-");
    } else if (evt.getKeyCode() == 66) {
        view.setText("-...");
    } else if (evt.getKeyCode() == 67) {
        view.setText("-.-.");
    } else if (evt.getKeyCode() == 68) {
        view.setText("-..");
    } else if (evt.getKeyCode() == 69) {
        view.setText(".");
    } else if (evt.getKeyCode() == 70) {
        view.setText("..-.");
    } else if (evt.getKeyCode() == 71) {
        view.setText("--.");
    } else if (evt.getKeyCode() == 72) {
        view.setText("....");
    } else if (evt.getKeyCode() == 73) {
        view.setText("..");
    } else if (evt.getKeyCode() == 74) {
        view.setText(".---");
    } else if (evt.getKeyCode() == 75) {
        view.setText(".-.-");
    } else if (evt.getKeyCode() == 76) {
        view.setText(".-..");
    } else if (evt.getKeyCode() == 77) {
        view.setText("--");
    } else if (evt.getKeyCode() == 78) {
        view.setText("-.");
    } else if (evt.getKeyCode() == 79) {
        view.setText("---");
    } else if (evt.getKeyCode() == 80) {
        view.setText(".--.");
    } else if (evt.getKeyCode() == 81) {
        view.setText("--.-");
    } else if (evt.getKeyCode() == 82) {
        view.setText(".-.");
    } else if (evt.getKeyCode() == 83) {
        view.setText("...");
    } else if (evt.getKeyCode() == 84) {
        view.setText("-");
    } else if (evt.getKeyCode() == 85) {
        view.setText("..-");
    } else if (evt.getKeyCode() == 86) {
        view.setText("...-");
    } else if (evt.getKeyCode() == 87) {
        view.setText(".--");
    } else if (evt.getKeyCode() == 88) {
        view.setText("-..-");
    } else if (evt.getKeyCode() == 89) {
        view.setText("-.--");
    } else {
        view.setText("--..");
    }

}         

Solution

  • I'm making a system to convert Mose cord to English alphabet. I'm using jtextfeild called "write" to type text and another jtextfeild call "View" to view which is typed on write.

    • use DocumentListener for JTextComponents instead of low_level KeyListener, otheriwise you can't be able to input sequence of chars from (for example) Ctrl+C (SystemClipBoard), or remove selected chars, then output to anther JComponents freeze without any, no changes are made, because KeyListener can firing an Event from single Char only

    • plus you can use DocumentFilter in the case that you want to replace, remove, modify single char or chars sequence typed by user into JTextField

    for example

    enter image description here

    import java.awt.GridLayout;
    import javax.swing.*;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    
    public class TextLabelMirror {
    
        private JPanel mainPanel = new JPanel();
        private JTextField field = new JTextField(20);
        private JTextField field1 = new JTextField(20);
    
        public TextLabelMirror() {
            field.getDocument().addDocumentListener(new DocumentListener() {
    
                @Override
                public void changedUpdate(DocumentEvent e) {
                    updateLabel(e);
                }
    
                @Override
                public void insertUpdate(DocumentEvent e) {
                    updateLabel(e);
                }
    
                @Override
                public void removeUpdate(DocumentEvent e) {
                    updateLabel(e);
                }
    
                private void updateLabel(DocumentEvent e) {
                    java.awt.EventQueue.invokeLater(new Runnable() {
    
                        @Override
                        public void run() {
                            field1.setText(field.getText());
                        }
                    });
                }
            });
    
            mainPanel.setLayout(new GridLayout(1, 0, 10, 0));
            mainPanel.add(field);
            mainPanel.add(field1);
        }
    
        public JComponent getComponent() {
            return mainPanel;
        }
    
        private static void createAndShowUI() {
            JFrame frame = new JFrame("TextLabelMirror");
            frame.getContentPane().add(new TextLabelMirror().getComponent());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    createAndShowUI();
                }
            });
        }
    }