Results 1 to 4 of 4
  1. #1
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default Smack App GUI Issue Help

    I have written the code for gtalk chat client using Smack API .

    This code displays a frame which contains 2 textarea 1 for sending message and other which contains received and sent messages.

    My code sends the message to the particular email and it also shows on the 2nd textarea but when the message is received from the other person i can not display on the 2nd textarea .

    NOTE : i am getting the messages from the other user i can see in the console but when is append nothing happens so plz help.

    Java Code:
    public class JabberSmackAPI implements MessageListener
    {
    
        XMPPConnection connection;
        JScrollPane jScrollPane1;
        JScrollPane jScrollPane2;
        JTextArea jTextArea1;
        JTextArea jTextPane1;
        JabberSmackAPI c;
        String email;
        JFrame f;
        private ChatPanel cp;
    
        public void login(String userName, String password,String em) throws XMPPException
        {
            email=em;
        ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com",5222, "Work");
        connection = new XMPPConnection(config);
    
        connection.connect();
        connection.login(userName, password);
        PacketFilter filter= new AndFilter(new PacketTypeFilter(Message.class),new FromContainsFilter(email));
    
               PacketListener myListener = new PacketListener() {
                    public void processPacket(Packet packet) {
                        if (packet instanceof Message) {
                            Message msg = (Message) packet;
                            // Process message
                            String temp = msg.getFrom();
                            String emailid = temp.substring(0,temp.lastIndexOf("/"));
                            String mess=msg.getBody();
                            System.out.println(emailid+" "+mess);
                            jTextPane1.append(emailid+" "+mess);
                        }
                    }
                };
                connection.addPacketListener(myListener, filter);
         
        }
    
        public void sendMessage(String message, String to) throws XMPPException
        {
        Chat chat = connection.getChatManager().createChat(email, this);
        chat.sendMessage(message);
        }
    
        public void displayBuddyList()
        {
        Roster roster = connection.getRoster();
        Collection<RosterEntry> entries = roster.getEntries();
    
        System.out.println("\n\n" + entries.size() + " buddy(ies):");
        for(RosterEntry r:entries)
        {
        System.out.println(r.getUser());
        }
        }
    
        public void disconnect()
        {
        connection.disconnect();
        }
    
    
        public void begin(String u,String p,String to) throws XMPPException, IOException
        {
        // declare variables
        c = new JabberSmackAPI();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String msg;
    
    
        // turn on the enhanced debugger
        XMPPConnection.DEBUG_ENABLED = false;
    email=to;
    
        // Enter your login information here
        c.login(u, p,to);
        String talkTo =to;
    
        f = new JFrame(talkTo);
        cp=new ChatPanel();
        f.add(cp);
        f.addWindowListener(new WindowAdapter()
                            {
                                public void windowClosing(WindowEvent e)
                                {
                                     c.disconnect();
                                     f.dispose();
                                }
                            });
        f.pack();
        f.setVisible(true);
        }
    
        public void processMessage(Chat chat, Message msg) {    }
    
        class ChatPanel extends javax.swing.JPanel
        {
    
        public ChatPanel() throws XMPPException
        {
            initComponents();
        }
    
        private void initComponents() throws XMPPException
        {     
            jScrollPane1 = new javax.swing.JScrollPane();
            jTextPane1 = new javax.swing.JTextArea();
            jScrollPane2 = new javax.swing.JScrollPane();
            jTextArea1 = new javax.swing.JTextArea();
    
            setBackground(new java.awt.Color(255, 255, 255));
            setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 3, true));
    
            jTextPane1.setBackground(new java.awt.Color(153, 255, 255));
            jScrollPane1.setViewportView(jTextPane1);
    
            jTextArea1.setBackground(new java.awt.Color(255, 102, 102));
            jTextArea1.setColumns(20);
            jTextArea1.setRows(5);
            jScrollPane2.setViewportView(jTextArea1);
            jTextPane1.setEditable(false);
            jTextArea1.addKeyListener(new KeyListener()
                                                {
                                                    public void keyPressed(KeyEvent ke)
                                                    {
                                                        int keyCode=ke.getKeyCode();
                                                        if(keyCode==KeyEvent.VK_ENTER)
                                                        {
                            try {
                                c.sendMessage(jTextArea1.getText().trim(), email);
                            } catch (XMPPException ex) {
                                Logger.getLogger(JabberSmackAPI.class.getName()).log(Level.SEVERE, null, ex);
                            }
                                jTextPane1.append("\n"+jTextArea1.getText().trim());
                            
                                jTextArea1.setText("");
    
                                                        }
                                                    }
    
                                                    public void keyTyped(KeyEvent ke) {
                    }
    
                                                    public void keyReleased(KeyEvent ke) {
                    }
                                                });
    
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 258, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))
            );
        }
    
    }
    
    
    }

    Thank you in adv.


    Please help
    Last edited by ravjot28; 10-27-2010 at 06:31 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ravjot28 View Post
    I have written the code for gtalk chat client using Smack API .

    This code displays a frame which contains 2 textarea 1 for sending message and other which contains received and sent messages.

    My code sends the message to the particular email and it also shows on the 2nd textarea but when the message is received from the other person i can not display on the 2nd textarea .
    Note that I know nothing about the Smack API, but I do wonder how you will be able to process messages when your method for this looks like this:

    Java Code:
       public void processMessage(Chat chat, Message msg) {
            // ???? is something supposed to go here ????
       }

    I have to ask myself if this is in fact your code or if it is borrowed code.

    Best of luck.

  3. #3
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    thanks for the reply

    basically that function was for testing stuff

    This is the listener set which will process messages

    Java Code:
    PacketFilter filter= new AndFilter(new PacketTypeFilter(Message.class),new FromContainsFilter(email));
    
               PacketListener myListener = new PacketListener() {
                    public void processPacket(Packet packet) {
                        if (packet instanceof Message) {
                            Message msg = (Message) packet;
                            // Process message
                            String temp = msg.getFrom();
                            String emailid = temp.substring(0,temp.lastIndexOf("/"));
                            String mess=msg.getBody();
                            System.out.println(emailid+" "+mess);
                            jTextPane1.append(emailid+" "+mess);
                        }
                    }
                };
                connection.addPacketListener(myListener, filter);
    thanks

  4. #4
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

Similar Threads

  1. Chat Client using Smack API
    By sQu3aKy in forum Networking
    Replies: 0
    Last Post: 05-01-2010, 02:40 AM
  2. Having an issue with this
    By baap30 in forum New To Java
    Replies: 5
    Last Post: 04-23-2010, 05:32 AM
  3. nio issue
    By mawandiadeepak in forum Networking
    Replies: 2
    Last Post: 03-17-2010, 05:23 AM
  4. PDF Box issue
    By jazz2k8 in forum Advanced Java
    Replies: 0
    Last Post: 03-20-2009, 11:04 AM
  5. using the org.jivesoftware.smack package
    By fabulousdd in forum Advanced Java
    Replies: 1
    Last Post: 07-13-2008, 08:27 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
  •