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.

Good morning neutralfox. Wow, that server code really has been cleaned up!
All you need to do in response to the button being clicked is to get the text from the input field and pass it as the parameter to the server's send method. The only problem I see in your code is that the send method is an instance method, and you were trying to invoke it as a class method. ie instead of Server.sendData it should be cs1.sendData.
ps. Maybe a good time to mark the previous thread as "solved"?

Hello, Good Morning to you too. Already marked as solved.

Concerning my new problem, I am unable to send the message to the method sendData() . I have tried your solution i.e cs1.sendData, still not working.

I think the problem is because I have instantiated the cs1 in the main and the button event is not detecting that instance.

public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ServerInterface().setVisible(true);
                Server cs1 = new Server();
                cs1.start();
               
               
            }
        });
    }

Please give me the solution to pass the data to the sendData() method when the button is clicked.

If I used cs1.sendData as you mentioned. Netbeans does not detect the cs1._________ because the instance cs1 have been instantiated in the main. This is what I have understand, maybe I am wrong !!

Its the first time I am using NetBeans GUI Builder, I don't know how to merge these 2 classes together. That is, how to call a method from an other class on an action event in the GUI(another class). Note: The program in working well without the GUI.


Thanks in advance.

When you create the GUI, pass the server instance (cs1) as a parameter to the GUI's constructor. (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.

Server cs1 = new Server();
 new ServerInterface(cs1).setVisible(true);

Thanks a lot for the answer but its too complicated for me. I have not been able to implement what you just said. Could you please give me an example.

You need to write a very short constructor that just saves the server reference then calls the default constructor

Its this part that I have not understood.

Thanks in advance James.

The new constructor for ServerInterface needs to look like this:

Server theServer; // Add a variable to the GUI class to hold a reference to the server
public ServerInterface(Server s) { // add a new constructor that takes a server reference as parameter
theServer = s; // save a ref to the server
this(); // call the original (default) constructor
}

I have tried it but still not working, I am getting some errors. I am sending the ServerInterface() with the modification, check it out please.

I the new variable is called theServer, but your call refers to cs1 (which is the variable in the main class, not the one in the GUI). so it would be
theServer.sendData
You original version of the constructors was right, so is the commented latest version, but you may find a problem when netbeans regenerates its code unless you stick to the first version.

Wooowww Wowwww .... You are a genius, its working. If ever you came to Mauritius, I will buy you a beer. Really you rocks. :D.

.. I always wanted to visit Mauritius... mine's a pint!

lol .. Thanks a lot for your precious time and help James.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.