I'm learning some basic java networking stuff and I'm trying to make what I learned come to life, so when I run my server and client classes on the same computer it runs without errors, but when I take the client project to another computer and run the project after running the server it freezes and prints a connection timed out statement.
here is my server code
package sample;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
TextArea ta = new TextArea();
primaryStage.setTitle("server");
primaryStage.setScene(new Scene(new ScrollPane(ta), 450, 200));
primaryStage.show();
new Thread(()->{
try {
ServerSocket ss = new ServerSocket(8000);
Platform.runLater(() ->
ta.appendText("Server started at " + new Date() + '\n'));
Socket s = ss.accept();
DataInputStream inputFromClient = new DataInputStream(s.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(s.getOutputStream());
while (true) {
double radius = inputFromClient.readDouble();
double area = radius * radius * Math.PI;
outputToClient.writeDouble(area);
Platform.runLater(() -> {
ta.appendText("Radius received from client: "
+ radius + '\n');
ta.appendText("Area is: " + area + '\n');
});
}
} catch (Exception e){
e.printStackTrace();
}
}).start();
}
public static void main(String[] args) {
launch(args);
}
}
and this is my client
package sample;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class Main extends Application {
DataOutputStream toServer = null;
DataInputStream fromServer = null;
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane();
pane.setPadding(new Insets(5, 5, 5, 5));
pane.setStyle("-fx-border-color: green");
pane.setLeft(new Label("Enter a radius: "));
TextField tf = new TextField();
tf.setAlignment(Pos.BOTTOM_RIGHT);
pane.setCenter(tf);
BorderPane mainPane = new BorderPane();
TextArea ta = new TextArea();
mainPane.setCenter(new ScrollPane(ta));
mainPane.setTop(pane);
Scene scene = new Scene(mainPane, 450, 200);
primaryStage.setTitle("Client"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
tf.setOnAction(e -> {
try {
Socket socket = new Socket("server IP address", 8000);
fromServer = new DataInputStream(socket.getInputStream());
toServer = new DataOutputStream(socket.getOutputStream());
} catch (IOException ex) {
ta.appendText(ex.toString() + '\n');
}
try {
double radius = Double.parseDouble(tf.getText().trim());
toServer.writeDouble(radius);
toServer.flush();
double area = fromServer.readDouble();
ta.appendText("Radius is " + radius + "\n");
ta.appendText("Area received from the server is "
+ area + '\n');
} catch (IOException ex) {
System.err.println(ex);
}
});
}
}
problem solved
I just had to change the network settings on my server so that it will be discoverable on my network
Within the server class you create a server socket on the port 8000, which is fine for both internal and external connections. However your client class attempts to create a socket with no given IP Address
Socket socket = new Socket("server IP address", 8000);
By passing the string "Server ip address", you're essentially telling java to look for a local server, as you did not pass in a proper IP Address.
So when both classes are running on the same system only the port matters, but you need to identify the IP Address of the server for the client to know where to look for its connection if they're not on the same system.
Related
My assignment is to get this simple client/server program to run, it sends the radius of a circle to a server and the area is returned. I literally just copied and pasted the code and tried to run it but I get this error. I have included the code for both client and server side below. Thanks.
Error: Could not find or load main class Client_Server.Client_Side
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
package Client_Server;
import java.io.*;
import java.net.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Client_Side extends Application
{
// IO streams
DataOutputStream toServer = null;
DataInputStream fromServer = null;
#Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Panel p to hold the label and text field
BorderPane paneForTextField = new BorderPane();
paneForTextField.setPadding(new Insets(5, 5, 5, 5));
paneForTextField.setStyle("-fx-border-color: green");
paneForTextField.setLeft(new Label("Enter a radius: "));
TextField tf = new TextField();
tf.setAlignment(Pos.BOTTOM_RIGHT);
paneForTextField.setCenter(tf);
BorderPane mainPane = new BorderPane();
// Text area to display contents
TextArea ta = new TextArea();
mainPane.setCenter(new ScrollPane(ta));
mainPane.setTop(paneForTextField);
// Create a scene and place it in the stage
Scene scene = new Scene(mainPane, 450, 200);
primaryStage.setTitle("Client"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
tf.setOnAction(e -> {
try {
// Get the radius from the text field
double radius = Double.parseDouble(tf.getText().trim());
// Send the radius to the server
toServer.writeDouble(radius);
toServer.flush();
// Get area from the server
double area = fromServer.readDouble();
// Display to the text area
ta.appendText("Radius is " + radius + "\n");
ta.appendText("Area received from the server is "
+ area + '\n');
}
catch (IOException ex) {
System.err.println(ex);
}
});
try {
// Create a socket to connect to the server
Socket socket = new Socket("localhost", 8000);
// Socket socket = new Socket("130.254.204.36", 8000);
// Socket socket = new Socket("drake.Armstrong.edu", 8000);
// Create an input stream to receive data from the server
fromServer = new DataInputStream(socket.getInputStream());
// Create an output stream to send data to the server
toServer = new DataOutputStream(socket.getOutputStream());
}
catch (IOException ex) {
ta.appendText(ex.toString() + '\n');
}
}
public static void main(String[] args) {
launch(args);
}
}
package Client_Server;
import java.io.*;
import java.net.*;
import java.util.Date;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
public class Server_Side extends Application {
#Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Text area for displaying contents
TextArea ta = new TextArea();
// Create a scene and place it in the stage
Scene scene = new Scene(new ScrollPane(ta), 450, 200);
primaryStage.setTitle("Server"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
new Thread( () -> {
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
Platform.runLater(() ->
ta.appendText("Server started at " + new Date() + '\n'));
// Listen for a connection request
Socket socket = serverSocket.accept();
// Create data input and output streams
DataInputStream inputFromClient = new DataInputStream(
socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(
socket.getOutputStream());
while (true) {
// Receive radius from the client
double radius = inputFromClient.readDouble();
// Compute area
double area = radius * radius * Math.PI;
// Send area back to the client
outputToClient.writeDouble(area);
Platform.runLater(() -> {
ta.appendText("Radius received from client: "
+ radius + '\n');
ta.appendText("Area is: " + area + '\n');
});
}
}
catch(IOException ex) {
ex.printStackTrace();
}
}).start();
}
public static void main(String[] args) {
launch(args);
}
}
If you are a Student you can get the Ultimate version of Intellij which simplifies settings for new projects..
Ijust tested your code and it works..
I created a new JFX project.. I added the right SDK.. for JFX is Java 11 the minimum.. so I downloaded it and created the project.. then I created 2 normal classes in the pre-created package one for Client_Side and one for Server_Side .. then run first the server class and it works
As you seem to manage compilation, your Runtime Environment misses an JavaFX-Distribution. You could either install an Runtime Environment with JavaFX, for example Liberica, Correto or Oracles 1.8-Runtime.
Otherwise, you may also run your class and add OpenJFX-JARs to your classpath. For more information about how to set the classpath, have a look at the Oracle Docs.
Another option is to install OpenJFX on your operating system. You can download the OpenJFX JARs from Maven repositories, as well. However, you must get the JARs for the correct platform (mac, linux or win classifier).
here is my first class client
package hwch33;
import java.net.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.*;
public class Client extends Application {
// IO streams
DataOutputStream toServer = null;
DataInputStream fromServer = null;
#Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Panel p to hold the label and text field
BorderPane paneForTextField = new BorderPane();
paneForTextField.setPadding(new Insets(5, 5, 5, 5));
paneForTextField.setStyle("-fx-border-color: green");
paneForTextField.setLeft(new Label("Enter a radius: "));
TextField tf = new TextField();
tf.setAlignment(Pos.BOTTOM_RIGHT);
paneForTextField.setCenter(tf);
BorderPane mainPane = new BorderPane();
// Text area to display contents
TextArea ta = new TextArea();
mainPane.setCenter(new ScrollPane(ta));
mainPane.setTop(paneForTextField);
// Create a scene and place it in the stage
Scene scene = new Scene(mainPane, 450, 200);
primaryStage.setTitle("Client"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
tf.setOnAction(e -> {
try {
// Get the radius from the text field
double radius = Double.parseDouble(tf.getText().trim());
// Send the radius to the server
toServer.writeDouble(radius);
toServer.flush();
// Get area from the server
double area = fromServer.readDouble();
// Display to the text area
ta.appendText("Radius is " + radius + "\n");
ta.appendText("Area received from the server is "
+ area + '\n');
}
catch (IOException ex) {
System.err.println(ex);
}
});
try {
// Create a socket to connect to the server
Socket socket = new Socket("localhost", 8000);
//Socket socket = new Socket("130.254.204.36", 8000);
//Socket socket = new Socket("drake.Armstrong.edu", 8000);
// Create an input stream to receive data from the server
fromServer = new DataInputStream(socket.getInputStream());
// Create an output stream to send data to the server
toServer = new DataOutputStream(socket.getOutputStream());
}
catch (IOException ex) {
ta.appendText(ex.toString() + '\n');
}
}
}
here is my second class server
package hwch33;
import java.io.*;
import java.net.*;
import java.util.Date;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
public class Server extends Application {
/**
*
* #param primaryStage
*/
#Override
public void start(Stage primaryStage) {
// Text area for displaying contents
TextArea ta = new TextArea();
// Create a scene and place it in the stage
Scene scene = new Scene(new ScrollPane(ta), 450, 200);
primaryStage.setTitle("Server"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
new Thread(() -> {
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
Platform.runLater(() ->
ta.appendText("Server started at " + new Date() + '\n'));
Socket socket = serverSocket.accept();
DataInputStream inputFromClient = new DataInputStream(
socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(
socket.getOutputStream());
while (true) {
double radius = inputFromClient.readDouble();
double area = radius * radius * Math.PI;
outputToClient.writeDouble(area);
Platform.runLater(() -> {
ta.appendText("Radius received from client: "
+ radius + '\n');
ta.appendText("Area is: " + area + '\n');
});
}
}
catch(IOException ex) {
ex.printStackTrace();
}
}).start();
}
}
Here is where i need to add the code to make it all work
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hwch33;
/**
*
* #author
*/
public class HWCH33 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
}
}
Not sure how to construct a main from here.
Ive been trying to form one but cannot get it right. I either get static error or it just doesnt work at all.
Any help is appreciated. Did not include any of my previous attempts because they are useless and I got rid of them all.
If the error is that a non-static variable is not compatible with a static method or something along those lines, just omit the word "static" from public static void main(String[] args){}.
I have to create Java program with GUI that simply is a text chat between a server and a client. I have referred to my professor's Powerpoint presentation and copied example code of a server and client program that where the client types a circle radius, and the server automatically calculates and returns the client the circle area (The example code did not have a textfield for input). I have changed up the example code into code where the server and client can chat with each other in text. My Client class works and I did not have to change anything in the Client class except that the text inputted stays as a string and not a double.
I tested my server. Why does my server freeze when I hit enter after inputting simple text?
Server:
import java.io.*;
import java.net.*;
import javafx.geometry.Insets;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.geometry.Insets;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.Label;
import javafx.geometry.Pos;
public class Server extends Application {
#Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Text area for displaying contents
BorderPane paneForTextField = new BorderPane();
paneForTextField.setPadding(new Insets(5, 5, 5, 5));
paneForTextField.setStyle("-fx-border-color: green");
paneForTextField.setLeft(new Label("Type here: "));
TextField tf = new TextField();
tf.setAlignment(Pos.BOTTOM_RIGHT);
paneForTextField.setCenter(tf);
BorderPane mainPane = new BorderPane();
TextArea ta = new TextArea();
mainPane.setCenter(new ScrollPane(ta));
mainPane.setTop(paneForTextField);
// Create a scene and place it in the stage
Scene scene = new Scene(mainPane, 450, 200);
primaryStage.setTitle("Server"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
try {
Socket socket = new Socket("localhost", 8000);
// Socket socket = new Socket("130.254.204.36", 8000);
// Socket socket = new Socket("drake.Armstrong.edu", 8000);
// Create an input stream to receive data from the server
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
// Create an output stream to send data to the server
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
// Get the radius from the text field
String serverText = tf.getText();
// Send the radius to the server
outputToClient.writeBytes(serverText);
outputToClient.flush();
// Get area from the server
String clientText = inputFromClient.readLine();
// Display to the text area
ta.appendText("Client: " + clientText + "\n");
ta.appendText("Server: " + serverText + '\n');
}
catch (IOException ex) {
System.err.println(ex.toString() + '\n');
}
tf.setOnAction(e -> {
new Thread( () -> {
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
Platform.runLater(() ->
ta.appendText("Server is working" + '\n'));
// Listen for a connection request
Socket socket = serverSocket.accept();
// Create data input and output streams
DataInputStream inputFromClient = new DataInputStream(
socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(
socket.getOutputStream());
while (true) {
// Receive radius from the client
String clientText = inputFromClient.readLine();
String serverText = tf.getText();
// Compute area
// Send area back to the client
outputToClient.writeBytes("Server: " + serverText);
Platform.runLater(() -> {
ta.appendText("Client: " + clientText + '\n');
ta.appendText("Server: " + serverText + '\n');
});
}
}
catch(IOException ex) {
ex.printStackTrace();
}
}).start();
});
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
Client
import java.io.*;
import java.net.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Client extends Application {
// IO streams
DataOutputStream toServer = null;
DataInputStream fromServer = null;
#Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Panel p to hold the label and text field
BorderPane paneForTextField = new BorderPane();
paneForTextField.setPadding(new Insets(5, 5, 5, 5));
paneForTextField.setStyle("-fx-border-color: green");
paneForTextField.setLeft(new Label("Type here: "));
TextField tf = new TextField();
tf.setAlignment(Pos.BOTTOM_RIGHT);
paneForTextField.setCenter(tf);
BorderPane mainPane = new BorderPane();
// Text area to display contents
TextArea ta = new TextArea();
mainPane.setCenter(new ScrollPane(ta));
mainPane.setTop(paneForTextField);
// Create a scene and place it in the stage
Scene scene = new Scene(mainPane, 450, 200);
primaryStage.setTitle("Client"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
tf.setOnAction(e -> {
try {
// Get the text from the text field
String clientText = tf.getText();
// Send the text to the server
toServer.writeBytes(clientText);
toServer.flush();
// Get text from the server
String serverText = fromServer.readLine();
// Display to the text area
ta.appendText("Client: " + clientText + "\n");
ta.appendText("Server: " + serverText + '\n');
}
catch (IOException ex) {
System.err.println(ex);
}
});
try {
// Create a socket to connect to the server
Socket socket = new Socket("localhost", 8000);
// Create an input stream to receive data from the server
fromServer = new DataInputStream(socket.getInputStream());
// Create an output stream to send data to the server
toServer = new DataOutputStream(socket.getOutputStream());
}
catch (IOException ex) {
ta.appendText(ex.toString() + '\n');
}
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
So the code you see below is from a powerpoint my professor gave us. It should be correct, because our textbook made these powerpoints. This code below is an example code of a client sending a circle radius to a server. The server calculates the circle's area. The issue is, I can't test the code, because I need both the server and client open simultaneously. How do I test the code if both can't be running? I am using JGRASP by the way.
Server Class
import java.io.*;
import java.net.*;
import java.util.Date;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
public class Server extends Application {
#Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Text area for displaying contents
TextArea ta = new TextArea();
// Create a scene and place it in the stage
Scene scene = new Scene(new ScrollPane(ta), 450, 200);
primaryStage.setTitle("Server"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
new Thread( () -> {
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
Platform.runLater(() ->
ta.appendText("Server started at " + new Date() + '\n'));
// Listen for a connection request
Socket socket = serverSocket.accept();
// Create data input and output streams
DataInputStream inputFromClient = new DataInputStream(
socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(
socket.getOutputStream());
while (true) {
// Receive radius from the client
double radius = inputFromClient.readDouble();
// Compute area
double area = radius * radius * Math.PI;
// Send area back to the client
outputToClient.writeDouble(area);
Platform.runLater(() -> {
ta.appendText("Radius received from client: "
+ radius + '\n');
ta.appendText("Area is: " + area + '\n');
});
}
}
catch(IOException ex) {
ex.printStackTrace();
}
}).start();
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
Client Class
import java.io.*;
import java.net.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Client extends Application {
// IO streams
DataOutputStream toServer = null;
DataInputStream fromServer = null;
#Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Panel p to hold the label and text field
BorderPane paneForTextField = new BorderPane();
paneForTextField.setPadding(new Insets(5, 5, 5, 5));
paneForTextField.setStyle("-fx-border-color: green");
paneForTextField.setLeft(new Label("Enter a radius: "));
TextField tf = new TextField();
tf.setAlignment(Pos.BOTTOM_RIGHT);
paneForTextField.setCenter(tf);
BorderPane mainPane = new BorderPane();
// Text area to display contents
TextArea ta = new TextArea();
mainPane.setCenter(new ScrollPane(ta));
mainPane.setTop(paneForTextField);
// Create a scene and place it in the stage
Scene scene = new Scene(mainPane, 450, 200);
primaryStage.setTitle("Client"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
tf.setOnAction(e -> {
try {
// Get the radius from the text field
double radius = Double.parseDouble(tf.getText().trim());
// Send the radius to the server
toServer.writeDouble(radius);
toServer.flush();
// Get area from the server
double area = fromServer.readDouble();
// Display to the text area
ta.appendText("Radius is " + radius + "\n");
ta.appendText("Area received from the server is "
+ area + '\n');
}
catch (IOException ex) {
System.err.println(ex);
}
});
try {
// Create a socket to connect to the server
Socket socket = new Socket("localhost", 8000);
// Socket socket = new Socket("130.254.204.36", 8000);
// Socket socket = new Socket("drake.Armstrong.edu", 8000);
// Create an input stream to receive data from the server
fromServer = new DataInputStream(socket.getInputStream());
// Create an output stream to send data to the server
toServer = new DataOutputStream(socket.getOutputStream());
}
catch (IOException ex) {
ta.appendText(ex.toString() + '\n');
}
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
Run either one of them from the command line, and the other from jGRASP.
I am not sure if this is a dumb question but I need help with this program I was creating. I wanted to make a login screen to this instant messenger app, which I already have created but I made it in javaFX. The problem is that since this instant messenger app is old, I created it with swing. Since there are no scenes in swing and there are in javafx, then when I hit login I would normally change the scene and that's it. That is why I am changing this program to javafx from swing but I am getting a couple of errors, if anyone is kind enough to tell me what I did wrong I would be grateful. Thanks and sorry for being so redundant, remember i'm just a beginner in 9th grade.
The exact error is in the line 56
The method ActionListener(new ActionListener(){}) is undefined for the type TextField
package messengerClient;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.*;
import javafx.stage.*;
import javafx.stage.Stage;
import javax.swing.JFrame;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import java.awt.*;
import java.awt.event.ActionEvent;
public class ClientFX extends Application {
private TextField userText;
private TextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String serverIP;
private Socket connection;
Stage window;
Scene scene1,scene2;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("Client - Instant Messenger!");
ClientFX juan = new ClientFX("127.0.0.1");
juan.startRunning();
}
//CONSTRUCTOR
public ClientFX(String host){
serverIP = host;
ScrollPane sp = new ScrollPane();
sp.setContent(chatWindow);
userText = new TextField();
userText.setEditable(false);
userText.ActionListener(
new ActionListener(){
#Override
public void actionPerformed(ActionEvent event){
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
BorderPane layout1 = new BorderPane();
layout1.setTop(userText);
layout1.setCenter(chatWindow);
chatWindow = new TextArea();
scene2 = new Scene(layout1,500,300);
window.setScene(scene2);
window.show();
}
//CONNECT TO SERVER
public void startRunning(){
try{
connectToServer();
setupStreams();
whileChatting();
}catch(EOFException eofException){
showMessage("\n Client terminated connection");
}catch(IOException ioException){
ioException.printStackTrace();
}finally{
closeCrap();
}
}
//CONNECT TO SERVER
private void connectToServer() throws IOException{
showMessage("Attempting connection... \n");
connection = new Socket(InetAddress.getByName(serverIP), 6789);
showMessage("You have succesfully connected to: " + connection.getInetAddress().getHostName());
}
//SET UP STREAMS TO SEND AND RECIVE MESSAGES
private void setupStreams() throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\nYour streams are now ready to go! \n");
}
//WHILE CHATTING WITH THE SERVER
private void whileChatting() throws IOException{
ableToType(true);
do{
try{
message = (String) input.readObject();
showMessage("\n" + message);
}catch(ClassNotFoundException classnotfoundException){
showMessage("\nI do not know that object type");
}
}while(!message.equals("Server - END"));
}
//CLOSE THE STREAMS AND THE SOCKETS
private void closeCrap(){
showMessage("\nClosing streams and the sockets down...");
ableToType(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//SEND MESSAGES TO THE SERVER
private void sendMessage(String message){
try{
output.writeObject("CLIENT - " + message);
output.flush();
showMessage("\nCLIENT - " + message);
}catch(IOException ioException){
chatWindow.appendText("\nThat data type cannot be sent, sorry!");
}
}
//UPDATE CHAT WINDOW
private void showMessage(final String m){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatWindow.appendText(m);
}
}
);
}
//GIVES USER PERMISION TO TYPE INTO THE TEXT BOX
private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
userText.setEditable(tof);
}
}
);
}
}
The problem is you are mixing swing and javafx. You are importing both:
import java.awt.event.*;
import javax.swing.*;
import javafx.application.Application;
...
import javafx.scene.Scene;
import javafx.scene.control.Button;
For example in the ClientFX constructor you are trying to add a javax.swing.JTextField (a swing component) to a javafx.scene.BorderPane (a javafx component). The JTextField does not extend javafx.scene.Node, therefore it cannot be given to BorderPane.setTop(Node).
Basically for any swing component (often start with J, JTextField, JTextArea etc.) find the javafx equivalent (for example javafx.scene.control.TextField, javafx.scene.control.TextArea) and learn how to use them (they will have a different api).
In the end you should not have an import statement for java.awt or javax.swing.
Update
As already said you need to learn how to use the new fx apis. Not only the package names, classes and methods change, but also maybe programming concepts.
To port your JTextField behaviour you use a TextField and call the setOnAction method with an EventHandler implementation (very similar to the swing concept):
userText.setOnAction(new EventHandler<javafx.event.ActionEvent>()
{
#Override
public void handle(javafx.event.ActionEvent event)
{
//
}
});