Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.image.ImageView;
- import javafx.scene.layout.BorderPane;
- import javafx.stage.Stage;
- import javafx.scene.layout.VBox;
- import javafx.scene.layout.HBox;
- import javafx.geometry.Insets;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextArea;
- public class JavaChatClientGUI extends Application {
- /*
- TODO - Needs a text area for chat transcripts
- TODO - Should have vertical scroll bars, wrap at line breaks
- TODO - Second text area for message inputs
- TODO - Button to send disconnect message onClick
- TODO - Send Button
- TODO -
- */
- private Stage window;
- public final int WINDOW_HEIGHT = 800;
- public final int WINDOW_WIDTH = 1100;
- public final int CHAT_HEIGHT = 500;
- public final int CHAT_WIDTH = 1000;
- public final int INPUT_HEIGHT = 200;
- public final int INPUT_WIDTH = 1000;
- public JavaChatClient jcc;
- private TextArea messageHistory;
- private TextArea messageInput;
- private Button submitInputButton;
- private Button closeChatButton;
- private BorderPane borderPane = new BorderPane();
- // public JavaChatClientGUI(JavaChatClient theClient){
- // this.jcc = theClient;
- // }
- private void closeProgram(){
- boolean ans = ConfirmBox.display("Close Window Confirm", "Are you sure you want to close?");
- if(ans){
- System.out.println("Closed properly");
- window.close();
- }
- else{
- System.out.println("No selected.");
- // window.close();
- }
- }
- private void submitMessage(){
- //Get input of text area
- //Send message on server
- //Server needs to update the program
- messageInput.setText("New Text");
- }
- private void newMessageFromClient(String message){
- //Read message in
- //Append message to text chat
- String currentText = messageHistory.getText();
- currentText += message;
- messageHistory.setText(currentText);
- }
- @Override
- public void start(Stage stage) throws Exception{
- this.window = stage;
- // TODO - Figure out proper instantiation
- // JavaChatClient jcc = new JavaChatClient();
- //jcc.sendMessage();
- // Send close requests to close method
- window.setOnCloseRequest(e -> {
- // Consume close event manually
- e.consume();
- closeProgram();
- }
- );
- //Start of JordansGUI
- window = stage;
- window.setTitle("Java Chat Client");
- //Root Vbox
- VBox root = new VBox();
- root.setPadding(new Insets(12));
- root.setSpacing(6);
- //Chat Label (Something likee: "Chatting as: <usernameE> on Port: <port #>")
- String chatLabelString = "jcc.chatLabelString goes here";
- Label chatLabel = new Label(chatLabelString);
- //Create TextArea with default message..
- TextArea messageHistory = new TextArea();
- messageHistory.setStyle("-fx-highlight-fill: lightgray; -fx-highlight-text-fill: firebrick; -fx-font-size: 12px;");
- messageHistory.setText("Welcome to the chat!\nSubmit your messages by pressing enter or clicking Submit");
- messageHistory.setPrefHeight(CHAT_HEIGHT);
- messageHistory.setPrefWidth(CHAT_WIDTH);
- //HBOX to hold the message input and button
- HBox inputBar = new HBox();
- //Input Message Text Area
- TextArea messageInput = new TextArea();
- messageInput.setPrefHeight(INPUT_HEIGHT);
- messageInput.setPrefWidth(INPUT_WIDTH});
- messageInput.setStyle("-fx-highlight-fill: lightgray; -fx-highlight-text-fill: firebrick; -fx-font-size: 12px;");
- //ToDo: Add handler that listens for Enter Preess and calls to submit the emeesesage
- //Submit input button
- Button submitInputButton = new Button("Submit");
- submitInputButton.setOnAction(new EventHandler<ActionEvent>(){
- public void handle(ActionEvent event){
- System.out.println("Message submitted by pressing enter");
- submitMessage();
- }
- });
- Button endChat = new Button("Leave Chat");
- endChat.setOnAction(new EventHandler<ActionEvent>(){
- public void handle(ActionEvent event){
- System.out.println("Program ended by pressing Leave Chat");
- closeProgram();
- }
- });
- //Add actions to the to close chat button
- // closeChatButton.setOnAction(action -> {
- // System.out.println("Close chat request");
- // closeProgram();
- // });
- //Adding input text area and submit input button to the HBox
- inputBar.getChildren().addAll(messageInput, submitInputButton);
- //Add all other components to the root VBox
- root.getChildren().addAll(chatLabel, messageHistory, inputBar, endChat);
- Scene scene = new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT);
- window.setScene(scene);
- window.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement