Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an inline image to the end of a string in a TextArea in JavaFX?

I am trying to add an emoji to my chat program when my client types :)

I am trying to add this in the FXML controller. I have captured when the user types :) using the following code snippet :

if(chat.contains(":)")) {
    ...
} 

My chat is printed into a textarea named taChat

taChat.appendText(chat + '\n');

Any help is appreciated!

like image 857
Maspe36 Avatar asked Apr 18 '15 04:04

Maspe36


1 Answers

A better approach would be to use TextFlow instead of using TextArea.

Advantages :

  • Individual Text are treated as children to the TextFlow. They can be added and accessed individually.
  • ImageView can be added directly to the TextFlow as a child.

A simple chat window with support for smiley :)

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

public class ChatWindowWithSmiley extends Application {

    public void start(Stage primaryStage) {

        TextFlow textFlow = new TextFlow();
        textFlow.setPadding(new Insets(10));
        textFlow.setLineSpacing(10);
        TextField textField = new TextField();
        Button button = new Button("Send");
        button.setPrefWidth(70);

        VBox container = new VBox();
        container.getChildren().addAll(textFlow, new HBox(textField, button));
        VBox.setVgrow(textFlow, Priority.ALWAYS);

        // Textfield re-sizes according to VBox
        textField.prefWidthProperty().bind(container.widthProperty().subtract(button.prefWidthProperty()));

        // On Enter press
        textField.setOnKeyPressed(e -> {
            if(e.getCode() == KeyCode.ENTER) {
                button.fire();
            }
        });

        button.setOnAction(e -> {
            Text text;
            if(textFlow.getChildren().size()==0){
                text = new Text(textField.getText());
            } else {
                // Add new line if not the first child
                text = new Text("\n" + textField.getText());
            }
            if(textField.getText().contains(":)")) {
                ImageView imageView = new ImageView("http://files.softicons.com/download/web-icons/network-and-security-icons-by-artistsvalley/png/16x16/Regular/Friend%20Smiley.png");
                // Remove :) from text
                text.setText(text.getText().replace(":)"," "));
                textFlow.getChildren().addAll(text, imageView);
            } else {
                textFlow.getChildren().add(text);
            }
            textField.clear();
            textField.requestFocus();
        });

        Scene scene = new Scene(container, 300, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Output

enter image description here

For unicode Emoji support, please visit How to support Emojis

like image 160
ItachiUchiha Avatar answered Nov 01 '22 13:11

ItachiUchiha