Search code examples
javaswinguser-interfaceflowlayout

Swing GUI and translator issues


So I have a class project where I have to create a translator where a user can enter in text, in English, hit a button, and get the Pig Latin translation.I have to use Swing GUI, FlowLayout, and JTextArea, with a separate JTextArea for the translation. My program brings up the GUI, it allows me to type in something, but when I hit the button, nothing happens. I looked through and it looks like I remembered to add everything to the JFrame, but I can't figure out why nothing is popping up.

// This program will take text that is English and translate it to Pig Latin


//import all the necessary packages for the program
import java.util.Scanner;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

public class PigLatin extends JFrame implements ActionListener{

public static final int WIDTH = 500;
public static final int HEIGHT= 400;
private JButton button;
private JTextArea original;
private JTextArea translation;
private JLabel english;
private JLabel pig;

public static void main(String[]args)
{
    PigLatin gui = new PigLatin();
    gui.setVisible(true);
    /*
    Scanner scan = new Scanner("far");
    Scanner scan1 = new Scanner("and");
    Scanner scan2 = new Scanner("away");

    while (scan.hasNext())
    {
        //prints out original words
    System.out.println(scan.next());
    System.out.println(scan1.next());
    System.out.println(scan2.next());
    } 

*/  
}

public PigLatin()
{   //titles the box, creates size and what to do when 'x' is clicked
    super("Pig latin translator");
    this.setSize(WIDTH,HEIGHT);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());
    this.setVisible(true);

    //creates frame for everything to be added to 
    JPanel frame = new JPanel();

    //creates labels for text boxes
    english = new JLabel("English");
    add(english);


    //enters in origninal text
    original = new JTextArea("Enter text here");
    original.setEditable(true);
    original.setLineWrap(true);

    //adds the text area to the GUI
    frame.add(original);


    //create text area for translation
    translation = new JTextArea();
    translation.setEditable(false);
    translation.setLineWrap(true);

    //adds the text area to the GUI
    frame.add(translation);

    //adds the frame to the GUI
    add(frame);
    frame.setVisible(true);

    //creates panel for the button
    JPanel buttons = new JPanel();
    buttons.setLayout(new FlowLayout());

    //create translate button
    button = new JButton("TRANSLATE TO PIG LATIN");
    button.addActionListener(this);

    buttons.add(button);

    JButton clear = new JButton("Clear");

    buttons.add(clear);

    //adds the buttons JPanel to the GUI
    add(buttons);
}
private int findWordEnd(String text) {

       int indexOfSpace = text.indexOf(" ");

       if (indexOfSpace == -1)  // Happens if there is no space
           return text.length();
       else
           return indexOfSpace;
    }

private int findVowel(String vowel)
{
    int i;
    //looks for vowel
    for(i=0; i<vowel.length();i++)
    {
        if(vowel.charAt(i)=='a'||vowel.charAt(i)=='e'||vowel.charAt(i)=='i'||vowel.charAt(i)=='o'||vowel.charAt(i)=='u')
            return i;
    }
    //if no vowels
    return vowel.length();
}

private String englishToPig(String text)
{
    String translate = "";

    while(!text.equals(""))
    {   
        //finds the first word
        int wordEndIndex = findWordEnd(text);
        String word = text.substring(0, wordEndIndex);

        //finds the start and end in Pig Latin
        int vowelIndex = findVowel(word);
        String start = word.substring(vowelIndex);
        String end = "-"+word.substring(0, vowelIndex)+"ay";

        //creates the translation
        translate = translate+start+end;
        //gets rid of first word, so translation can continue
        text = text.substring(wordEndIndex).trim();
    }
    return translate;
}
public void actionPerformed(ActionEvent e){
    if (e.getSource() == "TRANSLATE TO PIG LATIN") {

        // Get the English they entered
        String text = original.getText();
        text = text.trim();
        text = text.toLowerCase();

        // Display the translation
        translation.setText(englishToPig(text));
    }
}

}

Solution

  • The source of the click event is the button. What you're interested in is the action command instead.

    if (e.getActionCommand().equals("TRANSLATE TO PIG LATIN")) {
    

    Also note equals() instead of ==. However, it's usually needless to use the same action listener for everything and then figure out between the sources in the listener. You could as well use an anonymous class, when the source is guaranteed to be the one you're interested in:

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // Get the English they entered
            String text = original.getText();
            text = text.trim();
            text = text.toLowerCase();
    
            // Display the translation
            translation.setText(englishToPig(text));
        }
    });
    

    The clear button can then similarly have its own, as their functionality is not really related.