Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 23 of 23

Thread: frame is not opening in chat application

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default frame is not opening in chat application

    I'm making a chat application using client/server. I have 3 files - server, login and client.
    Nothing is happening when I click the login button, It should open the client page.

    I am getting an error in the client file which I think is causing the problem.

    Here is the login page (some of the code is generated from netbeans because I am using a JFrame)
    the problem is probably in button event handler:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import javax.swing.JFrame;
     
    public class Login extends javax.swing.JFrame implements ActionListener{
            JFrame frame1;
        /**
         * Creates new form Login
         */
        public Login() {
            initComponents();
           // frame1 = new JFrame("Login Page");
        }
     
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            tf = new javax.swing.JTextField();
            button = new javax.swing.JButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jLabel1.setText("Chat Application");
     
            jLabel2.setText("Enter your login name");
     
            button.setText("Login");
            button.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buttonActionPerformed(evt);
                }
            });
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(138, 138, 138)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addComponent(jLabel1)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(130, 130, 130)
                            .addComponent(tf, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(148, 148, 148)
                            .addComponent(button)))
                    .addContainerGap(141, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(44, 44, 44)
                    .addComponent(jLabel1)
                    .addGap(70, 70, 70)
                    .addComponent(jLabel2)
                    .addGap(18, 18, 18)
                    .addComponent(tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(39, 39, 39)
                    .addComponent(button)
                    .addContainerGap(58, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>                        
     
        private void buttonActionPerformed(java.awt.event.ActionEvent evt) {                                       
     
                         public void actionPerformed(ActionEvent e){
    		String name="";
    		try{
    			name=tf.getText();
    			frame1.dispose();
    			MyClient mc = new MyClient(name);
     
                    }
     
                    catch (IOException te){
                    }
     
    }
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Login().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton button;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JTextField tf;
        // End of variables declaration                   
     
     
    }

    here is the client page:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;
    import java.util.ArrayList;
    import java.util.Iterator;
     
     
    // class is used to maintaning the list of user name
     
    //class is used to received the messages
    public class MyClient extends javax.swing.JFrame {
     
            Socket s,s1,s2;
    	DataInputStream din;
    	DataOutputStream dout;
    	DataOutputStream dlout;
    	DataOutputStream dout1;
    	DataInputStream din1;
    	String name;
            JFrame frame;
           // DefaultListModel list;
            //DefaultListModel list1;
     
     
     
        public MyClient(String name) throws IOException{
            initComponents();
     
                    //list=new DefaultListModel();
    		//list1=new DefaultListModel();
                   // list=new JList(model);
    		//list1=new JList(model1);
     
                    s=new Socket("localhost",1004);	//creates a socket object
    		s1=new Socket("localhost",1004);
    		s2=new Socket("localhost",1004);
    	    //create inputstream for a particular socket
    		din=new DataInputStream(s.getInputStream());
    		//create outputstream
    		dout=new DataOutputStream(s.getOutputStream());
    		//sending a message for login
    		dout.writeUTF(name+" has Logged in");	
    		dlout=new DataOutputStream(s1.getOutputStream());
    		dout1=new DataOutputStream(s2.getOutputStream());
    		din1=new DataInputStream(s2.getInputStream());
     
    // creating a thread for maintaning the list of user name
    		My1 m1=new My1(dout1,model1,name,din1);
    		Thread t1=new Thread(m1);
    		t1.start();			
    	//creating a thread for receiving a messages
    		My m=new My(din,model);
    		Thread t=new Thread(m);
    		t.start();
     
     
        }
     
     
              public void windowClosing(WindowEvent w){
    		try{
    			dlout.writeUTF(name);
    			dlout.flush();	
    			Thread.currentThread().sleep(1000);
    			System.exit(1);
    			}catch(Exception oe){}
    		}
     
     
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
     
        private void buttonActionPerformed(java.awt.event.ActionEvent evt) {                                       
            String str="";
    			str=tf.getText();
    			tf.setText("");
    			str=name+": > "+str;
    			try{
    			dout.writeUTF(str);
    			System.out.println(str);
    			dout.flush();
    			}catch(IOException ae){System.out.println(ae);}
        }                                      
     
        private void loutActionPerformed(java.awt.event.ActionEvent evt) {                                     
                            frame.dispose();
    			try{
    				 //sending the message for logout
                                dout.writeUTF(name+" has Logged out");
                                dlout.writeUTF(name);
                                dlout.flush();
                                Thread.currentThread().sleep(1000);
                                System.exit(1);
    			}catch(Exception oe){}
        }                                    
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
     
            /* Create and display the form */
                  java.awt.EventQueue.invokeLater(new Runnable() {
                 public void run() {
                    new MyClient().setVisible(true); //error is here
                }
      });
     
     
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton button;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JScrollPane jScrollPane2;
        private javax.swing.JButton lout;
        private javax.swing.JList model;
        private javax.swing.JList model1;
        private javax.swing.JTextField tf;
        // End of variables declaration                   
    }
    class My1 implements Runnable{
    	DataOutputStream dout1;
                  JList model1;
    	DataInputStream din1;
    	String name,lname;
    	ArrayList alname=new ArrayList(); //stores the list of user names
    	ObjectInputStream obj; // read the list of user names
    	int i=0;
     
        My1(DataOutputStream dout1, JList model1, String name, DataInputStream din1) {
                    this.dout1=dout1;
    		this.model1=model1;
    		this.name=name;
    		this.din1=din1;
        }
     
     
            @Override
    	public void run(){
    		try{
     
    		dout1.writeUTF(name);  // write the user name in output stream
    		while(true){
    			obj=new ObjectInputStream(din1);
    			//read the list of user names
    			alname=(ArrayList)obj.readObject(); 
    			if(i>0) 
                            model1.removeAll();
    			Iterator i1=alname.iterator();
    			System.out.println(alname);
    			while(i1.hasNext()){
    				lname=(String)i1.next();
    				i++;
    				 //add the user names in list box
    				//model1.addElement(lname);
                                    model1.add(lname, model1);
    				}
    			}
    		}catch(Exception oe){}
    	}
    }
    //class is used to received the messages
    class My implements Runnable{
    	DataInputStream din;
    	DefaultListModel model;}
     
        My(DataInputStream din, JList list) {
                    this.din=din;
    		this.model=model;
        }
     
     
    	public void run(){
    		String str1="";
    		while(true){
    			try{
    				str1=din.readUTF(); // receive the message
    				// add the message in list box
    				model.addElement(str1);
    			}catch(Exception e){}
    		}
    	}
    }

    the line that is causing the error is :
     new MyClient().setVisible(true);
    it says required: string, found: no arguments

    when i try putting name in the brackets the error then says non static variable name cannot be referenced from a static context


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: frame is not opening in chat application

    it says required: string, found: no arguments
    The compiler does not find a constructor with no args. The one it finds has a String argument.
    Try calling the constructor with a String as argument.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: frame is not opening in chat application

    I tried that. I changed the code to new MyClient(name).setVisible(true);
    and the error says non static variable name cannot be referenced from a static context

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: frame is not opening in chat application

    non static variable name cannot be referenced from a static context
    main() is a static method and can only access static variables in its class.
    Make name static or use a String literal.

    Where is there a variable: name that has a useful value when the main() method is executed?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: frame is not opening in chat application

    ok so i changed String name to static String name; and now the error is gone
    but the client page is still not displaying when i click the login button on the login page

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: frame is not opening in chat application

    but the client page is still not displaying when i click the login button on the login page
    Try debugging the code by adding some println statements to print out messages and the values of all the important variables as the code executes so that you can see where the code is executing and from the print outs you should be able to see what is happening in the program.


    The posted code does not compile without errors.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: frame is not opening in chat application

    ok the problem is the login page its not running the actionPerformed function when the button is clicked.
    how can i fix this code?

  8. #8
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: frame is not opening in chat application

    Did you add an actionlistener/performed to the button?
    This can be done with : Button.addActionListener(this);
    (at least i thought your button was called "Button")

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: frame is not opening in chat application

    The posted code is full of compiler errors. I can not compile the code for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: frame is not opening in chat application

    that is because i didnt post the 3rd file
    here it is:
    import java.io.*;
    import java.net.*;
    import java.util.*;
     
    public class MyServer{
    	ServerSocket ss;
    	Socket s;
    	ArrayList al=new ArrayList();
    	ArrayList al1=new ArrayList();
    	ArrayList al2=new ArrayList();
    	ArrayList alname=new ArrayList();
    	Socket s1,s2;
     
    	MyServer()throws IOException{
                System.out.println("Server started....awaiting connections");
    		ss=new ServerSocket(1004);	// create server socket
    		while(true){
    			s=ss.accept();	//accept the client socket
    			s1=ss.accept();
    			s2=ss.accept();
    			al.add(s);	// add the client socket in arraylist
    			al1.add(s1);
    			al2.add(s2);
    			System.out.println("Client is Connected");
    			MyThread2 m=new MyThread2(s2,al2,alname); //new thread for maintaning the list of user name
    			Thread t2=new Thread(m);
    			t2.start();
     
    			MyThread r=new MyThread(s,al);//new thread for receive and sending the messages
    			Thread t=new Thread(r);
    			t.start();
     
    			MyThread1 my=new MyThread1(s1,al1,s,s2); // new thread for update the list of user name
    			Thread t1=new Thread(my);
    			t1.start();
    		}
    	}
    	public static void main(String[] args){
    		try{
    			new MyServer();			
    		}catch (IOException e){}
    	}
    }
    //class is used to update the list of user name
    class MyThread1 implements Runnable{
    	Socket s1,s,s2;
    	static ArrayList al1;
    	DataInputStream ddin;
    	String sname;
    	MyThread1(Socket s1,ArrayList al1,Socket s,Socket s2){
    		this.s1=s1;
    		this.al1=al1;
    		this.s=s;
    		this.s2=s2;
    	}
    	public void run(){	
    		try{
    		ddin=new DataInputStream(s1.getInputStream());
    		while(true){
    		sname=ddin.readUTF();
    		System.out.println("Exit  :"+sname);
    		MyThread2.alname.remove(sname);//remove the logout user name from arraylist
    		MyThread2.every();
    		al1.remove(s1);
    		MyThread.al.remove(s);
    		MyThread2.al2.remove(s2);
    		if(al1.isEmpty())
    			System.exit(0); //all client has been logout
    		}
    		}catch(Exception ie){}
    	}
    }
     
    // class is used to maintain the list of all online users
    class MyThread2 implements Runnable{
    	Socket s2;
    	static ArrayList al2;
    	static ArrayList alname;
    	static DataInputStream din1;	
    	static DataOutputStream dout1;
     
    	MyThread2(Socket s2,ArrayList al2,ArrayList alname){
    		this.s2=s2;
    		this.al2=al2;
    		this.alname=alname;
    	}
    	public void run(){
    		try{
    		din1= new DataInputStream(s2.getInputStream());
    		alname.add(din1.readUTF());	// store the user name in arraylist
    		every();
    		}catch(Exception oe){System.out.println("Main expression"+oe);}
    	}
    	// send the list of user name to all client
    	static void every()throws Exception{
    		Iterator i1=al2.iterator();
    		Socket st1;		
     
    		while(i1.hasNext()){
    			st1=(Socket)i1.next();
    			dout1=new DataOutputStream(st1.getOutputStream());
    			ObjectOutputStream obj=new ObjectOutputStream(dout1);
    			obj.writeObject(alname); //write the list of users in stream of all clients
    			dout1.flush();
    			obj.flush();
    		}
    	}
    }
    //class is used to receive the message and send it to all clients
    class MyThread implements Runnable{
    	Socket s;
    	static ArrayList al;
    	DataInputStream din;
    	DataOutputStream dout;
     
    	MyThread(Socket s, ArrayList al){
    		this.s=s;
    		this.al=al;
    	}
    	public void run(){
    		String str;
    		int i=1;
    		try{
    		din=new DataInputStream(s.getInputStream());
    		}catch(Exception e){}
     
    		while(i==1){
    				try{
     
    					str=din.readUTF(); //read the message
    					distribute(str);
    				}catch (IOException e){}
    			}
    	}
    	// send it to all clients
    	public void distribute(String str)throws IOException{
    		Iterator i=al.iterator();
    		Socket st;
    		while(i.hasNext()){
    			st=(Socket)i.next();
    			dout=new DataOutputStream(st.getOutputStream());
    			dout.writeUTF(str);
    			dout.flush();
    		}
    	}
    }
    This file (MyServer.java) needs to be run first, then the login file
    I don't have any compile errors in my code

  11. #11
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: frame is not opening in chat application

    he should use an IDE like Eclipse or netbeans

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: frame is not opening in chat application

    That class definition is not the problem. That third class is not used to compile the first two classes.
    For an example of the compiler problems, See this bit:
     
    //class is used to received the messages
    class My implements Runnable{
    	DataInputStream din;
    	DefaultListModel model;}   //<<<<<<<<<< this } ends the class
     
        My(DataInputStream din, JList list) {  //<<<< this is NOT in a class

    And there are many others like that.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: frame is not opening in chat application

    I am using netbeans

    --- Update ---

    i got this code from a website. here is the orginal code:
    i
    mport javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
     
    // Login class which takes a user name and passed it to client class
    public class Login implements ActionListener{
    	JFrame frame1;
    	JTextField tf;
    	JButton button;
    	JLabel heading;
    	JLabel label;
    	public static void main(String[] args){
    		new Login();
    	}
    	public Login(){
    		frame1 = new JFrame("Login Page");
    		tf=new JTextField();
    		button=new JButton("Login");
    		heading=new JLabel("Chat Server");
    		heading.setFont(new Font("Impact", Font.BOLD,40));
    		label=new JLabel("Enter you Login Name");
    		label.setFont(new Font("Serif", Font.PLAIN, 24));
    		JPanel panel = new JPanel();
    		button.addActionListener(this);
    		panel.add(heading);panel.add(tf);panel.add(label);
    		panel.add(button);
    		heading.setBounds(30,20,280,80);
    		label.setBounds(20,100,250,60);
    		tf.setBounds(50,150,150,30);
    		button.setBounds(70,200,90,30);
    		frame1.add(panel);
    		panel.setLayout(null);
    		frame1.setSize(300, 300);
                    frame1.setVisible(true);
    		frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    	// pass the user name to MyClient class
    	public void actionPerformed(ActionEvent e){
    		String name="";
    		try{
    			name=tf.getText();
    			frame1.dispose();
    			MyClient mc=new MyClient(name);
    		}catch (IOException te){}
    	}
    }

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;
    import java.util.ArrayList;
    import java.util.Iterator;
     
    //create the GUI of the client side
    public class MyClient extends WindowAdapter implements ActionListener{
    	JFrame frame;
    	JList list;
    	JList list1;
    	JTextField tf;
    	DefaultListModel model;
    	DefaultListModel model1;
    	JButton button;
    	JButton lout;
    	JScrollPane scrollpane;
    	JScrollPane scrollpane1;
    	JLabel label;
    	Socket s,s1,s2;
    	DataInputStream din;
    	DataOutputStream dout;
    	DataOutputStream dlout;
    	DataOutputStream dout1;
    	DataInputStream din1;
    	String name;
     
    	MyClient(String name)throws IOException{
    		frame = new JFrame("Client Side");
    		tf=new JTextField();
    		model=new DefaultListModel();
    		model1=new DefaultListModel();
    		label=new JLabel("Message");
    		list=new JList(model);
    		list1=new JList(model1);
    		button=new JButton("Send");
    		lout=new JButton("Logout");
    		scrollpane=new JScrollPane(list);
    		scrollpane1=new JScrollPane(list1);
    		JPanel panel = new JPanel();
    		button.addActionListener(this);
    		lout.addActionListener(this);
    		panel.add(tf);panel.add(button);panel.add(scrollpane);
    		panel.add(label);panel.add(lout);
    		panel.add(scrollpane1);
    		scrollpane.setBounds(10,20,180,150);
    		scrollpane1.setBounds(250,20,100,150);
    		label.setBounds(20,180,80,30);
    		tf.setBounds(100,180,140,30);
    		button.setBounds(260,180,90,30);
    		lout.setBounds(260,230,90,30);
    		frame.add(panel);
    		panel.setLayout(null);
    		frame.setSize(400, 400);
    	   	frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.name=name;
    		frame.addWindowListener(this);
     
    		s=new Socket("localhost",1004);	//creates a socket object
    		s1=new Socket("localhost",1004);
    		s2=new Socket("localhost",1004);
    	    //create inputstream for a particular socket
    		din=new DataInputStream(s.getInputStream());
    		//create outputstream
    		dout=new DataOutputStream(s.getOutputStream());
    		//sending a message for login
    		dout.writeUTF(name+" has Logged in");	
    		dlout=new DataOutputStream(s1.getOutputStream());
    		dout1=new DataOutputStream(s2.getOutputStream());
    		din1=new DataInputStream(s2.getInputStream());
     
    // creating a thread for maintaning the list of user name
    		My1 m1=new My1(dout1,model1,name,din1);
    		Thread t1=new Thread(m1);
    		t1.start();			
    	//creating a thread for receiving a messages
    		My m=new My(din,model);
    		Thread t=new Thread(m);
    		t.start();
      	}
    	public void actionPerformed(ActionEvent e){
    		// sending the messages
    		if(e.getSource()==button){	
    			String str="";
    			str=tf.getText();
    			tf.setText("");
    			str=name+": > "+str;
    			try{
    			dout.writeUTF(str);
    			System.out.println(str);
    			dout.flush();
    			}catch(IOException ae){System.out.println(ae);}
    		}
    		// client logout
    		if (e.getSource()==lout){
    			frame.dispose();
    			try{
    				 //sending the message for logout
    			dout.writeUTF(name+" has Logged out");
    			dlout.writeUTF(name);
    			dlout.flush();
    			Thread.currentThread().sleep(1000);
    			System.exit(1);
    			}catch(Exception oe){}
    		}
    	}
    	public void windowClosing(WindowEvent w){
    		try{
    			dlout.writeUTF(name);
    			dlout.flush();	
    			Thread.currentThread().sleep(1000);
    			System.exit(1);
    			}catch(Exception oe){}
    		}
    }
     
    // class is used to maintaning the list of user name
    class My1 implements Runnable{
    	DataOutputStream dout1;
    	DefaultListModel model1;	
    	DataInputStream din1;
    	String name,lname;
    	ArrayList alname=new ArrayList(); //stores the list of user names
    	ObjectInputStream obj; // read the list of user names
    	int i=0;
    	My1(DataOutputStream dout1,DefaultListModel model1,String name,DataInputStream din1){
    		this.dout1=dout1;
    		this.model1=model1;
    		this.name=name;
    		this.din1=din1;
    	}
    	public void run(){
    		try{
    		dout1.writeUTF(name);  // write the user name in output stream
    		while(true){
    			obj=new ObjectInputStream(din1);
    			//read the list of user names
    			alname=(ArrayList)obj.readObject(); 
    			if(i>0)
    			model1.clear(); 
    			Iterator i1=alname.iterator();
    			System.out.println(alname);
    			while(i1.hasNext()){
    				lname=(String)i1.next();
    				i++;
    				 //add the user names in list box
    				model1.addElement(lname);
    				}
    			}
    		}catch(Exception oe){}
    	}
    }
    //class is used to received the messages
    class My implements Runnable{
    	DataInputStream din;
    	DefaultListModel model;
    	My(DataInputStream din, DefaultListModel model){
    		this.din=din;
    		this.model=model;
    	}
    	public void run(){
    		String str1="";
    		while(true){
    			try{
    				str1=din.readUTF(); // receive the message
    				// add the message in list box
    				model.addElement(str1);
    			}catch(Exception e){}
    		}
    	}
    }

    I was trying to put this code into a JFrame and use event handlers for the buttons

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: frame is not opening in chat application

    So the originally posted code wasn't ever compiled or tested?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: frame is not opening in chat application

    yes i did test it and it worked. thats why i started making the changes because the code was fine

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: frame is not opening in chat application

    yes i did test it and it worked. thats why i started making the changes because the code was fine
    Sorry, the original code in post#1 is not fine. It has lots of compiler errors that must be fixed before it can be executed for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: frame is not opening in chat application

    yes but that code is already altered by him

  18. #18
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: frame is not opening in chat application

    sorry i though you mean't the code i just posted a minute ago - that code is fine. maybe compare the two and see whats the problem?
    i didnt change any of the code. i just want the code to work using a JFrame and the button event handlers

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: frame is not opening in chat application

    Please post the code you are currently working on and any questions about the problems you are having with it.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: frame is not opening in chat application

    ok here is all 3 files (sorry for all the extra stuff netbeans puts in):
    import java.io.*;
    import java.net.*;
    import java.util.*;
     
    public class MyServer{
    	ServerSocket ss;
    	Socket s;
    	ArrayList al=new ArrayList();
    	ArrayList al1=new ArrayList();
    	ArrayList al2=new ArrayList();
    	ArrayList alname=new ArrayList();
    	Socket s1,s2;
     
    	MyServer()throws IOException{
                System.out.println("Server started....awaiting connections");
    		ss=new ServerSocket(1004);	// create server socket
    		while(true){
    			s=ss.accept();	//accept the client socket
    			s1=ss.accept();
    			s2=ss.accept();
    			al.add(s);	// add the client socket in arraylist
    			al1.add(s1);
    			al2.add(s2);
    			System.out.println("Client is Connected");
    			MyThread2 m=new MyThread2(s2,al2,alname); //new thread for maintaning the list of user name
    			Thread t2=new Thread(m);
    			t2.start();
     
    			MyThread r=new MyThread(s,al);//new thread for receive and sending the messages
    			Thread t=new Thread(r);
    			t.start();
     
    			MyThread1 my=new MyThread1(s1,al1,s,s2); // new thread for update the list of user name
    			Thread t1=new Thread(my);
    			t1.start();
    		}
    	}
    	public static void main(String[] args){
    		try{
    			new MyServer();			
    		}catch (IOException e){}
    	}
    }
    //class is used to update the list of user name
    class MyThread1 implements Runnable{
    	Socket s1,s,s2;
    	static ArrayList al1;
    	DataInputStream ddin;
    	String sname;
    	MyThread1(Socket s1,ArrayList al1,Socket s,Socket s2){
    		this.s1=s1;
    		this.al1=al1;
    		this.s=s;
    		this.s2=s2;
    	}
    	public void run(){	
    		try{
    		ddin=new DataInputStream(s1.getInputStream());
    		while(true){
    		sname=ddin.readUTF();
    		System.out.println("Exit  :"+sname);
    		MyThread2.alname.remove(sname);//remove the logout user name from arraylist
    		MyThread2.every();
    		al1.remove(s1);
    		MyThread.al.remove(s);
    		MyThread2.al2.remove(s2);
    		if(al1.isEmpty())
    			System.exit(0); //all client has been logout
    		}
    		}catch(Exception ie){}
    	}
    }
     
    // class is used to maintain the list of all online users
    class MyThread2 implements Runnable{
    	Socket s2;
    	static ArrayList al2;
    	static ArrayList alname;
    	static DataInputStream din1;	
    	static DataOutputStream dout1;
     
    	MyThread2(Socket s2,ArrayList al2,ArrayList alname){
    		this.s2=s2;
    		this.al2=al2;
    		this.alname=alname;
    	}
    	public void run(){
    		try{
    		din1= new DataInputStream(s2.getInputStream());
    		alname.add(din1.readUTF());	// store the user name in arraylist
    		every();
    		}catch(Exception oe){System.out.println("Main expression"+oe);}
    	}
    	// send the list of user name to all client
    	static void every()throws Exception{
    		Iterator i1=al2.iterator();
    		Socket st1;		
     
    		while(i1.hasNext()){
    			st1=(Socket)i1.next();
    			dout1=new DataOutputStream(st1.getOutputStream());
    			ObjectOutputStream obj=new ObjectOutputStream(dout1);
    			obj.writeObject(alname); //write the list of users in stream of all clients
    			dout1.flush();
    			obj.flush();
    		}
    	}
    }
    //class is used to receive the message and send it to all clients
    class MyThread implements Runnable{
    	Socket s;
    	static ArrayList al;
    	DataInputStream din;
    	DataOutputStream dout;
     
    	MyThread(Socket s, ArrayList al){
    		this.s=s;
    		this.al=al;
    	}
    	public void run(){
    		String str;
    		int i=1;
    		try{
    		din=new DataInputStream(s.getInputStream());
    		}catch(Exception e){}
     
    		while(i==1){
    				try{
     
    					str=din.readUTF(); //read the message
    					distribute(str);
    				}catch (IOException e){}
    			}
    	}
    	// send it to all clients
    	public void distribute(String str)throws IOException{
    		Iterator i=al.iterator();
    		Socket st;
    		while(i.hasNext()){
    			st=(Socket)i.next();
    			dout=new DataOutputStream(st.getOutputStream());
    			dout.writeUTF(str);
    			dout.flush();
    		}
    	}
    }

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import javax.swing.JFrame;
     
     
    public class Login extends javax.swing.JFrame implements ActionListener{
            JFrame frame1;
        /**
         * Creates new form Login
         */
        public Login() {
            initComponents();
           // frame1 = new JFrame("Login Page");
        }
     
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            tf = new javax.swing.JTextField();
            button = new javax.swing.JButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jLabel1.setText("Chat Application");
     
            jLabel2.setText("Enter your login name");
     
            button.setText("Login");
            button.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buttonActionPerformed(evt);
                }
            });
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(138, 138, 138)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addComponent(jLabel1)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(130, 130, 130)
                            .addComponent(tf, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(148, 148, 148)
                            .addComponent(button)))
                    .addContainerGap(141, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(44, 44, 44)
                    .addComponent(jLabel1)
                    .addGap(70, 70, 70)
                    .addComponent(jLabel2)
                    .addGap(18, 18, 18)
                    .addComponent(tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(39, 39, 39)
                    .addComponent(button)
                    .addContainerGap(58, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>                        
     
        private void buttonActionPerformed(java.awt.event.ActionEvent evt) {                                       
    //        // pass the user name to MyClient class
    //        String name="";
    //		try{
    //			name=tf.getText();
    //			frame1.dispose();
    //			MyClient mc=new MyClient(name);
    //		}catch (IOException te){}
        }                                      
     
     
        public void actionPerformed(ActionEvent e){
    		String name="";
    		try{
    			name=tf.getText();
    			frame1.dispose();
    			MyClient mc=new MyClient(name);
     
                    }
     
                    catch (IOException te){
                    }
     
    }
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Login().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton button;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JTextField tf;
        // End of variables declaration                   
     
     
    }

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
     
    // class is used to maintaning the list of user name
     
    //class is used to received the messages
    public class MyClient extends javax.swing.JFrame {
     
            Socket s,s1,s2;
    	DataInputStream din;
    	DataOutputStream dout;
    	DataOutputStream dlout;
    	DataOutputStream dout1;
    	DataInputStream din1;
    	static String name;
            JFrame frame;
           // DefaultListModel list;
            //DefaultListModel list1;
     
     
     
        public MyClient(String name) throws IOException{
            initComponents();
                    //list=new DefaultListModel();
    		//list1=new DefaultListModel();
                   // list=new JList(model);
    		//list1=new JList(model1);
     
                    s=new Socket("localhost",1004);	//creates a socket object
    		s1=new Socket("localhost",1004);
    		s2=new Socket("localhost",1004);
    	    //create inputstream for a particular socket
    		din=new DataInputStream(s.getInputStream());
    		//create outputstream
    		dout=new DataOutputStream(s.getOutputStream());
    		//sending a message for login
    		dout.writeUTF(name+" has Logged in");	
    		dlout=new DataOutputStream(s1.getOutputStream());
    		dout1=new DataOutputStream(s2.getOutputStream());
    		din1=new DataInputStream(s2.getInputStream());
     
    // creating a thread for maintaning the list of user name
    		My1 m1=new My1(dout1,model1,name,din1);
    		Thread t1=new Thread(m1);
    		t1.start();			
    	//creating a thread for receiving a messages
    		My m=new My(din,model);
    		Thread t=new Thread(m);
    		t.start();
     
     
        }
     
     
              public void windowClosing(WindowEvent w){
    		try{
    			dlout.writeUTF(name);
    			dlout.flush();	
    			Thread.currentThread().sleep(1000);
    			System.exit(1);
    			}
                    catch(Exception oe){}
    		}
     
     
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            button = new javax.swing.JButton();
            lout = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
            tf = new javax.swing.JTextField();
            jScrollPane1 = new javax.swing.JScrollPane();
            model = new javax.swing.JList();
            jScrollPane2 = new javax.swing.JScrollPane();
            model1 = new javax.swing.JList();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            button.setText("Send");
            button.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buttonActionPerformed(evt);
                }
            });
     
            lout.setText("Logout");
            lout.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    loutActionPerformed(evt);
                }
            });
     
            jLabel1.setText("message:");
     
            jScrollPane1.setViewportView(model);
     
            jScrollPane2.setViewportView(model1);
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lout))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(25, 25, 25)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(tf, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 98, Short.MAX_VALUE)
                                    .addComponent(button))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(28, 28, 28)))))
                    .addGap(25, 25, 25))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(button)
                        .addComponent(jLabel1)
                        .addComponent(tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(lout)
                    .addGap(34, 34, 34))
            );
     
            pack();
        }// </editor-fold>                        
     
        private void buttonActionPerformed(java.awt.event.ActionEvent evt) {                                       
            String str="";
    			str=tf.getText();
    			tf.setText("");
    			str=name+": > "+str;
    			try{
    			dout.writeUTF(str);
    			System.out.println(str);
    			dout.flush();
    			}
                            catch(IOException ae){
                                System.out.println(ae);}
        }                                      
     
        private void loutActionPerformed(java.awt.event.ActionEvent evt) {                                     
                            frame.dispose();
    			try{
    				 //sending the message for logout
                                dout.writeUTF(name+" has Logged out");
                                dlout.writeUTF(name);
                                dlout.flush();
                                Thread.currentThread().sleep(1000);
                                System.exit(1);
    			}
                            catch(Exception oe){}
        }                                    
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(MyClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(MyClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(MyClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(MyClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
                  java.awt.EventQueue.invokeLater(new Runnable() {
                 public void run() {
                          try {
                              new MyClient(name).setVisible(true);
                          } catch (IOException ex) {
                              //Logger.getLogger(MyClient.class.getName()).log(Level.SEVERE, null, ex);
                          }
                }
      });
     
     
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton button;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JScrollPane jScrollPane2;
        private javax.swing.JButton lout;
        private javax.swing.JList model;
        private javax.swing.JList model1;
        private javax.swing.JTextField tf;
        // End of variables declaration                   
    }
    class My1 implements Runnable{
    	DataOutputStream dout1;
    	//DefaultListModel model1;
            JList model1;
    	DataInputStream din1;
    	String name,lname;
    	ArrayList alname=new ArrayList(); //stores the list of user names
    	ObjectInputStream obj; // read the list of user names
    	int i=0;
    //	My1(DataOutputStream dout1,DefaultListModel model1,String name,DataInputStream din1){
    //		this.dout1=dout1;
    //		this.model1=model1;
    //		this.name=name;
    //		this.din1=din1;
    //	}
     
        My1(DataOutputStream dout1, JList model1, String name, DataInputStream din1) {
                    this.dout1=dout1;
    		this.model1=model1;
    		this.name=name;
    		this.din1=din1;
        }
     
     
            @Override
    	public void run(){
    		try{
                         new MyClient(name).setVisible(true);
     
    		dout1.writeUTF(name);  // write the user name in output stream
    		while(true){
    			obj=new ObjectInputStream(din1);
    			//read the list of user names
    			alname=(ArrayList)obj.readObject(); 
    			if(i>0) 
                            //model1.clear();
                            model1.removeAll();
    			Iterator i1=alname.iterator();
    			System.out.println(alname);
    			while(i1.hasNext()){
    				lname=(String)i1.next();
    				i++;
    				 //add the user names in list box
    				//model1.addElement(lname);
                                    model1.add(lname, model1);
    				}
    			}
    		}catch(Exception oe){}
    	}
    }
    //class is used to received the messages
    class My implements Runnable{
    	DataInputStream din;
    	DefaultListModel model;
     
    //	My(DataInputStream din, DefaultListModel model){
    //		this.din=din;
    //		this.model=model;
    //	}
     
        My(DataInputStream din, JList list) {
                    this.din=din;
    		this.model=model;
        }
     
     
    	public void run(){
    		String str1="";
    		while(true){
    			try{
    				str1=din.readUTF(); // receive the message
    				// add the message in list box
    				model.addElement(str1);
    			}
                            catch(Exception e){}
    		}
    	}
    }

    so like i said the problem is when i click login from the login page the client page isn't opening

  21. #21
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: frame is not opening in chat application

    i have attached the files
    Attached Files Attached Files

  22. #22
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: frame is not opening in chat application

    and here is the orignal file where i got the code.
    bascially all i want to do is take this file and put the code into a JFrame so its easier to add more features and move things around
    Attached Files Attached Files

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: frame is not opening in chat application

    when i click login from the login page the client page isn't opening
    What is printed out by the println statements you added for debugging?
    Please copy the contents of the console with the debug print outs that shows where the code has executed and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. chat application login error
    By felixnoriel in forum Java Networking
    Replies: 4
    Last Post: January 4th, 2012, 01:38 PM
  2. java chat application login error
    By felixnoriel in forum Threads
    Replies: 1
    Last Post: January 4th, 2012, 01:15 PM
  3. Opening a new frame on close.
    By Zein in forum Java Theory & Questions
    Replies: 1
    Last Post: August 24th, 2011, 03:25 PM
  4. mobile chat application
    By teemone2001 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 21st, 2011, 03:33 AM
  5. Chat application problem
    By fembiz in forum Java Networking
    Replies: 1
    Last Post: December 11th, 2009, 05:21 AM