aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Transfering data from the textbox in one class to a method in another class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Transfering data from the textbox in one class to a method in another class" Watch "Transfering data from the textbox in one class to a method in another class" New topic
Author

Transfering data from the textbox in one class to a method in another class

Mikael Fox
Greenhorn

Joined: Mar 22, 2009
Posts: 23
Hello everyone,
I am having a problem, I want to transfer the data from a text field which is found in the class "ServerInterface" to a method in another class "Server".


I wanted to transfer data to the method "sendData()" in the class Server when the user enter data in the textfield "txtSendMessage" and click on the button "btnSend" 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.

I am send the two class as attachment.

Thanks in advance.





Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Please edit your post and delete the javadoc tags and add code tags. Your code is illegible.
Please read the important administrative private message I just sent you.

CR
Mikael Fox
Greenhorn

Joined: Mar 22, 2009
Posts: 23
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mikael Fox
Greenhorn

Joined: Mar 22, 2009
Posts: 23
OK .. Sorry for that . I have already changed the name and the tag code. Hope to find an answer soon, thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18


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.
Mikael Fox
Greenhorn

Joined: Mar 22, 2009
Posts: 23
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:


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Close. "this()" or "super()" must be the first statement in any constructor (if present), so you need to swap those two lines.
Mikael Fox
Greenhorn

Joined: Mar 22, 2009
Posts: 23
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??

Thanks in advance for the answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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).
Mikael Fox
Greenhorn

Joined: Mar 22, 2009
Posts: 23
Thanks a lot for the answer.

If I understand well .."this()" or "super()" is called a constructor. Its the default constructor that the compiler will insert if not present.

But in my case, if I put this("Jim"), what will happen ??


and if i put super("Jim") ?? Super will call a parent class or method ?? And which class or method?? I am a bit confuse.

Thanks a lot.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
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.
Mikael Fox
Greenhorn

Joined: Mar 22, 2009
Posts: 23
Ok ... now I have understand. Thank you very much for this nice explanation Campbell.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transfering data from the textbox in one class to a method in another class