monkey doodle monkey doodle - 1 month ago 5
Java Question

java GUI trying to get multiple inputs from multiple text fields

How do you get multiple inputs from the multiple textfields you have created?
I want one text field to get the port number, another to get file location.
So once the user enter the int and the string, I can use these inputs for the program.

I am new to this, so when I tried to implement this, I would enter the port number, and suddenly the UI seems to "freeze" and I can't enter the file location.

constructor for frame

public TcpServerCompareCSV () {
setLayout(new FlowLayout());
// "this" Frame sets its layout to FlowLayout, which arranges the components
// from left-to-right, and flow to next row from top-to-bottom.

lblPort = new Label("Port"); // construct Label
add(lblPort); // "this" Frame adds Label

tfPort = new TextField("0", 10); // construct TextField
tfPort.setEditable(true); //edit text
add(tfPort); // "this" Frame adds tfCount
tfPort.addActionListener(this); // for event-handling



lblLocation = new Label("CSV File Location"); // construct Label
add(lblLocation); // "this" Frame adds Label

tfLocation = new TextField("text", 40); // construct TextField
tfLocation.setEditable(true); //edit text
add(tfLocation); // "this" Frame adds tfCount
tfLocation.addActionListener(this);


setTitle("compare"); // "this" Frame sets title
setSize(250, 100); // "this" Frame sets initial window size
setVisible(true); // "this" Frame shows


addWindowListener(this);
// "this" Frame fires WindowEvent its registered WindowEvent listener
// "this" Frame adds "this" object as a WindowEvent listener

}


action event

/** ActionEvent handler - Called back when user clicks the button. */
@Override
public void actionPerformed(ActionEvent evt) {
// Get the String entered into the TextField tfPort, convert to int
port = Integer.parseInt(tfPort.getText());


fileLocation = tfLocation.getText();
String csvName = fileLocation;








ServerSocket serverSocket = null;

try {
serverSocket = new ServerSocket(port);
}
catch (IOException e)
{
System.err.println("Could not listen on port: 57635.");
System.exit(1);
}

Socket clientSocket = null;
System.out.println ("Waiting for connection.....");

try {
clientSocket = serverSocket.accept();
}
catch (IOException e)
{
System.err.println("Accept failed.");
System.exit(1);
}

System.out.println ("Connection successful");
System.out.println ("Waiting for input.....");

PrintWriter out = null;
try {
out = new PrintWriter(clientSocket.getOutputStream(),
true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader in = null;
try {
in = new BufferedReader(
new InputStreamReader( clientSocket.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

String inputLine, outputLine;

try {
while ((inputLine = in.readLine()) != null)
{
System.out.println ("Server: " + inputLine);



if (inputLine.trim().equals("Bye.")) {
System.out.println("Exit program");
break;
}

Scanner input1 = new Scanner(new File(csvName));
Scanner input2 = new Scanner(new File(csvName));
Scanner input3 = new Scanner(new File(csvName));
Scanner input4 = new Scanner(new File(csvName));


String csvline = getCsvLineVal (getLocation34CSV(getTag34Value(Tag34Location(getTagCSV( parseFixMsg(inputLine ,inputLine))), getValueCSV( parseFixMsg(inputLine ,inputLine))), getVal34(input1, input2)), getCSVLine( input3, input4) );
outputLine = compareClientFixCSV( getTagCSV( parseFixMsg(inputLine ,inputLine)), getValueCSV(parseFixMsg(inputLine ,inputLine)), getCSVTag(csvline), getCSVValue(csvline));

out.println(outputLine);

input1.close();
input2.close();
input3.close();
input4.close();



}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


out.close();
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
clientSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}




}

Answer

By adding the ActionListener to both text fields, if I remember correctly, they will fire the event as soon as you hit Return in one of them. That will trigger your network code immediately.

You should only register the action for the button.

Moreover, as was already pointed out in a comment, you are executing network communications on the GUI thread, which is causing the freeze.

In the action implementation, you must spawn a separate thread to do the actual network communications to prevent the blocking. To find out how that works, look at the documentation for Runnable and the Executor framework.

Comments