Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
|
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 31st, 2004, 02:48 PM
Authorized User
 
Join Date: Dec 2004
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default does java network programming support javax.swing

i am making a chat server and now i want to add menu to that but i have some errors like it does not supporting JTextarea .plz any body tell me that does java network programming supports javax.swing class and how i can add menus and other components to this

HumaMunir
__________________
HumaMunir
 
Old January 3rd, 2005, 03:42 AM
Registered User
 
Join Date: Dec 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes javax.swing supports network programming,it doesn't matter either we are making sigle unit programme or network programme......plz specify that you want to make what kind of menu...then i ll explain u further ...thnk you ...from yaseen...yammirza@hotmail.com

alexander of java
 
Old January 3rd, 2005, 12:33 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes you can use swing to create this program.

It sounds like you may not have all the API's you need set up with your compiler, if the message is specific to an API make sure you have it installed properly.



 
Old January 3rd, 2005, 02:24 PM
Authorized User
 
Join Date: Dec 2004
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for reply

i have used JTextArea with textArea.append.text() command to get the data on text area but there is an error message on append.text. here is the code plz tell me that am i using a wrong command or some thing else . plz help me soon.


/**
 * Chat Applet (Graphical chat client)
 * -----------------------------------
 * java ChatApplet [host-name] //default localhost:6666
 *
 * by Huma Munir Talpur (talpur111@hotmail.com)
 *
 * for JDK 1.0.2 and above
 *
 */


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

public class ChatApplet extends Frame implements Runnable
{

    static String command; //shared resource
    Socket socket;
    static final int portNumber=1666;
    //Thread commReader=null;

    DataInputStream input;
    PrintStream output;

    String alias=null;
    boolean logging=true;

    //TextArea textArea;
    //TextField comm;
    //Label status;

    JTextArea textArea;
    JTextField comm;
    JLabel status;

    public static void main(String[] argv) throws IOException
    {
        InetAddress clientAddr;
        if(argv.length==0)
            clientAddr=InetAddress.getLocalHost();
        else
            clientAddr=InetAddress.getByName(argv[0]);

        System.out.println("Connecting to Chat Server at "+clientAddr);


        ChatApplet ca=new ChatApplet(clientAddr);
        // ca.show();
        Thread t=new Thread(ca);
        t.start();
    }

    public ChatApplet(InetAddress adx)
    {
        super(" Welcome To Induljin Chat");
        //AWT
        resize(300,400);



    //setLayout(new BorderLayout());
        //textArea=new TextArea(20,20);
        //textArea.setEditable(false);
        //comm=new TextField(30);
        //status=new Label("Please type usser name to Connect..");
        //add("North",comm);
        //add("Center",textArea);
        //add("South",status);



        textArea=new JTextArea(20,20);
        //textArea.setEditable(false);
        comm=new JTextField(30);
        status=new JLabel("Please type usser name to Connect..");
        getContentPane().add(comm,BorderLayout.NORTH);
        getContentPane().add(textArea,BorderLayout.CENTER) ;
        getContentPane().add(status,BorderLayout.SOUTH);




        //CommandReader cr=new CommandReader(this);
        //commReader=new Thread(cr);
        command="xXx";
        try
        {
            socket=new Socket(adx,portNumber);
            input=new DataInputStream(socket.getInputStream());
            output=new PrintStream(socket.getOutputStream());
            //commReader.start();
        }
        catch(IOException e)
        {
            status.setText("Abnormal chat client socket condition:"+e);
            repaint();
        }
}

    public boolean action(Event e,Object o)
    {
        if(e.target==comm)
        {
            synchronized(command)
            {
                command=comm.getText();
                comm.setText("");
            }
            return true;
        }
        return false;
    }

    public boolean handleEvent(Event evt)
    {
        if (evt.id == Event.WINDOW_DESTROY)
        {
            synchronized(command)
            {
                command="exit";
            }
        }

      /*if (evt.id == Event.ACTION_EVENT)
      {
         String text = textField.getText();
         textArea.appendText(text + "\n");
         textField.selectAll();
      } */
        return super.handleEvent(evt);
    }

