Results 1 to 6 of 6

Thread: My Chat program

  1. #1
    WJM
    WJM is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Question My Chat program

    Hi, Im new to Java and programing in general. Currently, I am working on a chat program for me and my friends to use, and was wondering how I would add text submitted to the overall chunk of text (I don't know what its called). Obviously, completion is a long way off, but thats fine. So don't give me crap about not having the skills to make an online chat program, I know that, it just gives me a reason to learn the skills. anyway, heres my rather clunky bit of code.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.applet.*;
    public class chatStuff extends JFrame implements ActionListener {
    	public String name = ("Name");
    	public void framer(){
    		//setup
    		this.setTitle(name + "'s Chat Client");
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setAlwaysOnTop(true);
    		///setup
    		//components
    		JTextArea txt = new JTextArea("Say hello!", 20, 30);
    		String mytype = "" ;
    		JTextField typehere = new JTextField(mytype, 20);
    		JButton submit = new JButton("Say");
    		JLabel logo = new JLabel(new ImageIcon ("/home/***/logo.png"));
    		///components
    		//containers
    		JPanel pantxt = new JPanel();
    		///containers
    		//Component adding
    		pantxt.add(logo);
    		pantxt.add(txt);
    		pantxt.add(typehere);
    		pantxt.add(submit);
    		this.add(pantxt);
    		this.add(pantxt);
    		this.setVisible(true);
    		this.pack();
    		///component adding
    		//layout;
    		this.setSize(400, 450);
    		this.setMaximizedBounds(getBounds());
    	}
    	public void actionPerformed(ActionEvent arg0) {
    	
    	}
    
    }
    your help would be greatly, appreciated

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    how I would add text submitted to the overall chunk of text
    Can you explain what you mean here?
    By text I understand the something like contents of a String that is to be displayed to a human for information.

  3. #3
    WJM
    WJM is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    sorry, thats kinda vague. I meant how would you take a string that the user types into the textfield "typehere" and add it to the textarea. or is there a better way to do this?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    Have you read the API doc for the textfield class and the textarea class? There are methods to get the contents and methods to add contents. You want to get the contents of the textfield and add it to the contents of the textarea.

    The API doc: Java Platform SE 6

  5. #5
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Some remarks:
    - I see a simple GUI, lacking a main(). Probably you had one but did'n't post it: best to post code as a SSCCE (small self contained compiling example).
    - You called your class chatStuff. Convention is to start classsnames with a capital.
    - The main problem is that the gui-components are declared within the method framer(): their scope is that method, so your actionPerformed() doesn't see them or know they exist, and so can't read or write them: declare them as globals, outside the methods, as f.i. private JTextArea txt =...;'
    - Your button will only work if you add a listener to it: submit.addActionListener(this)
    That's it for now, otherwise you're chatting tomorrow without learning anything
    No bug ever had to calculate its fitnessfunction.

  6. #6
    WJM
    WJM is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Jodokus View Post
    Some remarks:
    - I see a simple GUI, lacking a main(). Probably you had one but did'n't post it: best to post code as a SSCCE (small self contained compiling example).
    - You called your class chatStuff. Convention is to start classsnames with a capital.
    - The main problem is that the gui-components are declared within the method framer(): their scope is that method, so your actionPerformed() doesn't see them or know they exist, and so can't read or write them: declare them as globals, outside the methods, as f.i. private JTextArea txt =...;'
    - Your button will only work if you add a listener to it: submit.addActionListener(this)
    That's it for now, otherwise you're chatting tomorrow without learning anything
    thanks Jodokus, that helps a lot. I do have a main(), but like you said i didn't show it. I also do have an action performed, i just didn't know what to put in it, but now I think I know what to do, thanks again.

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. MultiThreading Chat Program
    By Noelf21 in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-23-2009, 10:49 AM
  3. Help with chat client program
    By srivigneshwar in forum New To Java
    Replies: 1
    Last Post: 04-03-2009, 06:32 PM
  4. simple chat program
    By munishmhr in forum Networking
    Replies: 2
    Last Post: 03-25-2009, 04:00 PM
  5. Java Program chat
    By susan in forum Advanced Java
    Replies: 1
    Last Post: 07-25-2007, 09:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •