How can i Implement a text based app into a GUI interface?

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2252 Views - Last Post: 16 October 2011 - 01:32 PM Rate Topic: -----

#1 Gamme40   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 16-October 11

How can i Implement a text based app into a GUI interface?

Posted 16 October 2011 - 04:00 AM

Hello Guys,

I have two programs that im trying to put together, i managed to create a cipher ceaser program, and a cipher GUI program, the thing is i want to implement the cipher ceaser program into the GUI based app, i'm a new JAVA programmer and i need some help when it comes to this kind of complexity! please assist in any way possible! I tried to put the one coding from the ceaser cipher into the if statement actionListener and i tried to create new variables for the GUI but to no avail it never work, please i need some assistance here.. Below is both my apps codings.

Ceaser Cipher Simple program
import java.io.*;
import javax.swing.*;
public class CaesarCipher
{
    public static void main (String args[])
    {
        int shift = Integer.parseInt (JOptionPane.showInputDialog ("Enter Key here"));
        String msg = JOptionPane.showInputDialog ("Enter message here");
        char character;

        System.out.print ("Encrypted sentence is: ");

        for (int i = 0 ; i < msg.length () ; i++)
        {
            //gets the characters in the message typed
            character = msg.charAt (i);
            
                //if statement to check if character is a 'space'
                if (character != ' ')
                {
                    //the shift
                    character = (char) ('a' + (character - 'a' + shift) % 26); 
                }
            System.out.print (character);
            
        }
        
        System.out.println ();
        System.out.println ("Decrypted sentence is: " + msg);
    }
}



