TextField for input text and insert images in JavaFX chat application - java

I am developing a chat application on JavaFX and I stuck with a little problem. In short, I want to have a possibility to insert an image(for example smiles) inside TextField which is intended for text input.But as I know, there is no possibility to insert images inside this element. Does JavaFX have some alternatives for that or maybe you know something else, what I can use for this?
Thank you, in advance.

You can try to use HTMLEditor. It supports styled text and other html features, which include adding images.
There is example of using HTMLEditor as inputField for chat:
package org.laptech.sample;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;
public class ChatFieldSample extends Application{
private HTMLEditor htmlEditor;
public void hideHTMLEditorToolbars() {
htmlEditor.setVisible(false);
Platform.runLater(() -> {
Node[] nodes = htmlEditor.lookupAll(".tool-bar").toArray(new Node[0]);
for (Node node : nodes) {
node.setVisible(false);
node.setManaged(false);
}
htmlEditor.setVisible(true);
});
}
#Override public void start(Stage primaryStage) throws Exception {
htmlEditor = new HTMLEditor();
hideHTMLEditorToolbars();
htmlEditor.setHtmlText("Text<img src='urltosmile'/>Text Text Text");
primaryStage.setScene(new Scene(htmlEditor, 300, 300));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I create htmlEditor and hide toolbar in method(hideHTMLEditorToolbars)
The only, which I faced , was the problem of losing focus, when i handle keyEvents and setText there.

Related

How to achive the effect of UNDECORATED + UTILITY in javafx? [duplicate]

I need remove my javafx app from the taskbar. I tried StageStyle.UTILITY. This is works but I need both UNDECORATED and UTILITY stage styles or another solvings.
Thank you for your replies.
Sorry you've been waiting so long for some sort of an answer on this, the following is mainly for people who come to this in the future hoping to discover a way of achieving this.
Let me start of by saying I wouldn't consider the following a solution but more of a workaround.
Assigning more than one initStyle to a stage is not possible however hiding the application from the task-bar and assigning an initStyle other than utility to the stage that is shown is.
To achieve this one must create two stages, the stage they want the user to see, and an another stage that will be considered the parent of the main stage and will be of initStyle.UTILITY this will prevent the icon from showing in the task-bar.
Below you can see the hello world example from oracles documentation modified to allow for an undecorated window with no icon (Note if one wanted to achieve a transparent/decorated window they could do so by changing the style of mainStage).
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class MultipleStageStyles extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.setOpacity(0);
primaryStage.setHeight(0);
primaryStage.setWidth(0);
primaryStage.show();
Stage mainStage = new Stage();
mainStage.initOwner(primaryStage);
mainStage.initStyle(StageStyle.UNDECORATED);
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
mainStage.setScene(new Scene(root, 300, 250));
mainStage.show();
}
}

Why does my JavaFX button have strange symbols instead of text?

