How to add an inline image to the end of a string in a TextArea in JavaFX? -
i trying add emoji chat program when client types :)
i trying add in fxml controller. have captured when user types :) using following code snippet :
if(chat.contains(":)")) { ... } my chat printed textarea named tachat
tachat.appendtext(chat + '\n'); any appreciated!
a better approach use textflow instead of using textarea.
advantages :
- individual
texttreated children textflow. can added , accessed individually. imageviewcan added directlytextflowchild.
a simple chat window support 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 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 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 :) 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

for unicode emoji support, please visit how support emojis
Comments
Post a Comment