    public void run()
    {
        show();
        try
        {
            doLoop();
            input.close();
            output.close();
            socket.close();
            System.exit(0);
        }
        catch(IOException e)
        {
            status.setText("Abnormal chat client socket condition:"+e);;
            repaint();
        }

    }

    public void doLoop() throws IOException
    {
        String nullString=new String("xXx");
        String data=nullString;
        while(true)
        {

            synchronized(command)
            {
                //System.out.println("Leggo synch");
                if(!command.equals("xXx"))
                {
                    data=command;
                    //reset
                    command="xXx";
                    //System.out.println("Leggo e prendo");
                }
            } //end synch

            if(!data.equals("xXx"))
            {
                if(logging)
                {
                    //il primo messaggio
                    logging=false;
                    alias=data;
                    status.setText("Logging as:"+alias);
                    repaint();
                    output.println("S"+alias); //Starting new chat
                }
                else
                {
                    status.setText("Sending msg:"+data);
                    textArea.setEditable(true);

                textArea.appendText(">"+data+"\n");
                    repaint();
                    output.println("D"+data); //sending data
                }

                if(data.equals("exit"))
                {
                    status.setText("Exiting Chat...");
                    repaint();
                    output.println("X"+alias);
                    break;
                }
            }

            //System.out.println("read socket");
            data=input.readLine();
            if(!data.equals("xXx"))
            {
                //System.out.println(data);
                textArea.appendText("<"+data+"\n");
                repaint();
            }
            data=nullString;
        }
    }

}



HumaMunir
 
Old January 4th, 2005, 06:20 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Whats the error message?

 
Old January 5th, 2005, 02:30 PM
Authorized User
 
Join Date: Dec 2004
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

its not recognizing that getContentPane() saying (UNKNOWN SYMBOL) you can see it in code and also saying that textArea.appendText saying that (UNKNOWN SYMBOL).
  you can also compile and check it.
                                 Thanks for your concern

HumaMunir
 
Old January 5th, 2005, 06:19 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Havent used this stuff for a while, so I may be very wrong with what I have done. I couldnt get it to compile at all. To get it to compile and run I changed

JTextArea textArea to
TextArea textArea //change all references to textArea

and I added

JFrame f = new JFrame();
f.getContentPane().add(comm,BorderLayout.NORTH);
f.getContentPane().add(textArea,BorderLayout.CENTE R);
f.getContentPane().add(status,BorderLayout.SOUTH);

Like I say I got the program to compile and run, but I do not know whether this is what you wanted.

 
Old January 6th, 2005, 02:46 PM
Authorized User
 
Join Date: Dec 2004
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks friend

                  but it is not working .In fact it is also my frist time to work with awt and javax swing .it has compiled and run with the server but the applet does not appear and after some time it shows the exceptions like
        exception in thread "main" java.lang .nullpointer exception
                 at java.awt.container.addimpl(container.java:344) and some other if you say i will send you the server class so you can
check it yourself


HumaMunir
 
Old January 6th, 2005, 04:14 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I saw the errors when I ran it, but I dont think they are errors with the GUI, you should try searching the Sun Java forums http://forum.java.sun.com/index.jspa

I created a chat client some time ago and there were a number of posts on their relating to your problem.

 
Old January 8th, 2005, 06:50 PM
Authorized User
 
Join Date: Dec 2004
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the site. It really helped me a lot.

HumaMunir





Similar Threads
Thread Thread Starter Forum Replies Last Post
java.awt.event error in swing app. Mr. Monkey Java GUI 2 March 27th, 2007 09:48 AM
Network Programming simonongsk Visual Basic 2005 Basics 0 December 13th, 2006 10:35 PM
javax.swing on linux sunstrike Java GUI 3 July 11th, 2003 02:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.