omashhour
asked on
i have a problem with methods
i cant set the nickname of the user in this program can anybody help me please
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
class Client extends JFrame implements ActionListener
{
JButton send=new JButton("Send");
JLabel beyekteb=new JLabel(" ");
JTextArea itext=new JTextArea(4, 20);
JTextArea dtext=new JTextArea(15, 20);
JTextField bob=new JTextField(10);
Nick nickName=new Nick();
JMenuBar bar=new JMenuBar();
JMenu file=new JMenu("File");
JMenu help=new JMenu("Help");
JMenuItem nick=new JMenuItem("Change Nick");
JMenuItem about=new JMenuItem("About");
Socket socket=null;
PrintWriter out=null;
BufferedReader in=null;
String line;
String n;
Date now = new Date();
JScrollPane scroll= new JScrollPane(dtext);
Client()
{
super("Client");
bar.add(file);
bar.add(help);
file.add(nick);
dtext.setEditable(false);
send.addActionListener(thi s);
nick.addActionListener(thi s);
itext.setBorder(BorderFact ory.create EtchedBord er());
dtext.setBorder(BorderFact ory.create EtchedBord er());
BorderLayout bord=new BorderLayout();
BorderLayout bord1=new BorderLayout();
BorderLayout bord2=new BorderLayout();
JPanel pane2=new JPanel();
pane2.setLayout(bord);
pane2.add(itext);
pane2.add("East", send);
JPanel pane1=new JPanel();
pane1.setLayout(bord1);
pane1.add("North",scroll);
pane1.add(pane2);
pane1.add("South", beyekteb);
JPanel pane=new JPanel();
pane.setLayout(bord2);
pane.add("North", bar);
pane.add(pane1);
setContentPane(pane);
}
public void listenSocket(){
try
{
socket=new Socket("localhost",1348);
in=new BufferedReader(new InputStreamReader(socket.g etInputStr eam()));
out=new PrintWriter(socket.getOutp utStream() , true);
for(int i=0;i<5000;i++)
{ line=in.readLine();
dtext.append("\nAshraf:"+l ine);
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void main(String [] args)
{
Client client=new Client();
client.setTitle("Client Program");
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
client.addWindowListener(l );
client.pack();
client.setVisible(true);
client.listenSocket();
}
public void setNick(String n)
{
this.n=n;
}
public void actionPerformed(ActionEven t e)
{
Object source=e.getSource();
if(source==send)
{
String input=itext.getText();
out.println(input);
dtext.append("\n"+n+":"+in put);
itext.setText("");
beyekteb.setText("Your Chat Started at "+now);
}
if(source==nick)
{
nickName.setVisible(true);
}
}
}
class Nick extends JFrame implements ActionListener
{
JButton set=new JButton("Set");
JLabel label=new JLabel("New Nick");
JTextField text=new JTextField(10);
String n;
Client client;
Nick()
{
super("Change Nick");
set.addActionListener(this );
BorderLayout bord=new BorderLayout();
FlowLayout flow=new FlowLayout();
JPanel pane1=new JPanel();
pane1.setLayout(flow);
pane1.add(label);
pane1.add(text);
JPanel pane=new JPanel();
pane.setLayout(bord);
pane.add(pane1);
pane.add("South", set);
setContentPane(pane);
pack();
setVisible(false);
}
public void actionPerformed(ActionEven t e) {
client=new Client();
Object source=e.getSource();
if(source==set)
{
n=text.getText();
client.setNick(n);
setVisible(false);
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
class Client extends JFrame implements ActionListener
{
JButton send=new JButton("Send");
JLabel beyekteb=new JLabel(" ");
JTextArea itext=new JTextArea(4, 20);
JTextArea dtext=new JTextArea(15, 20);
JTextField bob=new JTextField(10);
Nick nickName=new Nick();
JMenuBar bar=new JMenuBar();
JMenu file=new JMenu("File");
JMenu help=new JMenu("Help");
JMenuItem nick=new JMenuItem("Change Nick");
JMenuItem about=new JMenuItem("About");
Socket socket=null;
PrintWriter out=null;
BufferedReader in=null;
String line;
String n;
Date now = new Date();
JScrollPane scroll= new JScrollPane(dtext);
Client()
{
super("Client");
bar.add(file);
bar.add(help);
file.add(nick);
dtext.setEditable(false);
send.addActionListener(thi
nick.addActionListener(thi
itext.setBorder(BorderFact
dtext.setBorder(BorderFact
BorderLayout bord=new BorderLayout();
BorderLayout bord1=new BorderLayout();
BorderLayout bord2=new BorderLayout();
JPanel pane2=new JPanel();
pane2.setLayout(bord);
pane2.add(itext);
pane2.add("East", send);
JPanel pane1=new JPanel();
pane1.setLayout(bord1);
pane1.add("North",scroll);
pane1.add(pane2);
pane1.add("South", beyekteb);
JPanel pane=new JPanel();
pane.setLayout(bord2);
pane.add("North", bar);
pane.add(pane1);
setContentPane(pane);
}
public void listenSocket(){
try
{
socket=new Socket("localhost",1348);
in=new BufferedReader(new InputStreamReader(socket.g
out=new PrintWriter(socket.getOutp
for(int i=0;i<5000;i++)
{ line=in.readLine();
dtext.append("\nAshraf:"+l
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void main(String [] args)
{
Client client=new Client();
client.setTitle("Client Program");
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
client.addWindowListener(l
client.pack();
client.setVisible(true);
client.listenSocket();
}
public void setNick(String n)
{
this.n=n;
}
public void actionPerformed(ActionEven
{
Object source=e.getSource();
if(source==send)
{
String input=itext.getText();
out.println(input);
dtext.append("\n"+n+":"+in
itext.setText("");
beyekteb.setText("Your Chat Started at "+now);
}
if(source==nick)
{
nickName.setVisible(true);
}
}
}
class Nick extends JFrame implements ActionListener
{
JButton set=new JButton("Set");
JLabel label=new JLabel("New Nick");
JTextField text=new JTextField(10);
String n;
Client client;
Nick()
{
super("Change Nick");
set.addActionListener(this
BorderLayout bord=new BorderLayout();
FlowLayout flow=new FlowLayout();
JPanel pane1=new JPanel();
pane1.setLayout(flow);
pane1.add(label);
pane1.add(text);
JPanel pane=new JPanel();
pane.setLayout(bord);
pane.add(pane1);
pane.add("South", set);
setContentPane(pane);
pack();
setVisible(false);
}
public void actionPerformed(ActionEven
client=new Client();
Object source=e.getSource();
if(source==set)
{
n=text.getText();
client.setNick(n);
setVisible(false);
}
}
}
Change the variable name that you are passing in to something like newn or tempn, so that the name of the temporary variable is not the same as the name of the variable in the class. As in:
public void setNick(String tempn)
{
this.n=tempn;
}
public void setNick(String tempn)
{
this.n=tempn;
}
How will that help?
Certainly though, your code should be self-documenting. A variable name of 'n', (including yourself later) anything.
Sometimes you *may* see this sort of thing, but usually it'll be a method one, perhaps used only once.
Certainly though, your code should be self-documenting. A variable name of 'n', (including yourself later) anything.
Sometimes you *may* see this sort of thing, but usually it'll be a method one, perhaps used only once.
changing the name of the variable would help because i think that the compiler doesn't know which n to use, because it is in two different scopes at the same time.
>>it is in two different scopes
It isn't. this.n is quite distinct from n. And anyway, where did you get the idea that there are compiling problems?
It isn't. this.n is quite distinct from n. And anyway, where did you get the idea that there are compiling problems?
ASKER
i tried what ur saying but its not working
ASKER
by the way for your own knowledge when i say this.n it differentiates between the 2 n's
ASKER
no there are no compiling problems but when i try the program the nick name remains null
it does not change
do u want me to send the server CEHJ??
it does not change
do u want me to send the server CEHJ??
What changes have you made to the method in question?
The Nick class has it's own reference to a Client. A new instance is of Client is created every time actionPerformed is called (though I'm not sure how this works, given the ActionListener is applied to the JFrame subclass and not a button or menu item).
Since a new Client object is created each time and not passed to anything else, this would explain why you are not seeing what you expect.
Perhaps when you create the Nick object from the Client, you should pass in the reference to Client in the constructor, eg.
Nick nickName = new Nick(this);
and in the Nick class:
public Nick(Client client)
{
this.client = client;
.
.
}
Then get rid of the client creation in the actionPerformed (in the Nick class).
Since a new Client object is created each time and not passed to anything else, this would explain why you are not seeing what you expect.
Perhaps when you create the Nick object from the Client, you should pass in the reference to Client in the constructor, eg.
Nick nickName = new Nick(this);
and in the Nick class:
public Nick(Client client)
{
this.client = client;
.
.
}
Then get rid of the client creation in the actionPerformed (in the Nick class).
>>but when i try the program the nick name remains null
I'm now getting confused as to what you mean by 'nick name' ;-) What *do* you mean?
I'm now getting confused as to what you mean by 'nick name' ;-) What *do* you mean?
ASKER
public void setNick(String n)
{
this.n=n;
}
i want the method above
to have effect in this part
if(source==send)
{
String input=itext.getText();
out.println(input);
dtext.append("\n"+n+":"+in put); <===============the n here is the nickname
itext.setText("");
beyekteb.setText("Your Chat Started at "+now);
}
but the problem is that the n is always null it does not change
{
this.n=n;
}
i want the method above
to have effect in this part
if(source==send)
{
String input=itext.getText();
out.println(input);
dtext.append("\n"+n+":"+in
itext.setText("");
beyekteb.setText("Your Chat Started at "+now);
}
but the problem is that the n is always null it does not change
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you were creating a new CLient class and setting the nick in that....this was having no effect on the existing Client class...
so it was staying null...
Tim.
so it was staying null...
Tim.
Oh yeah, and change:
Nick nickName=new Nick();
to
Nick nickName = null ;
in the Client class
Nick nickName=new Nick();
to
Nick nickName = null ;
in the Client class
Thanks Tim. I'm too tired to go into this now. I'm off to bed 3-)
ASKER
thanks tim your the man :)
:-)
>>
public void setNick(String n)
{
this.n=n;
}
>>
does not alter the actual menu item