Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

error in action method

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trying to make a chat room. Having a problem with my action method (JDK1.1) in my ChatApplet class Am after converting all the files from 1.1 to 1.3 and had no problems til I came across this warning after changing some of the lines in the method.

"action(java.awt.Event,java.lang.Object) in java.awt.Component has been deprecated"
Here's my method:

public boolean action(Event e, Object o)
{
if ((e.target == send) && (e.arg !=""))
{ if (newbie == true)
{ msgs.append("You must first connect to the server. \n");
msgs.append("Please type your name and press connect.\n");
writemsg.requestFocus();
}
else
{ send(writemsg.getText() );
writemsg.setText("");
writemsg.requestFocus();
return true;
}
}
else if (e.target == connect)
{ if(newbie == true)
{ connect();
newbie = false;
name = writemsg.getText();
writemsg.setText("");
writemsg.requestFocus();
msgs.setText("Begin chatting. \n");
}
else
{ msgs.append("already connected!\n");
return true;
}
}
else if (e.target == disconnect)
{ stop();
newbie = true;
msgs.setText("enter your name and press connect \n");
writemsg.requestFocus();
return true;
}
return false;
}
Any help would be appreciated!
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to register an ActionListener object with
the object that generates the action. The ActionListener object should have a ActionPerformed method with the following signature public void actionPerformed(ActionEvent e)
Your code to perform whatever actions necessary should reside in the actionPerformed method.
Check out this info
http://java.sun.com/docs/books/tutorial/uiswing/overview/event.html

Hope this helps
 
John Young
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx a mill - sorted out now!
Now having a problem with my applet server - keeps on giving a runtime error: due (I think!)to the following line:
clientList = new JList();
in the AppletServer() method.
Its driving me mad(PLEASE HELP!) - I swear I'm not stupid!

Heres my AppletServer code

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;
import javax.swing.*;
public class AppletServer extends Thread
{
protected final static int defaultPort = 1976;
protected int paramPort;
protected ServerSocket server;
protected ThreadGroup tg;
protected JList clientList; //ambiguous
protected Vector clientVector;
protected Watcher watcher;
public static void main (String [] args)
{
System.out.println("The Chat Server is running");
int port = -1;
boolean background = true;
if(args.length == 0)
{ new AppletServer();
}
else if (args.length == 1)
{
if(args[0].equals(" -f"))
{
background = false;
new AppletServer(background);
}
else
{ try
{
port = Integer.parseInt(args[0]);
new AppletServer(port, background);
}

catch(NumberFormatException e)
{
System.out.print("not a valid port number");
System.out.println("use and inteer over 1024");
System.out.println(e);
System.out.println();
System.out.print("Starting sever on "+ defaultPort);
new AppletServer();
}
}
}
else if ( (args.length == 2) && args[0] == "-f")
{
background = false;
try
{
port = Integer.parseInt(args[0]);
new AppletServer(port, background);
}
catch(NumberFormatException e)
{
System.out.print("not a valid port number");
System.out.println("use and intsert over 1024");
System.out.println(e);
System.out.println();
System.out.print("Starting sever on "+ defaultPort);
new AppletServer();
}
}
else
System.out.println("usage: java AppletServer [-f] [<port_#>]");
}

public AppletServer()
{
super(); //create thread
try
{
server = new ServerSocket(defaultPort);

}
catch(IOException e)
{
System.out.println("could not create ServerSocket");
System.out.println(e);
System.exit(1);
}

tg = new ThreadGroup("Connections");
clientList = new JList();
clientVector = new Vector();
watcher = new Watcher(this);
this.start();
}
public AppletServer(boolean background)
{
super(); //create thread
try
{
server = new ServerSocket(defaultPort);
}
catch(IOException e)
{
System.out.println("could not create ServerSocket");
System.out.println(e);
System.exit(1);
}
tg = new ThreadGroup("Connections");
clientList = new JList(); //ambiguous
clientVector = new Vector();
watcher = new Watcher(this);
this.start();

Frame f = new Frame("AppletServer Monitor");
f.add("Center", clientList);
f.setSize(400, 300);
f.show();
this.start();
}
public AppletServer(int port, boolean background)
{
super(); //create thread
try
{ server = new ServerSocket(port);
}
catch(IOException e)
{
System.out.println("could not create ServerSocket");
System.out.println(e);
System.exit(1);
}
tg = new ThreadGroup("Connections");
clientList = new JList(); //ambiguous
clientVector = new Vector();
watcher = new Watcher(this);
Frame f = new Frame("AppletServer Monitor");
f.add("Center", clientList);
f.setSize(400, 300);
f.show();
this.start();
}
public void run()
{ try
{ for(;
{
Socket client = server.accept();
ClientConnect cc = new ClientConnect(this, client, tg, watcher);
synchronized(clientVector)
{
clientVector.addElement(cc);
//clientList.add(cc.toString());
clientList = new JList(clientVector);
}
}
}
catch(IOException e)
{
System.out.println("error accepting connections");
System.out.println(e);
System.exit(1);
}
}
}
 
John Young
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
obviously that wink wasnt supposed to come out in the middle of the code, its because it supposed to be
for(;; then another )
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can prevent smilies from occurring in you posts by either (a) inserting a space between the : and the ), or (b) using the "disable smilies" checkbox at the bottom of your post. In the past the latter feature only worked if you remembered to use it when you first submitted the post, but now it's fixed so you can also fix it later, using the little pencil-and-paper icon to edit your post. Also, you should really use [code] tags to improve the readability of your code - follow the "UBB Code is enabled" link when you're comosing a post, to see examples.
 
John Young
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
em ok, thanx for advise. New to this so gimme a bit of time and I'll be a perfect poster!
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic