I am having a problem, I want to transfer the data from a text field which is found in the class &amp;quot;ServerInterface&amp;quot; to a method in another class &amp;quot;Server&amp;quot;.
I wanted to transfer data to the method &amp;quot;sendData()&amp;quot; in the class Server when the user enter data in the textfield &amp;quot;txtSendMessage&amp;quot; and click on the button &amp;quot;btnSend&amp;quot; found in ServerInterface.
I am new to Java and Socktet programming, the thing is that I have finally managed to understand socket programming after lots of hard work but my problem is that I am unable to merge my interface with the other class, could you please help me on this please.
What ?? This is the worst forum I ever been. You can't even upload a java file and its not user friendly at all. No need to help me ... Delete my account if you wish, i will not logged in again and but the way I hate when someone give me orders. This does not mean that I am not polite, this is the first time I posted over here and you gave me a very bad impression on this forum.
Bye and my code works perfectly, I am just having a problem to pass the message to the Server Class.
neutral fox wrote:its not user friendly at all. No need to help me ...
I hate when someone give me orders.
How can you say that? Be Nice is one of our few rules, and I haven't seen any unfriendliness against you.
Campbell's first comment was purely advice for your own good. If your code is not readable, people will ignore your post and you will not get any help. In the end, adding code tags is a little extra effort for you and you will get more responses.
As for the private message, this is one of the very few enforced rules we have at this board. You came here because you thought we could help you (and we can!), but if this one rule is too strict for you then feel free to leave.
That's the method that gets called when the button is pressed. All you need to do in that method is retrieve the contents from the text field and send that:
Since this is mostly a user interface issue I'm moving this to our Swing forum.
I got the answer. The above code will not works because sendData is an instance method. You should do this :
You need to write a very short constructor that just saves the server reference then calls the default constructor. That way the GUI will have a reference to the server instance and will be able to call its methods.
The new constructor for ServerInterface needs to look like this:
Thanks for the tips but what is the purpose of "this()" or "super()"?
I used "this()" before but it was throwing an error. I just removed it and everything works fine. Now as you mentioned, I tried "super()" and it was OK. But what is the purpose of these things?? Is it a method??
With "this()", you call another constructor. You can add parameters too - "this()" calls the constructor without any parameters, whereas "this(x)" will call the the constructor that takes an int if x is an int. super() works similar, only it calls a constructor from the parent class, not the same class.
Each constructor must start with either "this()" or "super()" (with any number of parameters. If you don't add it yourself, the compiler will add this call for you (without any parameters).
If you write this("Jim"); which you remember has to be the first line in the constructor, this happens:-
The compiler (or JVM) thinks, "Ah, he is looking for another constructor to call before going on with this constructor. He is passing "Jim" as a parameter. Now, "Jim" is a String, so let's find the version of the constructor which takes a String. Oh, here it is. Well, we'll go through this constructor, execute it, and then go back to the original constructor."
If you are using that idiom, you may find that this("Jim"); can be the whole of your constructor, because you can get the other constructor to do all the work.
If you had written super("Jim"); the same sort of thing would have happened, only the JVM/compiler would look in the immediate superclass rather than in the same class for the constructor.
You can't write both this(); and super(); in the same constructor because they each must be the first line.