I am trying to learn JavaFX and just stumbled across something I'm confused about. I created a new project in IntelliJ and added a single button.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
private Button firstButton;
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
firstButton = new Button("My First Button");
StackPane layout = new StackPane();
layout.getChildren().add(firstButton);
Scene scene = new Scene(layout, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
When I create the button with text it adds symbols instead of the text.
Window with "My First Button" as commanded text.
When I change the text to something small (I was thinking that it was erroring if the text was longer than the button length) it got even stranger. This is the next example I tried:
firstButton = new Button("E");
And the text I got was: Window with "E" as commanded text.
I have Googled a bunch and have not found anyone talking about this. Either I'm a bad Googler or it's not a common problem. My initial thought was that it was an encoding issue. After I saw the mystery E -> G though I'm thinking there might be something else off.
Any ideas what is going on?
Thank you!

javafx - how to create a loop of graphic components

In my javafx application I want to create a scene where I can show the usernames of all the users in my database.
Precisely, I want to show a list of labels where every label get a username.
(the number of labels depend on the number of users).
Note: I can do this in java with a list and a foreach loop, but this is the first time that I work with javafx and I want to know how to create a loop of graphic component.
Thanks.
Here are a couple of alternatives, one sample is just looping and adding new labels to the children of a layout pane, the other is using the in-built ListView component. There are other alternatives of course. Which you choose to use will depend upon the functionality you need to achieve.
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class UserDisplay extends Application {
#Override public void start(Stage stage) {
String[] users = { "Huey", "Dewey", "Louie" };
VBox layout = new VBox(10);
// ALTERNATIVE 1: add labels in a loop.
for (String user: users) {
Label userLabel = new Label(user);
layout.getChildren().add(userLabel);
}
// ALTERNATIVE 2: use the built-in ListView component.
ListView<String> listView = new ListView<>(
FXCollections.observableArrayList(users)
);
layout.getChildren().add(listView);
layout.setPadding(new Insets(10));
layout.setPrefSize(100,200);
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Change the layout on button click [duplicate]

This question already exists:
I Can't Change The Layout
Closed 6 years ago.
I want to change the page when I click the button. I can do it as soon as the program open, but I want to change the center of the Main class' BorderPane when I clicked the button. (I have a number of buttons and I want to go to different controllers with them).
I want to change the center of the Main Controller without changing it. I tried so many things but couldn't do that. On action method addClicked is working but doesn't change the center controller to another controller.
I have Main.class, MainController.java and AddTaskController.java.
Main.class:
package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
public class Main extends Application {
private Stage window;
private BorderPane layout;
#Override
public void start(Stage primaryStage) throws Exception{
window = primaryStage;
try {
MainController con1 = showMainView();
setLayout(con1.setLay("addssss.fxml"));
} catch(Exception e) {
e.printStackTrace();
}
}
public void setLayout(HBox lay){
layout.setCenter(lay);
}
private MainController showMainView() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("MainInterface.fxml"));
layout = loader.load();
Scene scene = new Scene(layout);
window.setScene(scene);
window.show();
return loader.getController();
}
public static void main(String[] args) {
launch(args);
}
}
MainController.java:
package application;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.HBox;
public class MainController {
#FXML
void addClicked(ActionEvent e) throws IOException{
}
public HBox setLay(String kaynak) throws IOException{
HBox layout;
FXMLLoader loader = new FXMLLoader();
loader.setLocation(AddTaskController.class.getResource(kaynak));
layout = loader.load();
return layout;
}
}
And AddTaskController.java (There is no code):
package application;
public class AddTaskController {
}
What exactly is your problem? What have you tried to solve it?
If you want to change the layout of your buttons, I see two possible solutions.
You remove the old layout from your scene (or where ever it was added). Then you add the buttons to a new layout and add the new layout to your scene. (Though I am not sure if this works or not, since the SceneGraph only allows components to be added once).
You have multiple layouts prepared with different buttons and in every layout there is one button calling the "addClicked" method. So there are different Buttons in different Layouts behaving the same way. (You can also prepare a new Layout with new Buttons on the fly, like the first solution does)

Horizontal Scrolling Listener

I feel like I have searched half the Web and found no solution...
I have a java application displaying a Map(different countries etc.).
currently you are able to scroll down and up using your mouse wheel...
I want it so, that you are able to scroll sideways (horizontally).
All I need is a Listener (in Swing or Javafx does not matter) triggering whenever the mousewheel gets tilted, without the need for a focus of the map (hovering with your mouse should be enough the windows should still be focused) and without any visible scrollbars.
Using the following code every time you scroll sideways a message gets printed out...
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
scene.setOnScroll(new EventHandler<ScrollEvent>(){
#Override
public void handle(ScrollEvent event) {
System.out.println("Scroll:" + event.getDeltaX());
}
});
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
One thing to consider:
Apparently when embedding a JFXPanel into a JFrame the sideway scrolling event is not getting passed.

Categories

Resources