File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Coding a Client side program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Coding a Client side program" Watch "Coding a Client side program" New topic

Coding a Client side program

Simon Evans
Ranch Hand

Joined: Mar 27, 2008
Posts: 93
Dear Norm Radder, and other programmers,

Thank you for your recommendations. I have put the code below into code tags.I hope they are appropriate.

I will try describing the difficulty I am encountering with the code more clearly than I did before.

Because I (or anyone, presumably) can't write ClientGUI extends JFrame, Thread - (though my Ivor Horton book says you can)
- I made two classes for ClientGUI - one holding the main method which instantiates and calls methods held within the other ClientGui program.

However there is the consequent problem in that ClientGUI holds an ActionListener/ Performed method - so it can't object.method() call from

the MainMethod class - ie: it can't invoke the sendmessage() method within it from the MainMethod, as the ActionPerformed is to press the

button 'SEND' which is in the GUI created by ClientGUI program.

The other methods like getConnection() can be object method called from within the MainMethod, as they are not contingent on an

ActionListener/Performed type method. If I copy the object.method() call process that invokes these methods in the MainMethod into the

ActionListener/Performed method in the clientGUI so that it might read :-

ClientGUI clientgui = new ClientGUI();

-then upon pressing the GUI's 'SEND' button I just get another GUI - from the instantiation, without the sendMessage() method ever being

reached, or executed.

Okay, maybe making two programs out of one wasn't such a cool idea after all, but then how do I extend one program from JPane (which I need

for the GUI) and Thread (which I need also to thread the sendMessage(), recieveMessage() methods?

I was thinking it'd be easier to get an idea of the problem if you just ran these programs, as writ, as they compile and run okay, they just

don't work (ie: take messages to the GUI'S textfield to and fro from Server and Client consoles, nor from GUI's textfield to textArea) like I

(or anyone) would wish them to. *****************************

As you may know, you have to run the Server program first, then the MainMethod to launch the ClientGUI program. I think the methods in ClientGUI are coded alright - to put input to text field to input and output streams and to transfer message thus initialised to the textArea.
Anyway thank you much for your help, particularly as most people out there are preoccupied with loot before they're prepared to offer any help.

[ July 26, 2008: Message edited by: Simon Evans ]

[ July 26, 2008: Message edited by: Simon Evans ]

[ July 26, 2008: Message edited by: Simon Evans ]
[ July 26, 2008: Message edited by: Simon Evans ]

Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Not sure why the MainMethod class extends Thread as there is not a run() method in it. Why do you need a Thread object?
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

I didn't read through everything in your comments (partly because the opening comments have a couple of scary run on sentences). But this comment did jump out at me:

Originally posted by Simon Evans:

...I can't subclsss (extend) it from the Jframe and Thread class using extends JFrame, Thread as the compiler doesn't like it...

In Java, you can only extend a single class. Extending multiple classes can result in the Diamond of Death, so Java doesn't allow it. But a class can implement multiple interfaces. And a class can extend one class and implement one or more interfaces.

So extend JFrame and implement the Runnable interface. You will then need to implement Ruannable's run() method in your class. The Thread class actually implements Runnable. But generally when you want to make something so it can run in a separate thread, you implement the Runnable method. You can then construct a Thread passing in the class that implements the Runnable interface.

For more information on multi-threading, see the Java Tutorial lesson on Concurrency. You may then want to check out the fine book Java Threads, Third Edition, co-authored by Scott Oaks and Henry Wong. Henry is one of the moderators here at JavaRanch.

Again, this is just some basic information based on a quick look at your stuff. But if I follow the gist of what yo want to do, that should help.

Some housekeeping notes...

I recommend you edit your post above and wrap your code in UBB [code] tags. Also see the UseCodeTags FAQ. You can just click the edit button , highlight the code, click the "code" button below the editing text field, then click the "Edit Post" button to save the changes. People will generally not want to spend the time reading unformatted code. So you will get more responses if you make the change.

I would also recommend editing your comments and questions so they are a clearer. As explained in this FAQ, while people here want to help others, if they have to spend too much time trying to figure out what you are asking, they will simply move on to other threads. We're all computer nuts... so for us it's fun answering computer questions... it's not fun trying to figure out what the question is.

Beat Regards,
[ July 25, 2008: Message edited by: Mark Vedder ]
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
In your listener class, you need a reference to the existing client class. You do NOT want to create a new one. Pass a reference to the client class when you create the listener and have the listener's constructor save it to be used by the actionPerformed method.
Henry Wong

Joined: Sep 28, 2004
Posts: 18553

I have put the code below into code tags.I hope they are appropriate.

The purpose of code tags is to preserve formatting -- which is, of course, to help with readibility. However, it doesn't look like you have any formatting in your code.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
subject: Coding a Client side program
Similar Threads
help with actionPerformed method.
Using the 'Enter' button in an ActionListener method
Java socket server running from witin weblogic
Downloading Java 1.5 (or Java 5.0 also Socket programming
Reseting socket after few seconds