Member Avatar

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();

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

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

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

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

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

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

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

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

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

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

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



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

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.

Recommended Answers

All 15 Replies

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

printlns arent for applets?

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

Member Avatar

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

I was trying to do that with

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

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

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

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

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( 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

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, learning, and sharing knowledge.