Member Avatar for loserspearl

Im writing the Applet GUI code that uses another class to translate a string into pig latin (dubbed PigLatin.class) My gui has two text areas, a few labels, and two buttons, but I'm having some trouble with event handling.

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

public class TxtCrypt extends JApplet implements ActionListener {
    LayoutManager lo;
    JButton encBtn, clrBtn;
    JTextArea preCrypTxt, postCrypTxt;
    JLabel titleLbl, infoLbl, plainLbl, ciphLbl;
    String inStr, outStr;

//    public TxtCrypt()  {
   
//    }

    public void init () {
        lo = new FlowLayout();
        setLayout(lo);
        setBackground(Color.BLUE);

        titleLbl = new JLabel("Text Encryption Program");
        titleLbl.setFont(new Font("SansSeriff", Font.BOLD, 20));
        titleLbl.setForeground(Color.white);

        infoLbl = new JLabel("\u00A9 McGalliger Manufacturing");
        infoLbl.setFont(new Font("SansSeriff", Font.BOLD, 16));
        infoLbl.setForeground(Color.white);      

        plainLbl = new JLabel("Plaintext: ");
        plainLbl.setFont(new Font("SansSeriff", Font.BOLD, 12));
        plainLbl.setForeground(Color.white);

        ciphLbl = new JLabel("Ciphertext: ");
        ciphLbl.setFont(new Font("SansSeriff", Font.BOLD, 12));
        ciphLbl.setForeground(Color.white);

        encBtn = new JButton("Encrypt");
        encBtn.setActionCommand("encAct");
        encBtn.setToolTipText("Enter and ecrypt text.");

        clrBtn = new JButton("Clear");
        clrBtn.setActionCommand("clrAct");
        clrBtn.setToolTipText("Clear the plain and ecrypted text.");

        preCrypTxt = new JTextArea("Enter your text to encrypt", 5, 35);
        preCrypTxt.setLineWrap(true);
        preCrypTxt.setWrapStyleWord(true); 
   
        postCrypTxt = new JTextArea(5, 35);
        postCrypTxt.setLineWrap(true);
        postCrypTxt.setWrapStyleWord(true);

        JPanel title = new JPanel();
            title.setLayout(new BorderLayout());
            title.setBackground(Color.BLUE);
            title.add(titleLbl, BorderLayout.CENTER);         

        JPanel top = new JPanel();
            top.setLayout(new BorderLayout());
            top.setBackground(Color.BLUE);
            top.add(plainLbl, BorderLayout.NORTH);
            top.add(preCrypTxt, BorderLayout.CENTER);


        JPanel mid = new JPanel();
            mid.setLayout(new BorderLayout());
            mid.setBackground(Color.BLUE);
            mid.add(encBtn, BorderLayout.WEST);
            mid.add(clrBtn, BorderLayout.EAST); 


        JPanel bot = new JPanel();
            bot.setLayout(new BorderLayout());
            bot.setBackground(Color.BLUE);
            bot.add(ciphLbl, BorderLayout.NORTH);
            bot.add(postCrypTxt, BorderLayout.CENTER);
            bot.add(infoLbl, BorderLayout.SOUTH);

        add(title);
        add(top);
        add(mid);
        add(bot);

        encBtn.addActionListener(this);
        clrBtn.addActionListener(this);

    }
     
    public void actionPerformed (ActionEvent e) {
            
        if ("encAct".equals(e.getActionCommand())) {
            String inStr = preCrypTxt.getText();
        //use PigLatin.encrypt         
            postCrypTxt.setText(outStr);
        }
        else if("clrAct".equals(e.getActionCommand())) {
             preCrypTxt.setText("");    
             postCrypTxt.setText("");
        }
        else {
        //error
        }
    }    
}

I'm need to take the text from the first textarea pass it to the PigLatin class into the encrypt method which will return a string and add that new string to the second text area. Thats the basis of the project, not sure why the applet frames background isn't blue(setBackground) nor clear button isnt working. I would also like if when users click and change focus to the textarea it would remove the starting text, but Im new to event handling.

clear button isnt working.

Add some debugging printlns to your code to show the values of the variables as they are set and changed.

Member Avatar for loserspearl

printlns arent for applets?

They work fine for me. The output goes to the browser's java console.

Member Avatar for loserspearl

I didnt recompile or change anything so idk why clear works now. One step down, still trying to figure out how to pass a string to the other class's 'encrypt method', and get all my background to be solid blue.

how to pass a string to the other class's 'encrypt method'

Get a reference to the object and call a method.
One way is to create a new instance of the class and save the reference in a class variable.

Member Avatar for loserspearl

I was trying to do that with

if ("encAct".equals(e.getActionCommand())) {
            inStr = preCrypTxt.getText();
	   outStr = PigLatin.encrypt(inStr);         
            postCrypTxt.setText(outStr);
        }

Not sure how else if would be coded

Did the code work?
If there were errors, you need to post the full text of the error messages if you want help.

Member Avatar for loserspearl

Nothing happened on the applet, no compiler errors with that code, I can normally figure those out.

Nothing happened on the applet,

Please explain.
Add printlns to the code to show the execution flow.
If nothing prints out, then there is something wrong with the way you are executing the code.

Member Avatar for loserspearl

Well again after not changing anything the translation code works (as if my compiler is hanging for a few minutes)

How do you use printlns on a gui applet?

I still don't know how to get my background to be a solid blue, its using the standard 'metal' java background color even with my setbackground

How do you use printlns on a gui applet?

There is no difference in usage between console and GUI.

Member Avatar for loserspearl

Again a few minutes after compiling my code starts working after not changing anything(as if my compiler is hanging)

How do you use printlns in a gui applet?

I also cannot get my applets background to be solid blue (even after using a setBackground(Color.blue)) it only uses the default 'metal' java look

eek my internet is dying and double posting! TY so far

How do you use printlns in a gui applet?

It prints to the java console.

Member Avatar for loserspearl

I'm not using any IDE like ecliplse just cmd and notepad, so no console to print to when using HTML to display applet. Do you know why my background wont change color?

No ideas on your background.

using HTML to display applet.

What program displays the applet?
If its a browser, look in the browser's java console.
It looks like this: You can see some of my program's printlns at the bottom of the window

Be a part of the DaniWeb community

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