I look around and I found only one answer that was not clear enough, at least for me.
I am creating a very basic GUI chat application and I have separated the GUI from the connection materials. Now I need to call one method from the GUI in the server class and vice versa. But I do not quite understand how to do this (even with "this"). This is what the piece of code looks like (this is a class called server_frame):
textField.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { srv.sendData(arg0.getActionCommand()); } catch (Exception e) { e.printStackTrace(); } textField.setText(""); } } );
This is the code from server_frame, srv is an object from another class (server) that contains the sendData method, and I probably did not define it correctly, so hopefully someone can make its definition.
On the other hand, the class server from which the srv object was created contains a method that uses JTextArea displayArea from server_frame in this code:
private void displayMessage(final String message){ sf = new server_frame(); SwingUtilities.invokeLater(new Runnable(){ public void run(){ sf.displayArea.append(message); } } ); }
And again, sf is an object created from server_frame, and, again, possibly missed :)
I hope this was clear enough, unfortunately, I tried the search, but it just did not give me the results that I was looking for, if you need more information, I will be happy to add it!
Thank you for reading,
Mr.P.
PS Please do not mind if I made failures in the terminology, I am still new to java and open to any corrections!
source share