FileDocCategorySizeDatePackage
SimpleChatClient.javaAPI DocExample3084Thu Mar 24 11:37:02 GMT 2005None

SimpleChatClient

public class SimpleChatClient extends Object

Fields Summary
JTextArea
incoming
JTextField
outgoing
BufferedReader
reader
PrintWriter
writer
Socket
sock
Constructors Summary
Methods Summary
public voidgo()


       // build gui

        JFrame frame = new JFrame("Ludicrously Simple Chat Client");
        JPanel mainPanel = new JPanel();
        
        incoming = new JTextArea(15,20);  // was 15,50
        incoming.setLineWrap(true);
        incoming.setWrapStyleWord(true);
        incoming.setEditable(false);
       
        JScrollPane qScroller = new JScrollPane(incoming);
        qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        

        outgoing = new JTextField(20);
        

        JButton sendButton = new JButton("Send");
        sendButton.addActionListener(new SendButtonListener());

        
        mainPanel.add(qScroller);
        mainPanel.add(outgoing);
        mainPanel.add(sendButton);
        
        setUpNetworking();
        Thread readerThread = new Thread(new IncomingReader());
        readerThread.start();
          
        frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
        frame.setSize(400,500);
        frame.setVisible(true);
                
   
     
public static voidmain(java.lang.String[] args)

       SimpleChatClient client = new SimpleChatClient();
       client.go();
    
private voidsetUpNetworking()

  
        try {
           sock = new Socket("127.0.0.1", 5000);
           InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
           reader = new BufferedReader(streamReader);

           writer = new PrintWriter(sock.getOutputStream());
          
           System.out.println("networking established");
        } catch(IOException ex) {
           ex.printStackTrace();
        }