Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.30 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.event.ActionEvent;
  3. import javafx.event.EventHandler;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.image.ImageView;
  7. import javafx.scene.layout.BorderPane;
  8. import javafx.stage.Stage;
  9. import javafx.scene.layout.VBox;
  10. import javafx.scene.layout.HBox;
  11. import javafx.geometry.Insets;
  12. import javafx.scene.control.Label;
  13. import javafx.scene.control.TextArea;
  14.  
  15. public class JavaChatClientGUI extends Application {
  16.     /*
  17.     TODO - Needs a text area for chat transcripts
  18.     TODO - Should have vertical scroll bars, wrap at line breaks
  19.     TODO - Second text area for message inputs
  20.     TODO - Button to send disconnect message onClick
  21.     TODO - Send Button
  22.     TODO -
  23.      */
  24.  
  25.  
  26.     private Stage window;
  27.  
  28.     public final int WINDOW_HEIGHT = 800;
  29.     public final int WINDOW_WIDTH = 1100;
  30.  
  31.     public final int CHAT_HEIGHT = 500;
  32.     public final int CHAT_WIDTH = 1000;
  33.  
  34.     public final int INPUT_HEIGHT = 200;
  35.     public final int INPUT_WIDTH = 1000;
  36.    
  37.     public JavaChatClient jcc;
  38.     private TextArea messageHistory;
  39.     private TextArea messageInput;
  40.     private Button submitInputButton;
  41.     private Button closeChatButton;
  42.  
  43.     private BorderPane borderPane = new BorderPane();
  44.  
  45. //    public JavaChatClientGUI(JavaChatClient theClient){
  46. //        this.jcc = theClient;
  47. //    }
  48.  
  49.     private void closeProgram(){
  50.         boolean ans = ConfirmBox.display("Close Window Confirm", "Are you sure you want to close?");
  51.         if(ans){
  52.             System.out.println("Closed properly");
  53.             window.close();
  54.         }
  55.         else{
  56.             System.out.println("No selected.");
  57.             // window.close();
  58.         }
  59.     }
  60.  
  61.     private void submitMessage(){
  62.         //Get input of text area
  63.         //Send message on server
  64.         //Server needs to update the program
  65.         messageInput.setText("New Text");
  66.     }
  67.  
  68.     private void newMessageFromClient(String message){
  69.         //Read message in
  70.         //Append message to text chat
  71.         String currentText = messageHistory.getText();
  72.         currentText += message;
  73.         messageHistory.setText(currentText);
  74.     }
  75.  
  76.     @Override
  77.     public void start(Stage stage) throws Exception{
  78.         this.window = stage;
  79.  
  80.         // TODO - Figure out proper instantiation
  81.         // JavaChatClient jcc = new JavaChatClient();
  82.  
  83.         //jcc.sendMessage();
  84.  
  85.         // Send close requests to close method
  86.         window.setOnCloseRequest(e -> {
  87.             // Consume close event manually
  88.             e.consume();
  89.             closeProgram();
  90.         }
  91.         );
  92.  
  93.         //Start of JordansGUI
  94.  
  95.         window = stage;
  96.         window.setTitle("Java Chat Client");
  97.  
  98.         //Root Vbox
  99.         VBox root = new VBox();
  100.         root.setPadding(new Insets(12));
  101.         root.setSpacing(6);
  102.  
  103.         //Chat Label (Something likee: "Chatting as: <usernameE> on Port: <port #>")
  104.         String chatLabelString = "jcc.chatLabelString goes here";
  105.         Label chatLabel = new Label(chatLabelString);
  106.  
  107.         //Create TextArea with default message..
  108.         TextArea messageHistory = new TextArea();
  109.         messageHistory.setStyle("-fx-highlight-fill: lightgray; -fx-highlight-text-fill: firebrick; -fx-font-size: 12px;");
  110.         messageHistory.setText("Welcome to the chat!\nSubmit your messages by pressing enter or clicking Submit");
  111.         messageHistory.setPrefHeight(CHAT_HEIGHT);  
  112.         messageHistory.setPrefWidth(CHAT_WIDTH);  
  113.  
  114.         //HBOX to hold the message input and button
  115.         HBox inputBar = new HBox();
  116.  
  117.         //Input Message Text Area
  118.         TextArea messageInput = new TextArea();
  119.         messageInput.setPrefHeight(INPUT_HEIGHT);  
  120.         messageInput.setPrefWidth(INPUT_WIDTH});  
  121.         messageInput.setStyle("-fx-highlight-fill: lightgray; -fx-highlight-text-fill: firebrick; -fx-font-size: 12px;");
  122.         //ToDo: Add handler that listens for Enter Preess and calls to submit the emeesesage
  123.  
  124.         //Submit input button
  125.         Button submitInputButton = new Button("Submit");
  126.         submitInputButton.setOnAction(new EventHandler<ActionEvent>(){
  127.  
  128.             public void handle(ActionEvent event){
  129.                 System.out.println("Message submitted by pressing enter");
  130.                 submitMessage();
  131.             }
  132.         });
  133.  
  134.         Button endChat = new Button("Leave Chat");
  135.         endChat.setOnAction(new EventHandler<ActionEvent>(){
  136.             public void handle(ActionEvent event){
  137.                 System.out.println("Program ended by pressing Leave Chat");
  138.                 closeProgram();
  139.             }
  140.         });
  141.  
  142.         //Add actions to the to close chat button
  143. //        closeChatButton.setOnAction(action -> {
  144. //            System.out.println("Close chat request");
  145. //            closeProgram();
  146. //        });
  147.  
  148.         //Adding input text area and submit input button to the HBox
  149.         inputBar.getChildren().addAll(messageInput, submitInputButton);
  150.  
  151.         //Add all other components to the root VBox
  152.         root.getChildren().addAll(chatLabel, messageHistory, inputBar, endChat);
  153.  
  154.  
  155.  
  156.         Scene scene = new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT);
  157.         window.setScene(scene);
  158.         window.show();
  159.  
  160.  
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement