This project is similar to, but not exactly like, my phonetic project. I am having to construct a GUI and have done so. I have been able to get everything except one part.

What I need it to do is:
1) User inputs a string (this part works)
2) When user clicks button "Code It" it needs to convert the string into Morse code. (this part is what isn't working)

I don't know how to set it up to do it. I'm trying to use an array to do it.

What it does:
1) Takes user's input
2) When user clicks button "Code It" it just displays the string they entered.

Any ideas and help will be appreciated.

package morsecode;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MorseCode
    public static class morseCode implements ActionListener
        JFrame frame;
        JPanel contentPane;
        JTextField words;
        JButton CodeIt;
        JLabel enterString, coded;

        public morseCode()
            // Create and set up the frame
            frame = new JFrame("Morse Code");

            // Create a content pane with a BoxLayout
            // and empty borders
            contentPane = new JPanel();
            contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 100));
            contentPane.setLayout(new GridLayout(4,2,5,10));

            // Create a text field and a descriptive label
            enterString = new JLabel("Enter string: ");

            words = new JTextField(50);

            // Create a display morseCode button
            CodeIt = new JButton("Code It");

            // Create a label that will display text in morse code
            coded = new JLabel(" ");

            // Add content pane to frame

            // Size and then diplay the frame

//////////// I think this is the section where I need to put my array
//////////// set up since it retrieves the user input and I believe
//////////// displays it
        public void actionPerformed(ActionEvent event)
            String text = words.getText();
//            char[] stringArray = text.toCharArray();
//            String[] morse = {".- ","-... ","-.-. ","-.. ",". ","..-. ","--. ",".... ",".. ",".--- ","-.- ",".-.. ","-- ","-. ","--- ",".--. ","--.- ",".-. ","... ","- ","..- ","...- ",".-- ","-..- ","-.-- ","--.. "};

    private static void runGUI()

        morseCode greeting = new morseCode();

    public static void main(String[] args)
        javax.swing.SwingUtilities.invokeLater(new Runnable()
            public void run()

Recommended Answers

All 3 Replies

What java version are you using?
If you are using 1.5 for example then you can map the letters with the morse code using a hashmap:

HashMap<Character,String> map = HashMap<Character,String>(26);

map.put('a', ".-");
map.put('b', "-...");

String text = words.getText().toLowerCase();
char[] stringArray = text.toCharArray();

Then you can loop through the array and use the char as the key to get the String value from the map.

String result="";
result += map.get(stringArray[i]);

Write new class


Place in it your array

String[] morse

Start from single char. Get result.
Later use array of char or String.
Alternatively you can do all work inside single static method.

I'm using NetBeans IDE 6.7.1
Not sure if that means much in the way of what version I'm using.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.