|
| 1 | +package exercise_31_06Client; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.net.*; |
| 5 | +import javafx.application.Application; |
| 6 | +import javafx.geometry.Insets; |
| 7 | +import javafx.geometry.Pos; |
| 8 | +import javafx.scene.Scene; |
| 9 | +import javafx.scene.control.Button; |
| 10 | +import javafx.scene.control.Label; |
| 11 | +import javafx.scene.control.TextField; |
| 12 | +import javafx.scene.layout.BorderPane; |
| 13 | +import javafx.scene.layout.GridPane; |
| 14 | +import javafx.scene.layout.HBox; |
| 15 | +import javafx.stage.Stage; |
| 16 | + |
| 17 | +public class Exercise_31_06Client extends Application implements StudentAddressConstants { |
| 18 | + private TextField tfName = new TextField(); |
| 19 | + private TextField tfStreet = new TextField(); |
| 20 | + private TextField tfCity = new TextField(); |
| 21 | + private TextField tfState = new TextField(); |
| 22 | + private TextField tfZip = new TextField(); |
| 23 | + |
| 24 | + private Button btAdd = new Button("Add"); |
| 25 | + private Button btFirst = new Button("First"); |
| 26 | + private Button btNext = new Button("Next"); |
| 27 | + private Button btPrevious = new Button("Previous"); |
| 28 | + private Button btLast = new Button("Last"); |
| 29 | + |
| 30 | + private String host = "localhost"; |
| 31 | + private Socket socket; |
| 32 | + private ObjectOutputStream outputToServer; |
| 33 | + private ObjectInputStream inputFromServer; |
| 34 | + |
| 35 | + @Override |
| 36 | + public void start(Stage primaryStage) { |
| 37 | + BorderPane borderPane = new BorderPane(); |
| 38 | + borderPane.setPadding(new Insets(5, 15, 5, 15)); |
| 39 | + |
| 40 | + GridPane pane = new GridPane(); |
| 41 | + pane.setVgap(3); |
| 42 | + pane.setHgap(3); |
| 43 | + pane.setPadding(new Insets(3)); |
| 44 | + pane.add(new Label("Name"), 0, 0); |
| 45 | + pane.add(tfName, 1, 0); |
| 46 | + pane.add(new Label("Street"), 0, 1); |
| 47 | + pane.add(tfStreet, 1, 1); |
| 48 | + pane.add(new Label("City"), 0, 2); |
| 49 | + |
| 50 | + HBox hBox = new HBox(2); |
| 51 | + pane.add(hBox, 1, 2); |
| 52 | + hBox.getChildren().addAll(tfCity, new Label("State"), tfState, new Label("Zip"), tfZip); |
| 53 | + pane.setAlignment(Pos.CENTER); |
| 54 | + borderPane.setCenter(pane); |
| 55 | + |
| 56 | + HBox hBoxForButtons = new HBox(5); |
| 57 | + hBoxForButtons.getChildren().addAll(btAdd, btFirst, btNext, btPrevious, btLast); |
| 58 | + hBoxForButtons.setAlignment(Pos.CENTER); |
| 59 | + borderPane.setBottom(hBoxForButtons); |
| 60 | + |
| 61 | + tfName.setPrefColumnCount(15); |
| 62 | + tfStreet.setPrefColumnCount(15); |
| 63 | + tfCity.setPrefColumnCount(10); |
| 64 | + tfState.setPrefColumnCount(2); |
| 65 | + tfZip.setPrefColumnCount(3); |
| 66 | + |
| 67 | + try { |
| 68 | + socket = new Socket(host, 8000); |
| 69 | + outputToServer = new ObjectOutputStream(socket.getOutputStream()); |
| 70 | + inputFromServer = new ObjectInputStream(socket.getInputStream()); |
| 71 | + } |
| 72 | + catch (IOException ex) { |
| 73 | + ex.printStackTrace(); |
| 74 | + } |
| 75 | + |
| 76 | + btAdd.setOnAction(e -> { |
| 77 | + try { |
| 78 | + StudentAddress address = getDataFromTextFields(); |
| 79 | + outputToServer.writeInt(ADD); |
| 80 | + outputToServer.writeObject(address); |
| 81 | + } |
| 82 | + catch (IOException ex) { |
| 83 | + ex.printStackTrace(); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + setButtonAction(btFirst, FIRST); |
| 88 | + setButtonAction(btNext, NEXT); |
| 89 | + setButtonAction(btPrevious, PREVIOUS); |
| 90 | + setButtonAction(btLast, LAST); |
| 91 | + |
| 92 | + Scene scene = new Scene(borderPane); |
| 93 | + primaryStage.setTitle("Exercise_31_06Client"); |
| 94 | + primaryStage.setScene(scene); |
| 95 | + primaryStage.show(); |
| 96 | + } |
| 97 | + |
| 98 | + private void setButtonAction(Button bt, int request) { |
| 99 | + bt.setOnAction(e -> { |
| 100 | + try { |
| 101 | + outputToServer.writeInt(request); |
| 102 | + setTextFields((StudentAddress)(inputFromServer.readObject())); |
| 103 | + } |
| 104 | + catch (IOException ex) { |
| 105 | + ex.printStackTrace(); |
| 106 | + } |
| 107 | + catch (ClassNotFoundException ex) { |
| 108 | + ex.printStackTrace(); |
| 109 | + } |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + private void setTextFields(StudentAddress data) { |
| 114 | + tfName.setText(data.getName()); |
| 115 | + tfStreet.setText(data.getStreet()); |
| 116 | + tfCity.setText(data.getCity()); |
| 117 | + tfState.setText(data.getState()); |
| 118 | + tfZip.setText(data.getZip()); |
| 119 | + } |
| 120 | + |
| 121 | + private StudentAddress getDataFromTextFields() { |
| 122 | + StudentAddress data = new StudentAddress(tfName.getText(), tfStreet.getText(), |
| 123 | + tfCity.getText(), tfState.getText(), tfZip.getText()); |
| 124 | + return data; |
| 125 | + } |
| 126 | + |
| 127 | + public static void main(String[] args) { |
| 128 | + launch(args); |
| 129 | + } |
| 130 | +} |
0 commit comments