Hi,

My java is really bad and I haven't had to use it in a few years but I want to make it better so I've decided to try building it up slowly but at the moment I'm a little stuck and was wondering if anyone could help point me in the right direction.

I'm trying to make a Chat program, similar to msn and yahoo. It would be a client that can send and receive messages between its self on running on two client machines. The machines can either be wired or unwired and the messages sent via a IP local area network.

To start, I'm just working on a basic client-server idea and then to build up to this but I've come to a glitch on the client side (my server side has no bugs but until the client side is bug-free I can't test them).

This is the code, and I'll underline the bits that are showing an error message on my screen. If anybody can help, that would be great, just a little nudge in the right direction. Oh and some of this has been used from a textbook which is why it could be a little dodgy - I know from past experience that the code in textbooks isn't always the greatest.

import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BasicChatClient {

    JTextArea imcoming;
    JTextField outgoing;
    BufferedReader reader;
    PrintWriter writer;
    Socket sock;

    public static void main(String[] args) {
        BasicChatClient client = new BasicChatClient();
        client.go();
    }

    public void go() {

        JFrame frame = new JFrame("A Basic Chat Client");
        JPanel mainPanel = new JPanel();
        imcoming = new JTextArea(15,50);
        [U]incoming[/U].setLineWrap(true);
        [U]incoming[/U].setWrapStyleWord(true);
        [U]incoming[/U].setEditable(false);
        JScrollPane qScroller = new JScrollPane([U]incoming[/U]);
        qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        outgoing = new JTextField(20);
        JButton sendButton = [U]JButton[/U]("Send");
        sendButton.addActionListener(new SendButtonListener());
        JButton exitButton = [U]JButton[/U]("Exit");
        exitButton.addActionListener(new ExitButtonListener());
        mainPanel.add(qScroller);
        mainPanel.add(outgoing);
        mainPanel.add(sendButton);
        mainPanel.add(exitButton);
        setUpNetworking();

        Thread readerThread = new Thread(new IncomingReader());
        readerThread.start();

        frame.[U]getContent()[/U].add(BorderLayout.CENTER, mainPanel);
        frame.setSize(400,500);
        frame.setVisible(true);
    }

    private void setUpNetworking() {
        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();
        }
    }

    public class SendButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent ev) {
            try {
                writer.println(outgoing.getText());
                writer.flush();
            } catch(Exception ex) {
                ex.printStackTrace();
            }
            outgoing.setText("");
            outgoing.requestFocus();
        }
    }

    public class ExitButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent ev) {
            System.exit(0); //exit the program
        }
    }

    public class IncomingReader implements Runnable {
        public void run() {
            String message;
            try {
                while ((message = reader.readLine()) != null) {
                    System.out.println("read " + message);
                    imcoming.append(message + "\n");
                }
            } catch(Exception ex) {
                ex.printStackTrace();
            }
        }
    }

}

Recommended Answers

All 5 Replies

If you try to compile your code you will get a list of error messages that you should post here. The messages contain information that explains what and where the error is.

Please copy and paste here the full text of your error messages.

I've figured out some of the errors. The JButton one and the getContent() ones are fixed. It's just wherever "incoming" pops up.

All the error message reads is "incoming cannot be resolved" which isn't very helpful to me.

Any pointers would be great.

Please copy and paste here the full text of your error messages.

For example:

TestCode1.java:71: cannot find symbol
symbol  : variable varXX
location: class TestCode1
      System.out.println("varX=" + varXX);  // varX=1
                                   ^

You have done a small mistake that you have written imcoming in place of incoming.
So, you must first correct this...

Thank you, Dupron, that was the problem the whole time! I can now see if it works. Sometimes, staring at code just doesn't help!

Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.