Java Reference
In-Depth Information
38 HBox hBox =
new
HBox(
2
);
39 pane.add(hBox,
1
,
2
);
40 hBox.getChildren().addAll(tfCity,
new
Label(
"State"
), tfState,
41
new
Label(
"Zip"
), tfZip);
42 pane.add(btRegister,
1
,
3
);
43 GridPane.setHalignment(btRegister, HPos.RIGHT);
44
45 pane.setAlignment(Pos.CENTER);
46 tfName.setPrefColumnCount(
15
);
47 tfStreet.setPrefColumnCount(
15
);
48 tfCity.setPrefColumnCount(
10
);
49 tfState.setPrefColumnCount(
2
);
50 tfZip.setPrefColumnCount(
3
);
51
52
btRegister.setOnAction(
new
ButtonListener());
register listener
53
54
// Create a scene and place it in the stage
55 Scene scene =
new
Scene(pane,
450
,
200
);
56 primaryStage.setTitle(
"StudentClient"
);
// Set the stage title
57 primaryStage.setScene(scene);
// Place the scene in the stage
58 primaryStage.show();
// Display the stage
59 }
60
61
/** Handle button action */
62
private class
ButtonListener
implements
EventHandler<ActionEvent> {
63 @Override
64
public void
handle(ActionEvent e) {
65
try
{
66
// Establish connection with the server
67
Socket socket =
new
Socket(host,
8000
);
server socket
68
69
// Create an output stream to the server
70
ObjectOutputStream toServer =
output stream
71
new
ObjectOutputStream(socket.getOutputStream());
72
73
// Get text field
74 String name = tfName.getText().trim();
75 String street = tfStreet.getText().trim();
76 String city = tfCity.getText().trim();
77 String state = tfState.getText().trim();
78 String zip = tfZip.getText().trim();
79
80
// Create a Student object and send to the server
81
StudentAddress s =
82
new
StudentAddress(name, street, city, state, zip);
83
toServer.writeObject(s);
send to server
84 }
85
catch
(IOException ex) {
86 ex.printStackTrace();
87 }
88 }
89 }
90 }
L
ISTING
31.7
StudentServer.java
1
import
java.io.*;
2
import
java.net.*;
3
4
public class
StudentServer {
Search WWH ::
Custom Search