Results 1 to 10 of 10
  1. #1
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Trying to create a basic server/client, jlabel not updating

    Hello, I have a problem of not being able to update the LChatLog JLabel on line 123 in the ServerTest, it works if I update the JLabel inside the ActionListener of the button. I have tried to update the Jlabel in a new running thread with InvokeLater() because i know that updates to the GUI should be made on the EDT and this invokes it to be updated on the EDT(am i right?) but it didn't seem to fix it.

    ServerTest.java
    Java Code:
    package servertest;
    
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class ServerTest implements Runnable{
    
        JFrame fFrame;
        JPanel pPanel;
        JButton bCreate;
        JButton bSendMessage;
        static JLabel lInfo;
        static JLabel LChatLog;
        ServerSocket serverSocket;
        
        ObjectOutputStream socketOut;
        ObjectInputStream socketIn;
        
        String message = null;
        
        
        
        public ServerTest() {
            fFrame = new JFrame();
            pPanel = new JPanel(new GridBagLayout());
            bCreate = new JButton();
            lInfo = new JLabel();
            LChatLog = new JLabel();
            bSendMessage = new JButton();
            serverSocket = null;
        }
        
        public void init(){
            fFrame.setPreferredSize(new Dimension(400,200));
            fFrame.setMinimumSize(new Dimension(400,200));
            fFrame.setMaximumSize(new Dimension(400,200));
            fFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            fFrame.setTitle("Server Test");
            fFrame.pack();
            fFrame.add(pPanel);
            fFrame.setLocationRelativeTo(null);
            
            
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.NORTH;
            
            gbc.weighty = .01;
            gbc.weightx = .25;
            gbc.gridx = 0;
            gbc.gridy = 0;
            lInfo.setText("Click create.");
            pPanel.add(lInfo, gbc);
            
            gbc.weighty = .01;
            gbc.weightx = .25;
            gbc.gridx = 0;
            gbc.gridy = 1;
            bCreate.setText("Create Server");
            pPanel.add(bCreate, gbc);
                    
            gbc.weighty = .01;
            gbc.weightx = .25;
            gbc.gridx = 0;
            gbc.gridy = 4;
            LChatLog.setText("Waiting to receive message...");
            pPanel.add(LChatLog, gbc);
            
            gbc.weighty = .1;
            gbc.weightx = .25;
            gbc.gridx = 0;
            gbc.gridy = 5;
            bSendMessage.setText("Send message");
            pPanel.add(bSendMessage, gbc);
            
            fFrame.setVisible(true);
            
            bSendMessage.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e)
                {
                }
            }); 
            
            bCreate.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    lInfo.setText("Server: Now listening on port 4444");
                    (new Thread(new ServerTest())).start();
                }
            }); 
        }
        
        @Override
        public void run(){
            try
            {
                ServerSocket ss = new ServerSocket(9999);  
                Socket s = ss.accept();  
                System.out.println("client found");
                InputStream is = s.getInputStream();  
                ObjectInputStream ois = new ObjectInputStream(is);  
                try {
                    message = (String)ois.readObject();
                    System.out.println(message);
                    //LChatLog.setText(message);
                    Runnable run = new Runnable(){
                        public void run(){
                            LChatLog.setText(message);
                        }
                    };
                    try{
                        SwingUtilities.invokeLater(run);
                    } catch (Exception e){
                        System.out.println(e);
                    }
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ServerTest.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            catch (IOException e)
            {
                System.out.println(e);
            }
        }
    
        public static void main(String[] args) {
            System.out.println(Thread.currentThread());
            ServerTest test = new ServerTest();
            test.init();
        }
    }
    ClientTest.java
    Java Code:
    package clienttest;
    
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    public class ClientTest  implements Runnable{
        Socket sock;
        JFrame fFrame;
        JPanel pPanel;
        JButton bJoin;
        JButton bSendMessage;
        static JLabel lMessage;
        static JLabel lInfo;
        String message = "";
        
        Socket socket;
        ObjectOutputStream socketOut;
        ObjectInputStream socketIn;
        
        public ClientTest() {
            fFrame = new JFrame();
            pPanel = new JPanel(new GridBagLayout());
            bJoin = new JButton();
            lMessage = new JLabel();
            lInfo = new JLabel();
            bSendMessage = new JButton();
        }
        
        public void init(){
            fFrame.setPreferredSize(new Dimension(400,200));
            fFrame.setMinimumSize(new Dimension(400,200));
            fFrame.setMaximumSize(new Dimension(400,200));
            fFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            fFrame.setTitle("Client Test");
            fFrame.pack();
            fFrame.add(pPanel);
            fFrame.setLocationRelativeTo(null);
            
            
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.NORTH;
            
            gbc.weighty = .01;
            gbc.weightx = .25;
            gbc.gridx = 0;
            gbc.gridy = 0;
            lInfo.setText("Click join.");
            pPanel.add(lInfo, gbc);
            
            gbc.weighty = .01;
            gbc.weightx = .25;
            gbc.gridx = 0;
            gbc.gridy = 2;
            bJoin.setText("Join Server");
            pPanel.add(bJoin, gbc);
            
            gbc.weighty = .01;
            gbc.weightx = .25;
            gbc.gridx = 0;
            gbc.gridy = 3;
            lMessage.setText("Welcome to the server test.");
            pPanel.add(lMessage, gbc);
            
            gbc.weighty = .1;
            gbc.weightx = .25;
            gbc.gridx = 0;
            gbc.gridy = 5;
            bSendMessage.setText("Send message");
            pPanel.add(bSendMessage, gbc);
            
            fFrame.setVisible(true);
            try{
                System.out.println("Searching for server...");
                Socket s = new Socket("localhost",9999);  
                OutputStream os = s.getOutputStream();  
                socketOut = new ObjectOutputStream(os);  
                System.out.println("Server found!");
            } catch(IOException e){
                System.out.println(e);
            }
            bSendMessage.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    message = JOptionPane.showInputDialog("Enter a message");
                    sendMessage(message);
                }
            }); 
            
            bJoin.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    //(new Thread(new ClientTest())).start();
                }
            }); 
        }
        
        public void sendMessage(String str){
            try {
                System.out.println("send object");
                System.out.println(socketOut);
                socketOut.writeObject(str);
            } catch (IOException ex) {
                Logger.getLogger(ClientTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
       
        @Override
        public void run(){
        }
        
        public static void main(String[] args) {
            ClientTest test = new ClientTest();
            test.init();
        }
    }
    Running the client and sending the message "345"
    Java Code:
    run:
    Searching for server...
    Server found!
    send object
    java.io.ObjectOutputStream@2cba5bdb
    BUILD SUCCESSFUL (total time: 3 seconds)
    Running the Server
    Java Code:
    run:
    Thread[main,5,main]
    client found
    345
    BUILD SUCCESSFUL (total time: 7 seconds)
    Thanks for any help as I have been trying to fix this for the past 6 hours :)
    Last edited by Teedo; 07-22-2013 at 11:38 PM.

  2. #2
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Trying to create a basic server/client, jlabel not updating and NPE problem

    I have updated my post to just one problem and I will post the other problem in another thread :)

  3. #3
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Trying to create a basic server/client, jlabel not updating and NPE problem

    Ok so i've tested a little bit more and the JLabel display is not getting updated after the new thread is made, even though it's on the EDT....why would this be?
    Here is the new debugging code....
    Line 100: LChatLog.setText("hello"); will work until a new thread is created on line 113.
    If I start the server, and click 'send message', then the value of the JLabel will change, but the GUI will not.
    If I do NOT start the server, and click 'send message', then the value and the display of the JLabel will change to "hello"

    ServerTest.java
    Java Code:
    package servertest;
    
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class ServerTest implements Runnable{
    
        JFrame fFrame;
        JPanel pPanel;
        JButton bCreate;
        JButton bSendMessage;
        static JLabel lInfo;
        static JLabel LChatLog;
        ServerSocket serverSocket;
        
        ObjectOutputStream socketOut;
        ObjectInputStream socketIn;
        ObjectInputStream ois;
        
        String message = null;
        
        
        
        public ServerTest() {
            fFrame = new JFrame();
            pPanel = new JPanel(new GridBagLayout());
            bCreate = new JButton();
            lInfo = new JLabel();
            LChatLog = new JLabel();
            bSendMessage = new JButton();
            serverSocket = null;
        }
        
        public void init(){
            fFrame.setPreferredSize(new Dimension(400,200));
            fFrame.setMinimumSize(new Dimension(400,200));
            fFrame.setMaximumSize(new Dimension(400,200));
            fFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            fFrame.setTitle("Server Test");
            fFrame.pack();
            fFrame.add(pPanel);
            fFrame.setLocationRelativeTo(null);
            
            
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.NORTH;
            
            gbc.weighty = .01;
            gbc.weightx = .25;
            gbc.gridx = 0;
            gbc.gridy = 0;
            lInfo.setText("Click create.");
            pPanel.add(lInfo, gbc);
            
            gbc.weighty = .01;
            gbc.weightx = .25;
            gbc.gridx = 0;
            gbc.gridy = 1;
            bCreate.setText("Create Server");
            pPanel.add(bCreate, gbc);
                    
            gbc.weighty = .01;
            gbc.weightx = .25;
            gbc.gridx = 0;
            gbc.gridy = 4;
            LChatLog.setText("Waiting to receive message...");
            pPanel.add(LChatLog, gbc);
            
            gbc.weighty = .1;
            gbc.weightx = .25;
            gbc.gridx = 0;
            gbc.gridy = 5;
            bSendMessage.setText("Send message");
            pPanel.add(bSendMessage, gbc);
            
            fFrame.setVisible(true);
            
            bSendMessage.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("Test");
                    LChatLog.setText("hello");
                    System.out.println(LChatLog.getText());
                    System.out.println(Thread.currentThread());
                }
            }); 
            
            bCreate.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    lInfo.setText("Server: Now listening on port 4444");
                    System.out.println(Thread.currentThread());
                    (new Thread(new ServerTest())).start();
                }
            }); 
        }
        
        @Override
        public void run(){/*
            try
            {
                System.out.println(Thread.currentThread());
                //ServerSocket ss = new ServerSocket(9999);  
                //Socket s = ss.accept();  
                System.out.println("client found");
                //InputStream is = s.getInputStream();  
                //ois = new ObjectInputStream(is);  
                try {
                    message = (String)ois.readObject();
                    System.out.println("Received message: " + message);
                    //LChatLog.setText(message);
                    /*SwingUtilities.invokeLater(new Runnable(){  
                        public void run(){  
                          LChatLog.setText(message);
                        }  
                    });
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ServerTest.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            catch (IOException e)
            {
                System.out.println(e);
            }*/
        }
    
        public static void main(String[] args) {
            System.out.println(Thread.currentThread());
            ServerTest test = new ServerTest();
            test.init();
        }
    }
    Last edited by Teedo; 07-23-2013 at 12:34 PM.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Trying to create a basic server/client, jlabel not updating and NPE problem

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Trying to create a basic server/client, jlabel not updating and NPE problem

    It is being done on the EDT though, which I checked by printing out the current thread.....the code that changs the JLabel is even done inside an action of a JButton

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Trying to create a basic server/client, jlabel not updating and NPE problem

    Oh, yeah, sorry, I've misread the code.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Trying to create a basic server/client, jlabel not updating and NPE problem

    It's okay, Thanks anyway...I just can't seem to find an answer of why creating a new thread will stop the label from updating, even though it's trying to do so on the EDT

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,079
    Rep Power
    6

    Default Re: Trying to create a basic server/client, jlabel not updating and NPE problem

    I see you creating two instances of ServerTest - one to create your GUI and another to start the server thread. Try moving the actual server code into a separate class first because now it is just a mess of doubly created objects.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Trying to create a basic server/client, jlabel not updating and NPE problem

    Fixed it! It was because the constructor was setting the JLabel back to null. This was just meant to be a small test, but I will do that too gimbal2.
    Thank you very much :)

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,079
    Rep Power
    6

    Default Re: Trying to create a basic server/client, jlabel not updating and NPE problem

    Oh now I see that those two members are static. If you would have made them non-static, you wouldn't have had this problem.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. How do I create a client - server application??
    By ady_bavarezu89 in forum Java Servlet
    Replies: 9
    Last Post: 12-29-2011, 07:49 PM
  2. Updating a JLabel from another class.
    By Fumomo in forum AWT / Swing
    Replies: 6
    Last Post: 05-04-2011, 04:31 PM
  3. Swing JLabel Updating (Threading)
    By Skezza in forum AWT / Swing
    Replies: 3
    Last Post: 07-27-2010, 03:43 PM
  4. about client to server problem
    By raywai in forum New To Java
    Replies: 3
    Last Post: 05-13-2010, 04:34 PM
  5. client server problem
    By kievari in forum Advanced Java
    Replies: 4
    Last Post: 02-12-2010, 08:06 AM

Posting Permissions

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