Groups | Search | Server Info | Keyboard shortcuts | Login | Register
Groups > comp.lang.java.gui > #5512
| From | Knute Johnson <nospam@rabbitbrush.frazmtn.com> |
|---|---|
| Newsgroups | comp.lang.java.gui |
| Subject | Re: Dynamic updation of a JTextArea |
| Date | 2016-12-15 14:58 -0600 |
| Organization | A noiseless patient Spider |
| Message-ID | <o2v02q$vam$1@dont-email.me> (permalink) |
| References | (4 earlier) <o2scne$ect$1@dont-email.me> <o2seh4$kim$1@dont-email.me> <bc5ab371-045e-432c-9ca8-3ab7d5fcc862@googlegroups.com> <o2sgne$sfk$2@dont-email.me> <e4cc5ca9-3580-49b9-8084-f311ce05381d@googlegroups.com> |
On 12/15/2016 02:18, abhprk3926@gmail.com wrote:
> just one time when i have created the chat class. here lemme show. here is the full code .
>
> import java.net.*;
> import java.io.*;
> import javax.swing.*;
> import java.awt.event.*;
> import java.awt.Toolkit;
>
> class mainServer
> {
> public static void main(String args[])
> {
> if(args.length!=1)
> {
> System.out.println("Port Number Required");
> System.exit(-1);
> }
>
> try(ServerSocket server = new ServerSocket(Integer.parseInt(args[0])))
> {
> while(true)
> new chatServerThread(server.accept());
>
> }catch(Exception e){e.printStackTrace();}
> }
> }
>
> class chatServerThread implements Runnable
> {
> Socket client;
> Thread t;
> String clientName,s="";
>
> chatServerThread(Socket socket)
> {
> client=socket;
> t=new Thread(this,"newThread");
> t.start();
> }
>
> public void run()
> {
> try
> {
> DataInputStream in = new DataInputStream(client.getInputStream());
> s="Joined";
>
> clientName=in.readUTF();
> clientChat.ta.append(clientName+" : "+s);
>
> new Thread(new Runnable(){
> public void run()
> {
> while(!s.equals("terminate"))
> {
> try
> {
> s=in.readUTF();
> clientChat.ta.append(clientName+" : "+s);
>
> }catch(Exception e){e.printStackTrace();}
>
> }
> }
> }).start();
>
>
> }catch(Exception e){e.printStackTrace();}
> }
>
> }
>
> class clientChat
> {
> public JFrame f1 = new JFrame();
> public JLabel name = new JLabel("Enter Name : ");
> public JTextField namet = new JTextField(20);
> public JLabel port = new JLabel("Enter Port Number : ");
> public JTextField portt = new JTextField(20);
> public JLabel ip = new JLabel("Enter Server's IP : ");
> public JTextField ipt = new JTextField(20);
> public JButton connect = new JButton("Connect To Server");
> public JLabel message = new JLabel("Write Message : ");
> public JTextField messaget = new JTextField(20);
> public static JTextArea ta = new JTextArea();
> public static JScrollPane jsp = new JScrollPane(ta);
> public JButton send = new JButton("Send Message");
> public JButton terminate = new JButton("Close Session");
> Socket client=null;
> DataOutputStream out=null;
>
> clientChat()
> {
> f1.setSize(525,400);
> send.setEnabled(false);
> ta.setEditable(false);
> f1.setResizable(false);
> messaget.setEditable(false);
> f1.setTitle("Chat Window");
> f1.setLocationRelativeTo(null);
> f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> setLayout();
> f1.setVisible(true);
>
> send.addActionListener(new ActionListener(){
> public void actionPerformed(ActionEvent ae)
> {
>
> try
> {
> out.writeUTF(messaget.getText());
>
> }catch(Exception e){e.printStackTrace();}
>
> }
> });
>
> terminate.addActionListener(new ActionListener(){
> public void actionPerformed(ActionEvent ae)
> {
>
> try
> {
> out.writeUTF("terminate");
> send.setEnabled(false);
> connect.setEnabled(true);
> namet.setEditable(true);
> ipt.setEditable(true);
> portt.setEditable(true);
> messaget.setEditable(false);
> client.close();
>
> }catch(Exception e){e.printStackTrace();}
>
> }
> });
>
> connect.addActionListener(new ActionListener(){
> public void actionPerformed(ActionEvent ae)
> {
>
>
> if(namet.getText().length() == 0 || portt.getText().length() == 0 || ipt.getText().length() == 0)
> {
> Toolkit.getDefaultToolkit().beep();
> JOptionPane.showMessageDialog(f1,"Make Sure These Details Are Filled: \n1:Name\n2:Port Number Of Server\n3:Server's IP Address","Notice",JOptionPane.ERROR_MESSAGE);
> }
> else
> {
> try
> {
> client = new Socket(ipt.getText(),Integer.parseInt(portt.getText()));
> out = new DataOutputStream(client.getOutputStream());
> out.writeUTF(namet.getText());
> connect.setEnabled(false);
> namet.setEditable(false);
> messaget.setEditable(true);
> ipt.setEditable(false);
> portt.setEditable(false);
> send.setEnabled(true);
>
> }catch(Exception e){e.printStackTrace();}
>
> }
>
> }
> });
> }
>
> public void setLayout()
> {
> GroupLayout gl = new GroupLayout(f1.getContentPane());
> f1.getContentPane().setLayout(gl);
> gl.setAutoCreateGaps(true);
> gl.setAutoCreateContainerGaps(true);
>
> gl.setHorizontalGroup(
> gl.createParallelGroup()
> .addGroup(gl.createSequentialGroup()
> .addComponent(name)
> .addComponent(namet)
> .addComponent(connect))
> .addGroup(gl.createSequentialGroup()
> .addComponent(port)
> .addComponent(portt))
> .addGroup(gl.createSequentialGroup()
> .addComponent(ip)
> .addComponent(ipt))
> .addGroup(gl.createSequentialGroup()
> .addComponent(message)
> .addComponent(messaget)
> .addComponent(send))
> .addComponent(jsp)
> .addComponent(terminate)
> );
>
> gl.setVerticalGroup(
> gl.createSequentialGroup()
> .addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
> .addComponent(name)
> .addComponent(namet)
> .addComponent(connect))
> .addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
> .addComponent(port)
> .addComponent(portt))
> .addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
> .addComponent(ip)
> .addComponent(ipt))
> .addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
> .addComponent(message)
> .addComponent(messaget)
> .addComponent(send))
> .addComponent(jsp)
> .addComponent(terminate)
> );
>
> gl.linkSize(SwingConstants.HORIZONTAL,name,port,ip,message);
> gl.linkSize(SwingConstants.HORIZONTAL,namet,portt,ipt,messaget);
> gl.linkSize(SwingConstants.HORIZONTAL,connect,send);
> }
>
> public static void main(String args[])
> {
> SwingUtilities.invokeLater(new Runnable(){
> public void run()
> {
> new clientChat();
> }
> });
> }
>
> }
>
I think I see what your problem is (or at least one of them). In
mainServer you reference clientChat.ta which is a static variable. When
mainServer then creates a clientChat object it is a different clientChat
object than when the program clientChat creates that object. You say
"how can that be, it's static and there is only one static object?"
Well there is one in the virtual machine that is running mainServer and
there is one in the virtual machine that is running clientChat they
aren't the same object.
I think this would have become obvious to you had you done as I
suggested and created an SSCCE. You would have seen that adding text to
a JTextArea running in one virtual machine would not affect another
JTextArea running in another virtual machine even if they are static.
In a classic "Chat" program you have clients connecting and
disconnecting from the server and you have messages coming from a client
to the server and back out to all the other clients. Your server needs
to be a little smarter and keep track of everybody that is connected so
when a message comes in the server can send it out to everybody that is
connected.
--
Knute Johnson
Back to comp.lang.java.gui | Previous | Next — Previous in thread | Next in thread | Find similar
Dynamic updation of a JTextArea abhprk3926@gmail.com - 2016-12-14 06:16 -0800
Re: Dynamic updation of a JTextArea Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-12-14 09:14 -0600
Re: Dynamic updation of a JTextArea abhprk3926@gmail.com - 2016-12-14 08:44 -0800
Re: Dynamic updation of a JTextArea abhprk3926@gmail.com - 2016-12-14 08:47 -0800
Re: Dynamic updation of a JTextArea Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-12-14 15:15 -0600
Re: Dynamic updation of a JTextArea Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-12-14 15:46 -0600
Re: Dynamic updation of a JTextArea abhprk3926@gmail.com - 2016-12-14 14:09 -0800
Re: Dynamic updation of a JTextArea abhprk3926@gmail.com - 2016-12-14 14:09 -0800
Re: Dynamic updation of a JTextArea Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-12-14 16:23 -0600
Re: Dynamic updation of a JTextArea Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-12-14 16:24 -0600
Re: Dynamic updation of a JTextArea abhprk3926@gmail.com - 2016-12-15 00:18 -0800
Re: Dynamic updation of a JTextArea Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-12-15 14:58 -0600
Re: Dynamic updation of a JTextArea abhprk3926@gmail.com - 2016-12-16 02:23 -0800
csiph-web