here is the Ceaser cipher GUI based Application that doesnt work yet :(
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class CaeserCipherGUI extends JFrame implements ActionListener
{
    JButton encr;
    JButton decr;
    JPanel pane = new JPanel ();
    CaeserCipherGUI ()
    {
        super ("Caeser");
        setBounds (200, 200, 200, 200);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setResizable (false);
        Container con = this.getContentPane ();
        con.add (pane);

        JButton encr = new JButton ("Encrypt");
        pane.add (encr);
        encr.requestFocus();
        encr.addActionListener(this);
        
        JButton decr = new JButton ("Decrypt");
        pane.add (decr);
        decr.requestFocus();
        decr.addActionListener(this);

        TextArea msg = new TextArea ("Enter Message here", 3, 20);
        pane.add (msg);

        JLabel key = new JLabel ("Key: ");
        pane.add (key);

        TextField txtKey = new TextField (20);
        pane.add (txtKey);
        
        setVisible (true);
    }

    public void actionPerformed (ActionEvent e)
    {
        if(e.getSource() == encr)
        {
            
        }
        else if (e.getSource() == decr)
        {
        
        }
    }
    
    public static void main (String args[])
    {
        new CaeserCipherGUI ();
    }
}



I am only in HighSchool at the moment and i really need a few pointers and any advice! Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: How can i Implement a text based app into a GUI interface?

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: How can i Implement a text based app into a GUI interface?

Posted 16 October 2011 - 04:07 AM

Your UI looks pretty good. So what you'd do next is add your encryption code to the actionPerformed() method to execute when the encrypt button is pressed and add your decryption code to the actionPerformed() method to execute when the decrypt button is pressed.

You'll have to decide where the output goes in your UI. Is it a new text component to show the output?
Was This Post Helpful? 0
  • +
  • -

#3 Gamme40   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 16-October 11

Re: How can i Implement a text based app into a GUI interface?

Posted 16 October 2011 - 04:28 AM

Thanks for the quick reply.

Yes what the program is supposed to do is when i put a number, example '3', into the 'Key' TextField then it should shift the alphabet 3 spaces to the right and use that as the code for encryption, the TextArea i put there should encrypt whatever was in the TextArea when the 'Encrypt' Button is pushed, and the opposite should happen when the decrypt button is pushed. the Output should replace the text that was written in plain english in the TextArea...

I'm having some trouble getting access to the TextArea so that i can use that which was typed in for encryption

I did come across the getText (); function but i have no idea where or how to use it... Thanks
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: How can i Implement a text based app into a GUI interface?

Posted 16 October 2011 - 04:41 AM

View PostGamme40, on 16 October 2011 - 07:28 AM, said:

I'm having some trouble getting access to the TextArea so that i can use that which was typed in for encryption

I did come across the getText (); function but i have no idea where or how to use it... Thanks

So for the encrypt function, you'd access the text in the TextArea, msg, with the statement:
String toEncrypt = msg.getText();

Similarly for your TextField, txtKey, to get the key.
You'll have to move the declarations for msg and txtKey to instance fields so that they are visible to your actionPerformed() method like so:
public class CaeserCipherGUI extends JFrame implements ActionListener
{
    JButton encr;
    JButton decr;
    JPanel pane = new JPanel ();
    TextArea msg;
    TextField txtKey;

    . . . 
} // end class CaeserCipherGUI

BTW, remove JButton from in front of encr and decr in your constructor and do not precede (redeclare) msg and txtKey with TextArea and TextField, respectively, in the constructor. Declaring them in the constructor makes them local variables which limits their visibility.

If you have trouble with those changes or don't understand them, please ask.
Was This Post Helpful? 1
  • +
  • -

#5 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7327
  • View blog
  • Posts: 15,238
  • Joined: 16-October 07

Re: How can i Implement a text based app into a GUI interface?

Posted 16 October 2011 - 04:45 AM

The problem is that both programs have a UI. When programming, you should always try to remove computational logic from IO logic.

If we were to write your class so it wasn't so tied up in IO:
import java.io.*;
import javax.swing.*;

public class CaesarCipher {
	public String encode(String msg, int shift) {
		String msgEncoded = "";
		for (int i = 0 ; i < msg.length () ; i++) {
			char character = msg.charAt(i);
			if (character != ' ') {
				character= (char) ('a' + (character - 'a' + shift) % 26);
			}
			msgEncoded += character;
		}
		return msgEncoded;
	}
	
	public static void main (String args[]) {
		int shift = Integer.parseInt (JOptionPane.showInputDialog ("Enter Key here"));
		String msg = JOptionPane.showInputDialog ("Enter message here");
		
		CaesarCipher cc = new CaesarCipher();
		System.out.println ("Encrypted sentence is: " + cc.encode(msg, shift));
		System.out.println ("Decrypted sentence is: " + msg);
	 }
}



Now you should be able to call your method from any program.

Hope this helps.
Was This Post Helpful? 3
  • +
  • -

#6 Gamme40   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 16-October 11

Re: How can i Implement a text based app into a GUI interface?

Posted 16 October 2011 - 05:23 AM

This is my new code that i created, i put the encryption technique from my CeaserCipher into the GUI without any compilation problems, When i run the app it allows me to enter information to encrypt but it wont read the information and encrypt it

I put the TextArea and TextField in a public static form as suggested, thanks

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class CaeserCipherGUI extends JFrame implements ActionListener
{
    int alphaShift;
    String toEncrypt;
    String msgTyped;
    char character;
    JButton encr;
    JButton decr;
    JPanel pane = new JPanel ();

    public static TextArea msg;
    public static TextField txtKey;

    CaeserCipherGUI ()
    {
        super ("Caeser");
        setBounds (200, 200, 200, 200);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setResizable (false);
        Container con = this.getContentPane ();
        con.add (pane);

        JButton encr = new JButton ("Encrypt");
        pane.add (encr);
        encr.requestFocus ();
        encr.addActionListener (this);

        JButton decr = new JButton ("Decrypt");
        pane.add (decr);
        decr.requestFocus ();
        decr.addActionListener (this);

        TextArea msg = new TextArea ("Enter Message here", 3, 20);
        pane.add (msg);

        JLabel key = new JLabel ("Key: ");
        pane.add (key);

        TextField txtKey = new TextField (20);
        pane.add (txtKey);

        setVisible (true);
    }


    public void actionPerformed (ActionEvent e)
    {
        if (e.getSource () == encr)
        {

            for (int i = 0 ; i < msgTyped.length () ; i++)
            {
                toEncrypt = msg.getText ();

                //gets the characters in the message typed
                character = msgTyped.charAt (i);

                //if statement to check if character is a 'space'
                if (character != ' ')
                {
                    //the shift
                    character = (char) ('a' + (character - 'a' + alphaShift) % 26);
                }
                System.out.print (character);

            }
        }
        else if (e.getSource () == decr)
        {

        }
    }


    public static void main (String args[])
    {
        new CaeserCipherGUI ();
    }
} 


The reason i have the 'JButton encr;' and the 'JButton decr;' is if i remove them then the if statements won't function as they request a field name to be declared... :bigsmile:

Mabye i have to make a new way for the cipher to work, the only other way i can think of is an Array, but we havent learnt anything about those yet! Thank you for your time in assisting me, i really appreciate it! i hope i can finally get this program working with your help before the end of the day :bigsmile: :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: How can i Implement a text based app into a GUI interface?

Posted 16 October 2011 - 05:46 AM

You're getting an error, a null pointer exception. Are you seeing that? You should be. When you get errors, post them exactly, cut and paste.

Why are you getting an error, 2 reasons:
1. As I tried to tell you before, line 37 should be:
msg = new TextArea ("Enter Message here", 3, 20);
so that the instance variable msg is visible to the actionPerfomed() method. Redeclaring it as you did makes it local to the constructor AND the instance variable msg is empty or NULL.
2. msgTyped is similarly NULL. You declared it to be an String instance field but never put anything in it. Simplify the code some and change it to:
        if (e.getSource () == encr)
        {
            toEncrypt = msg.getText();

            for (int i = 0 ; i < toEncrypt.length() ; i++)
            {
                //gets the characters in the message typed
                character = toEncrypt.charAt(i);

                //if statement to check if character is a 'space'
                if (character != ' ')
                {
                    //the shift
                    character = (char) ('a' + (character - 'a' + alphaShift) % 26);
                }
                System.out.print (character);

            }
        }

You will need to do the same for the txtKey component.
Your buttons work fine with the leading "JButton" part removed in the constructor, so that the code looks like:
        encr = new JButton ("Encrypt");
        decr = new JButton ("Decrypt");

I'm not sure why you think that presents problems.

I didn't say that msg and txtKey should be static.
TextArea msg;
TextField txtKey;

is sufficient.

Edit: corrected actionPerformed() method.

This post has been edited by GregBrannon: 16 October 2011 - 05:52 AM

Was This Post Helpful? 1
  • +
  • -

#8 Gamme40   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 16-October 11

Re: How can i Implement a text based app into a GUI interface?

Posted 16 October 2011 - 06:13 AM

Thanks alot!! :bigsmile: i can now see the text in the text area be output into a new form

but is it possible for the new text to replace the old text after the encrypt button is pushed?

right now its replacing the ':' and 'E' with 'T' and '_' when the encrypt button is pushed, here is the new code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class CaeserCipherGUI extends JFrame implements ActionListener
{
    int alphaShift;
    String msgTyped;
    char character;
    JButton encr;
    JButton decr;
    JLabel key;
    JPanel pane = new JPanel ();
    TextArea msg;
    TextField txtKey;

    CaeserCipherGUI ()
    {
        super ("Caeser");
        setBounds (200, 200, 200, 200);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setResizable (false);
        Container con = this.getContentPane ();
        con.add (pane);

        encr = new JButton ("Encrypt");
        pane.add (encr);
        encr.requestFocus ();
        encr.addActionListener (this);

        decr = new JButton ("Decrypt");
        pane.add (decr);
        decr.requestFocus ();
        decr.addActionListener (this);

        msg = new TextArea ("Enter Message here:", 3, 20);
        pane.add (msg);

        key = new JLabel ("Key: ");
        pane.add (key);

        txtKey = new TextField (20);
        pane.add (txtKey);

        setVisible (true);
    }


    public void actionPerformed (ActionEvent e)
    {
        if (e.getSource () == encr)
        {
            msgTyped = msg.getText();
            int len = msgTyped.length ();
            for (int i = 0 ; i < len ; i++)
            {
                //gets the characters in the message typed
                character = msgTyped.charAt (i);

                //if statement to check if character is a 'space'
                if (character != ' ')
                {
                    //the shift
                    character = (char) ('a' + (character - 'a' + alphaShift) % 26);
                }
                System.out.print(character);
            }
        }
        else if (e.getSource () == decr)
        {

        }
    }


    public static void main (String args[])
    {
        new CaeserCipherGUI ();
    }
}

Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: How can i Implement a text based app into a GUI interface?

Posted 16 October 2011 - 06:23 AM

Quote

but is it possible for the new text to replace the old text after the encrypt button is pushed?

I would clear the msg area first:
msg.setText( "" );

then add the encrypted text when it has completed encrypting:
msg.setText( encryptedMsg );

You also might want to change the msg component label so it's obvious to the user what happened.

Quote

right now its replacing the ':' and 'E' with 'T' and '_' when the encrypt button is pushed,

I don't understand this comment. If you're not getting the desired results, show us the input, the actual output, and the desired or expected output. Since you had the cipher working, the same algorithms should work here.
Was This Post Helpful? 0
  • +
  • -

#10 Gamme40   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 16-October 11

Re: How can i Implement a text based app into a GUI interface?

Posted 16 October 2011 - 06:23 AM

forgot to mention, it is doing some sort of encryption but not exactly what i need, but that can be solved as soon as i fix the 'txtKey' textField, so that it identifys the number and then uses it as a alphabet shift

Thanks again for your time. . . :w00t:
Was This Post Helpful? 0
  • +
  • -

#11 Gamme40   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 16-October 11

Re: How can i Implement a text based app into a GUI interface?

Posted 16 October 2011 - 06:33 AM

the program and method im using works as such

When i put Hello into the TextArea and 3 into the TextField it should display 'Khoor', the 3 shifted the alphabet 3 letters forward.

How it does this is that it takes each character and changes it along the alphabet according to the 'Keys' input (3) hence why i used

66| System.out.print(character);
inside the for loop so that the characters are displayed next to each other instead of underneath each other
Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: How can i Implement a text based app into a GUI interface?

Posted 16 October 2011 - 06:45 AM

View PostGamme40, on 16 October 2011 - 09:33 AM, said:

When i put Hello into the TextArea and 3 into the TextField it should display 'Khoor', the 3 shifted the alphabet 3 letters forward.

How it does this is that it takes each character and changes it along the alphabet according to the 'Keys' input (3) hence why i used

You don't change alphaShift, so no shifting is happening. Set alphaShift to the key (or use key natively), and your algorithm should start working again.

As for your other desire, as you define each ciphered or shifter character, add it to a resulting ciphered String:
    public void actionPerformed (ActionEvent e)
    {
        String cipher = "";
        // . . . . body that does the shifting/ciphering
        msg.setText( "" ); // <--- new line
        for( )
        {
            if ( )
            {
            }
            cipher += character;  // <--- new line
        }
        msg.setText( cipher );  // <--- new line
    }

Was This Post Helpful? 1
  • +
  • -

#13 Gamme40   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 16-October 11

Re: How can i Implement a text based app into a GUI interface?

Posted 16 October 2011 - 06:59 AM

it works!@!!!#!@$!%!#^@#$^%#@ Oh my gosh!!! finally!!!! YAY!!!!! WOOT!! :bigsmile:

okay, ehm, now for one last final thing,the output is being presented in a frame of its own, due to the 'System.out.print(character);' how do i transfer that output into the TextArea? i tried to change it as 'msg.setText(character)' but it wont compile using that component, as (i think) that character has many values...

Thank you so much for all your assistance it is greatly appreciated by a young student such as myself...
Was This Post Helpful? 0
  • +
  • -

#14 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: How can i Implement a text based app into a GUI interface?

Posted 16 October 2011 - 07:02 AM

Look at all of the changes I sent you, especially building the String cipher and displaying it in the msg area. If you still can't get it and have code that doesn't compile, please post updated code (in code tags) and cut and paste the error messages you've received. What I sent you works fine here, and I think it does what you're asking.
Was This Post Helpful? 0
  • +
  • -

#15 Gamme40   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 16-October 11

Re: How can i Implement a text based app into a GUI interface?

Posted 16 October 2011 - 07:35 AM

Thank you So much this program works perfectly, all that is left to do is create the decryption part and this part of the program is completed! you have been a tremendous help!

Here is the code thusfar:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class CaeserCipherGUI extends JFrame implements ActionListener
{
    int alphaShift;
    String msgTyped;
    char character;
    JButton encr;
    JButton decr;
    JLabel key;
    JPanel pane = new JPanel ();
    TextArea msg;
    TextField txtKey;

    CaeserCipherGUI ()
    {
        super ("Caeser");
        setBounds (200, 200, 200, 200);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setResizable (false);
        Container con = this.getContentPane ();
        con.add (pane);

        encr = new JButton ("Encrypt");
        pane.add (encr);
        encr.requestFocus ();
        encr.addActionListener (this);

        decr = new JButton ("Decrypt");
        pane.add (decr);
        decr.requestFocus ();
        decr.addActionListener (this);

        msg = new TextArea ("Enter Message here:", 3, 20);
        setResizable (false);
        pane.add (msg);

        key = new JLabel ("Key: ");
        pane.add (key);

        txtKey = new TextField (20);
        pane.add (txtKey);

        setVisible (true);
    }
    public void actionPerformed (ActionEvent e)
    {
        if (e.getSource () == encr)
        {
            String cipher = "";
            alphaShift = Integer.parseInt (txtKey.getText ());
            msgTyped = msg.getText ();
            int len = msgTyped.length ();
            for (int i = 0 ; i < len ; i++)
            {
                //gets the characters in the message typed
                character = msgTyped.charAt (i);

                //if statement to check if character is a 'space'
                if (character != ' ')
                {
                    //the shift
                    character = (char) ('a' + (character - 'a' + alphaShift) % 26);
                }
                msg.setText ("");
                cipher += character;
                msg.setText (cipher);
            }
        }
        else if (e.getSource () == decr)
        {

        }
    }
    public static void main (String args[])
    {
        new CaeserCipherGUI ();
    }
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2