Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help Need with KeyPressed

807597Mar 29 2005 — edited Mar 29 2005
Hello! I've looked though all my books in paper form and online. But, I can't seem to find a good example to follow on how to setup a key event. What I'm trying to do is the user enters data into the textfield. Then when the user either clicks the send button or presses the enter key I need to copy the data from the textfield to the textarea. Below is my attempt at it. I will say that is not very good. I also have compile errors from this attempt. But, seeing I have no idea on how to set this up. I have no idea on how to fix the compile errors. If anyone can explain to me on how to do this, I would be very happy.
import java.io.*;
import java.awt.*;
import java.awt.event.*;


class ChatClient extends Frame
{
	private TextField tf1 = new TextField("", 50);
	private TextArea ta;
    private Button sendButton;
    private Button quitButton;
    private MenuItem exitMT, aboutMT;


	public ChatClient()
   	{

	  super("ChatClient");
	  addWindowListener(new WindowAdapter()
	  {public void windowClosing(WindowEvent e) {System.exit(0); }});
	  setTitle("Chat Room");
	  Panel p = new Panel(new BorderLayout());
      Panel p1 = new Panel(new BorderLayout());
      Panel p2 = new Panel(new GridLayout(3,1));


      MenuBar mb = new MenuBar ();
      setMenuBar (mb);

      Menu fileMenu = new Menu ("File");
	  mb.add(fileMenu);

	  Menu helpMenu = new Menu ("Help");
      mb.add(helpMenu);

      fileMenu.add (exitMT= new MenuItem ("Quit"));
	  helpMenu.add (aboutMT = new MenuItem ("About"));

	  exitMT.addActionListener(new ExitCommand());
      aboutMT.addActionListener(new AboutCommand());

	  //creating main panel
	  add(p, BorderLayout.NORTH);
      ta = new TextArea(10, 50);
      add(ta, BorderLayout.CENTER);
      ta.setSize(500,300);
      ta.setEditable(false);

    //adding buttons to the right side of the window
      sendButton = new Button("Send");
      Panel p3 = new Panel();
      sendButton.setSize(5,5);
      sendButtom.setBackground(Color.green);
	  p3.add(sendButton);
	  p2.add(p3);
	  sendButton.addActionListener(new CopyCommand());
	  quitButton = new Button("Quit");
	  Panel p4 = new Panel();
	  quitButton.setSize(10,5);
	  quitButton.setBackground(Color.red);
	  p4.add(quitButton);
	  p2.add(p4);
	  quitButton.addActionListener(new ExitCommand());

      add(p2, BorderLayout.EAST);

   //adding the textfield to the south end of the window
      tf1.requestFocus();
      p1.add(tf1);

      add(p1, BorderLayout.SOUTH);
      tf1.addkeyListener(new CopyCommand());


   }


//handling ActionEvent from buttons and menu items
  class ExitCommand implements ActionListener
  {
	  public void actionPerformed (ActionEvent e)
	  {
		  tf1.setText("");
	      ta.setText("");
          System.exit(0);
      }
  }

  class AboutCommand implements ActionListener
  {
  	  public void actionPerformed (ActionEvent e)
  	  {
  		  System.out.println("About menu was pressed!");
      }
  }

  class CopyCommand implements ActionListener, KeyListener
  {
     public void actionPerformed (ActionEvent e)
     {
	  //need to able to pass these statements to the end of this class.
	  //So, they will have all the data needed.

      //ta.append(tf1.getText() + "\n");
      //tf1.setText("");
     }

     public void keyPressed (KeyEvent ke)
     {
		//figure out if the enter key was pressed
		public int getKeyCode();

		if getKeyCode() equals VK_ENTER
		{
			//continue
	    else
	       System.out.print("Bad Key Pressed");
	    }
     }

     ta.append(tf1.getText() + "\n");
     tf1.setText("");
  }





   public static void main(String[] args)
   {  Frame f = new ChatClient();
      f.setSize(600, 400);
      f.show();
   }

} 
Compile errors:

F:\Chatclient\ChatClient.java:112: illegal start of expression
public int getKeyCode();
^
F:\Chatclient\ChatClient.java:114: '(' expected
if getKeyCode() equals VK_ENTER
^
F:\Chatclient\ChatClient.java:120: illegal start of expression
}
^
F:\Chatclient\ChatClient.java:122: <identifier> expected
ta.append(tf1.getText() + "\n");
^
F:\Chatclient\ChatClient.java:123: <identifier> expected
tf1.setText("");
^
5 errors
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 26 2005
Added on Mar 29 2005
12 comments
166 views