Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
| Search | Today's Posts | Mark Forums Read
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 10th, 2007, 08:57 AM
Authorized User
 
Join Date: May 2007
Location: East London, Eastern cape, South Africa.
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Socket connection

The question says to use a socket connection to allow a user to specify a file name and have the server send the contents of that file or indicate that the file does not exist.

This is what I have so far and I know its not right, im not sure where to make changes or what to add.

When I try to compile it I just get a error that says “ cannot find symbol setTextFieldEditable( true ); “…Why is that..?

Any help will be appreciated, it seems Im a bit lost.

// Client.java

import java.io.EOFException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.SwingUtilities;

public class Client extends JFrame
{
   private JTextField enterField; // JTextField to enter site name from user
   private JTextArea displayArea; //display info to user
   private JEditorPane contentsArea; // to display Web site
   private ObjectOutputStream output; //output stream to server
   private ObjectInputStream input; //input stream from server
   private String message = ""; //message from server
   private String chatServer; //host server for this application
   private Socket client; //socket to communicate with server


   // initialize chatServer and set up GUI
   public Client( String host )
   {
      super( "Client" );

      chatServer = host; //set server to which this client connects

      // create enterField and register its listener
      enterField = new JTextField( "Specify file name" ); //create enter field
      enterField.addActionListener(
         new ActionListener()
         {
            // get file specified by user
            public void actionPerformed( ActionEvent event )
            {
               getThePage( event.getActionCommand() );
            } // end method actionPerformed
         } // end inner class
      ); // end call to addActionListener

      add( enterField, BorderLayout.NORTH );

      contentsArea = new JEditorPane(); // create contentsArea
      contentsArea.setEditable( false );
      contentsArea.addHyperlinkListener(
         new HyperlinkListener()
         {
            // if user clicked hyperlink, go to specified page
            public void hyperlinkUpdate( HyperlinkEvent event )
            {
               if ( event.getEventType() ==
                    HyperlinkEvent.EventType.ACTIVATED )
                  getThePage( event.getURL().toString() );
            } // end method hyperlinkUpdate
         } // end inner class
      ); // end call to addHyperlinkListener

      add( new JScrollPane( contentsArea ), BorderLayout.CENTER );
      setSize( 400, 300 ); // set size of window
      setVisible( true ); // show window
   } // end Client constructor

   // connect to server and process messages from server
   public void runClient()
   {
      try // connect to server, get streams, process connection
      {
         connectToServer(); // create a Socket to make connection
         getStreams(); // get the input and output streams
         processConnection(); // process connection
      } // end try
      catch ( EOFException eofException )
      {
         System.out.printf( "\nClient terminated connection" );
      } // end catch
      catch ( IOException ioException )
      {
         ioException.printStackTrace();
      } // end catch
      finally
      {
         closeConnection(); // close connection
      } // end finally
   } // end method runClient

   // connect to server
   private void connectToServer() throws IOException
   {
      System.out.printf( "Attempting connection\n" );

      // create Socket to make connection to server
      client = new Socket( InetAddress.getByName( chatServer ), 12345 );

      // display connection information
      System.out.printf( "Connected to: " +
         client.getInetAddress().getHostName() );
   } // end method connectToServer

   // get streams to send and receive data
   private void getStreams() throws IOException
   {
      // set up output stream for objects
      output = new ObjectOutputStream( client.getOutputStream() );
      output.flush(); // flush output buffer to send header information

      // set up input stream for objects
      input = new ObjectInputStream( client.getInputStream() );

      System.out.printf( "\nGot I/O streams\n" );
   } // end method getStreams

   // process connection with server
   private void processConnection() throws IOException
   {
      // enable enterField so client user can send messages
      setTextFieldEditable( true );

      do // process messages sent from server
      {
         try // read message and display it
         {
            message = ( String ) input.readObject(); // read new message
            System.out.printf( "\n" + message ); // display message
         } // end try
         catch ( ClassNotFoundException classNotFoundException )
         {
            System.out.printf( "\nUnknown object type received" );
         } // end catch

      } while ( !message.equals( "SERVER>>> TERMINATE" ) );
   } // end method processConnection

   // close streams and socket
   private void closeConnection()
   {
      System.out.printf( "\nClosing connection" );
      setTextFieldEditable( false ); // disable enterField

      try
      {
         output.close(); // close output stream
         input.close(); // close input stream
         client.close(); // close socket
      } // end try
      catch ( IOException ioException )
      {
         ioException.printStackTrace();
      } // end catch
   } // end method closeConnection

   // send message to server
   private void sendData( String message )
   {
      try // send object to server
      {
         output.writeObject( "CLIENT>>> " + message );
         output.flush(); // flush data to output
         System.out.printf( "\nCLIENT>>> " + message );
      } // end try
      catch ( IOException ioException )
      {
         displayArea.append( "\nError writing object" );
      } // end catch
   } // end method sendData

   // load document
   private void getThePage( String location )
   {
      try // load document and display location
      {
         contentsArea.setPage( location ); // set the page
         enterField.setText( location ); // set the text
      } // end try
      catch ( IOException ioException )
      {
         JOptionPane.showMessageDialog( this,
            "Error retrieving specified file", "File does not exist",
            JOptionPane.ERROR_MESSAGE );
      } // end catch
   } // end method getThePage
} // end class Client


  #2 (permalink)  
Old September 23rd, 2007, 11:11 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hyderabad, A.P., India.
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

There is no method with the name setTextFieldEditable() in your client class. Check it again. Try defining this method and compile again.

- Rakesh


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with socket connection sowmya1 Java Basics 1 January 23rd, 2006 01:36 PM
how to create a socket connection? Janet Classic ASP Professional 0 December 30th, 2003 06:42 PM
Secure Socket Connection planeswalk ASP.NET 1.0 and 1.1 Professional 0 July 22nd, 2003 11:04 PM
Secure Socket Connection planeswalk Pro VB.NET 2002/2003 0 July 22nd, 2003 08:24 PM





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