Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    michail is offline Member
    Join Date
    Dec 2009
    Posts
    31
    Rep Power
    0

    Default Listener/JTextField to Int

    Code below
    Last edited by michail; 05-08-2010 at 07:57 PM. Reason: Solved
    For a world of science and reason

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,354
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by michail View Post
    Hi guy, I am doing a chat application.
    I get an error. listenportext : cannot find variable
    What is the exact error message and line number?

    kind regards,

    Jos

    edit: the structure of your outer class doesn't make sense: you can't have statements at the class level, they belong in a method or constructor.
    Last edited by JosAH; 05-05-2010 at 06:11 PM.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    You've got execution code floating around inside your class outside of a block (ie method).

  4. #4
    michail is offline Member
    Join Date
    Dec 2009
    Posts
    31
    Rep Power
    0

    Default

    Solved problem
    Last edited by michail; 05-08-2010 at 07:53 PM.
    For a world of science and reason

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    listenportext isn't declared anywhere in that code.

  6. #6
    michail is offline Member
    Join Date
    Dec 2009
    Posts
    31
    Rep Power
    0

    Default

    It is at the beginning!

    private JTextField listenporttext;
    listenportext = new JTextField("listen",6);
    listenportext.addActionListener(handler);

    I still can't get it to work. I have a print statement just after the Object source = event.getSource();. It recognises the action but then it does nothing.
    For a world of science and reason

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    If you're going to post code then please make sure it is at least meaningful.
    Those three lines, if stuck in a class, wouldn't compile.

    You need to show us where you are declaring the variable, where it is initialised (less important, but you never know) and the part of the code you have a problem. With all the brackets and relevant method declarations in the right place.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,354
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by michail View Post
    It is at the beginning!

    private JTextField listenporttext;
    listenportext = new JTextField("listen",6);
    listenportext.addActionListener(handler);

    I still can't get it to work. I have a print statement just after the Object source = event.getSource();. It recognises the action but then it does nothing.
    That third line isn't allowed; you can't have arbitrary statements outside of a method or initialization block; please read the previous replies because it has been mentioned before (by several repliers).

    kind regards,

    Jos

  9. #9
    michail is offline Member
    Join Date
    Dec 2009
    Posts
    31
    Rep Power
    0

    Default

    Ok, I will post the whole code.
    Maybe I can understand it then
    For a world of science and reason

  10. #10
    michail is offline Member
    Join Date
    Dec 2009
    Posts
    31
    Rep Power
    0

    Default

    This is it
    The problem is, when I press listen or connect it doesn.t triger anything, so that means I can.t even test it to see if the rest of it works


    Java Code:
    import java.io.EOFException;
    import java.io.IOException;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.net.*;
    import java.io.*;
    
    public [B]class[/B] Server extends JFrame
    {
        
        private JButton send;
        private JButton close;
        private JButton save;
        private JButton clear;
        private JButton connect;
        private JButton listen;
        private JTextArea displayarea;
        private JTextField writearea;
        private JTextField listenporttext;
        private JTextField connectport;
        private JTextField connectIp;
        public final static int CONNECTED = 1;
        public final static int WAITING = 2;
        public final static int CONNECTING = 3;
        public final static int DISCONNECTED = 4;
        public static int connectionStatus;
        private Socket connection;
        private ServerSocket server;
        private ObjectOutputStream output;
        private ObjectInputStream input;
        private ConnectionHandler connections;   
        private JTextField listenportext;
        
        [B]public[/B] Server()
    {
        super("Server");
    
            JPanel container = new JPanel(new BorderLayout(3,3));//add to the container the components below
            //maybe put some colour
            JPanel topContainer = new JPanel(new GridLayout(2,1,2,2));
           
            JPanel topbuttonBar = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
            JPanel inputPanel = new JPanel(new BorderLayout(5,5));
            inputPanel.setBackground(Color.RED);
            JPanel connections = new JPanel(new FlowLayout(FlowLayout.CENTER,3,3));//connections panel
            //JMenu Sth = new JMenu("sth");
            
    /*Input outpout area
    */      
            listenportext = new JTextField("22222",6);
            connectport = new JTextField("22222",6);
            connectIp = new JTextField("localhost",6);
            writearea = new JTextField();
            writearea.setEditable(true);
            JTextArea displayarea = new JTextArea(30,2);
            displayarea.setLineWrap(true);
            displayarea.setEditable(false);
    
    /*Buttons
    */
            send = new JButton("send");
            add(send);
            close = new JButton("close");
            add(close);
            save = new JButton("save");
            add(save);
            clear = new JButton("clear");
            add(clear);
            connect = new JButton("connect");
            add(connect);
            listen = new JButton("listen");
            add(listen);
    
            container.setBorder(BorderFactory.createLineBorder(Color.green));//compound border?
            container.add(topContainer, BorderLayout.NORTH);//add topContainer on top
            topContainer.add(connections);//add the panel with the connections buttons
            topContainer.add(topbuttonBar);//add the panel with thr rest of the buttons
            topbuttonBar.add(close);
            topbuttonBar.add(save);
            topbuttonBar.add(clear);
            topbuttonBar.add(Box.createHorizontalStrut(50));
            topbuttonBar.add(listen);
            topbuttonBar.add(listenportext);
            topbuttonBar.add(connect);
    		topbuttonBar.add(connectIp);        
    		topbuttonBar.add(connectport);
            container.add(inputPanel, BorderLayout.SOUTH);
            container.add(new JScrollPane(displayarea));
            inputPanel.add(writearea, BorderLayout.CENTER);
            inputPanel.add(send, BorderLayout.EAST);
            
            
    
    /*Action Handling
    */
            ActionListener handler = new ActionsHandler();
            send.addActionListener(handler);
            close.addActionListener(handler);
            save.addActionListener(handler);
            clear.addActionListener(handler);
            connect.addActionListener(handler);
            listen.addActionListener(handler);
            writearea.addActionListener(handler);
            listenportext.addActionListener(handler);
            connectport.addActionListener(handler);
            connectIp.addActionListener(handler);
    
            setContentPane(container);
            pack();
    
            
        }
    
         private [B]class[/B] [B]ActionsHandler[/B] implements ActionListener
                {
                    
                    public void actionPerformed(ActionEvent event)
                        {
                              Object source = event.getSource();
                               
                            if (source == listen)
                            {
                                
                                String temp = listenportext.getText();
                                int port = Integer.parseInt(temp);
                                connections = new ConnectionHandler(port);
    							connections.start();
                             }
                            
                            else if (source == connect)
                            {   int port;
                                String connectemp = connectport.getText();
                                
                                port = Integer.parseInt(connectemp);
                                connections = new ConnectionHandler(connectIp.getText(), port);
    							connections.start();
                            }
                            
                            else if (source == close)
                            {
                                System.exit(0);
                            }
    
    						else if (source == send || source == writearea)
    						{
    							if (connection !=null && connectionStatus == CONNECTED)
    						{
    							connections.sendData(writearea.getText());
    						}
                        }
                    }
                    }
                
                    
                    
                //type object
                private [B]class[/B] [B]ConnectionHandler[/B] extends Thread
                {
                    private int port;
                    private String remoteip;
                    private ObjectOutputStream out = null;
    				private ObjectInputStream in = null;
                    private ServerSocket serversocket;
                    private Socket socket = null;
       //First constructor if it.s a server         
                    ConnectionHandler(int port)
                    {
                        Server.this.connectionStatus = WAITING;
                        this.port = port;
                        System.out.println("Waiting on port "+port);
                        //this.start();
                    }
                    
      //Second constructor if it.s a client          
                    ConnectionHandler(String remoteip, int port)
                    {
                        Server.this.connectionStatus = CONNECTING;
                        this.port = port;
                        this.remoteip = remoteip;
                        System.out.println("Connecting to "+remoteip+" "+port);
                    }
                    
                    
                    
               //thread has been started     
                    public void [B]run[/B]()
                    {
                   
                     try
                        {
                            if (Server.this.connectionStatus == WAITING) 
                            {   
                    //server waiting for a connection
    							System.out.println( "Setting up server socket on port " + port );
                                serversocket = new ServerSocket(port);
    							System.out.println( "Waiting for connections..." );
                                connection = serversocket.accept();
                               
                            }
                    //client is connecting
                                else if (Server.this.connectionStatus == CONNECTING)
                                {
                                    socket = new Socket(InetAddress.getByName(remoteip), port);
        							System.out.println("Accepted a connection from: "+InetAddress.getByName(remoteip));
        							System.out.println("Accepted a connection from: "+socket.getInetAddress());
                                    
                                   //while loop to handle the messages
                                }    
        						connectionEstablished();
        						while(connectionStatus == CONNECTED)
                                {
                              		String dispatch = (String)in.readObject();
                              		sendData(dispatch);
                              		
                                
                                }
     						
                            }catch (Exception e)
                                {
        						System.out.println( e );
                                }
                                finally{}
                        }
                    
                    
                    
                    private void [B]connectionEstablished[/B]() throws IOException
                    {[B]//problem here
                    //after connecting get the streams[/B]
                    	try{
        				 out = new ObjectOutputStream(socket.getOutputStream());//auto flush
        				 out.flush();
        				 in = new ObjectInputStream(socket.getInputStream());
       						}catch(NullPointerException s){s.printStackTrace();}
       						
        				 connectionStatus = CONNECTED;
                    }
                    
                    
                    private void [B]sendData[/B](String message) 
                    {
                        try {
                            
                        out.writeObject("Client>");
                        out.flush();
                        displayarea.append(message);
                            }
                            catch (IOException ioException)
                            {
                                System.out.print("Error writing");
                            }
                    }
                
                }//end of connection handler
                
      
                        
            public static void [B]main[/B](String[] args) 
            {
              Server chat = new Server();
              chat.setVisible(true);
            }
      
    }
    Last edited by michail; 05-08-2010 at 07:56 PM. Reason: One step further
    For a world of science and reason

  11. #11
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Java Code:
    /*Buttons
    */
            /*JButton send = new JButton("send");
            add(send);
            JButton close = new JButton("close");
            add(close);
            JButton save = new JButton("save");
            add(save);
            JButton clear = new JButton("clear");
            add(clear);
            JButton connect = new JButton("connect");
            add(connect);
            JButton listen = new JButton("listen");
            add(listen);*/
    
    		/*
    		you have already declared the buttons as instance level variables at top
    		so the listeners were not getting attached to instance level vars, rather
    		to the vars declared inside the constructor
    		*/
            send = new JButton("send");
    		add(send);
    		close = new JButton("close");
    		add(close);
    		save = new JButton("save");
    		add(save);
    		clear = new JButton("clear");
    		add(clear);
    		connect = new JButton("connect");
    		add(connect);
    		listen = new JButton("listen");
            add(listen);
    /*buttons*/
    Swastik

  12. #12
    michail is offline Member
    Join Date
    Dec 2009
    Posts
    31
    Rep Power
    0

    Default

    Thank you very very much. I missed that detail. Have you got any suggestions on the other methods?
    Thanks again
    For a world of science and reason

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    OK, so what exactly is the problem?

    What is the error you are getting, stack trace and all?

  14. #14
    michail is offline Member
    Join Date
    Dec 2009
    Posts
    31
    Rep Power
    0

    Default

    At the moment I press connect and listen, it shows the input. when I get to the run(), it doesn.t recognise the variables in the code.(serversocket). I don't know even if it distinguishes between the constants WAITING and CONNECTING in the run() method
    Have I grasped the general idea? I mean am i going in the right direction with the methods ?

    Thanks
    For a world of science and reason

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    What do you mean by "doesn't recognise the variables".
    What exception are you getting?

  16. #16
    michail is offline Member
    Join Date
    Dec 2009
    Posts
    31
    Rep Power
    0

    Default

    Ok I got there in the end. I will post further
    The problem was here---> connectionStatus == WAITING
    it should have been Server.this.connectionStatus == WAITING

    Thanks
    For a world of science and reason

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Eh?
    connectionStatus is a static in Server, so a direct reference, or Server.connectionStatus shouldn't make a difference.

    Hang on.
    Do you have multiple connections coming in here? All using the single static connectionStatus?

  18. #18
    michail is offline Member
    Join Date
    Dec 2009
    Posts
    31
    Rep Power
    0

    Default

    Hi again. I updated the code above.
    I got the server running! When I try to connect I get---->>.Connecting to localhost 3333
    java.net.ConnectException: Connection refused

    What does that mean? Do I have to put an exception somewhere?
    :confused:
    For a world of science and reason

  19. #19
    michail is offline Member
    Join Date
    Dec 2009
    Posts
    31
    Rep Power
    0

    Default

    Hi, I fixed the connection problem. It was a blocked port!!
    When I compile and connect to the server the server comes up with this error


    java.lang.NullPointerException
    at Server$ConnectionHandler.connectionEstablished(Ser ver.java:244)
    at Server$ConnectionHandler.run(Server.java:222)

    Any suggestions on why the OutputStream is behaving like this
    It should just get the streams, right?
    For a world of science and reason

  20. #20
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,354
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by michail View Post
    ]java.lang.NullPointerException
    at Server$ConnectionHandler.connectionEstablished(Ser ver.java:244)
    at Server$ConnectionHandler.run(Server.java:222)

    Any suggestions on why the OutputStream is behaving like this
    It should just get the streams, right?
    Don't assume anything; at line 244 in your class Server.java an object is null and it shouldn't be.

    kind regards,

    Jos

Page 1 of 2 12 LastLast

Similar Threads

  1. Tab Listener
    By teckno101 in forum AWT / Swing
    Replies: 2
    Last Post: 09-29-2009, 09:40 PM
  2. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  3. [SOLVED] action listener and Jtextfield
    By tOpach in forum AWT / Swing
    Replies: 4
    Last Post: 12-16-2008, 01:02 PM
  4. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-20-2008, 11:07 PM
  5. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-10-2008, 02:00 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
  •