Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #5511

Re: Dynamic updation of a JTextArea

Newsgroups comp.lang.java.gui
Date 2016-12-15 00:18 -0800
References (3 earlier) <89366421-9545-42ed-8d8e-b960310c1047@googlegroups.com> <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>
Message-ID <e4cc5ca9-3580-49b9-8084-f311ce05381d@googlegroups.com> (permalink)
Subject Re: Dynamic updation of a JTextArea
From abhprk3926@gmail.com

Show all headers | View raw


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();
			}
		});
	}
	
}

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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