Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javafx Text multi-word colorization

Ok, I have a ListView object. I'm using this as a sort of console window for my server. It was really the only way I could think of to display colorized text in a box like that. Which this works wonderful so far. Now what I want to be able to do is color different text on one index or line.

Example:

listView[0] = "Hello " + "world";

Where "Hello" would be green and "world" would be blue. If this can be done usine javafx Text or any other way I would like to know how to go about it. I'm using Javafx Text as the primary culprit since you can customize so much with it.

I hope everyone can understand what I'm trying to do here, if not, let me know and I'll try to reword it a bit.

SOLUTION

Thanks to jewelsea I was able to figure out a solution. I went with a bit of a different approach with it instead of using a cellfactory.

ListView

ListView<FlowPane> consoleWindow = new ListView<>();
ArrayList<FlowPane> consoleBuffer = FXCollections.observableArrayList();

consoleWindow.setItems(consoleBuffer);

inputBox.setOnKeyPressed(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent keyEvent) {
           if (keyEvent.getCode() == KeyCode.ENTER) {
                consoleBuffer.add(parseInput.parseInputToArray(inputBox.getText()));
           }
            consoleWindow.scrollTo(consoleBuffer.size());
        }
});

ConsoleInputParse:

public class ConsoleInputParse {

    private String[] wordList = {};

    public ConsoleInputParse() {}

    public FlowPane parseInputToArray(String input) {
        wordList = input.trim().split("[ ]+");

        return colorize();
    }

    public FlowPane colorize() {

        ArrayList<Text> textChunks = new ArrayList<>();
        FlowPane bundle = new FlowPane();

        //Todo: use regex to check for valid words
        for (String word : wordList) {
            String spaced = word + " ";
            switch (word) {
                case "Hello": case "hello":
                    textChunks.add(customize(spaced, "purple"));
                    break;
                case "World": case "world":
                    textChunks.add(customize(spaced, "blue"));
                    break;
                case "Stack Overflow":
                    textChunks.add(customize(spaced, "orange", "Arial Bold", 15));
                default:
                    textChunks.add(customize(spaced, "black", "Arial",  13));
                    break;
            }
        }

        bundle.getChildren().addAll(textChunks);
        return bundle;
    }

    public Text customize(String word, String color) {
        return TextBuilder.create().text(word).fill(Paint.valueOf(color)).build();
    }

    public Text customize(String word, String color, String font) {
        return TextBuilder.create()
                .text(word)
                .fill(Paint.valueOf(color))
                .font(Font.font(font, 12)).build();
    }

    public Text customize(String word, String color, String font, int fontSize) {
        return TextBuilder.create()
                .text(word)
                .fill(Paint.valueOf(color))
                .font(Font.font(font, fontSize)).build();
    }

}

1 "Working Example"

like image 316
DerekE Avatar asked Feb 17 '23 09:02

DerekE


1 Answers

Create a custom cellfactory for your ListView and have it generate cells containing a FlowPane with different Text instances, each with different styles. I created a sample to demonstrate this method.

Sample output:

colorized text

In Java 8 you can you can use the TextFlow to style your text rather than a combination of different Text instances in a FlowPane.

Sample code

fruits.css

/** 
 * fruits.css - place in same source directory as FruitsDisplay.java and 
 * ensure the build system copies the file over to the output path
 */
.root {
  -fx-font-size: 20px;
  -fx-font-family: "Comic Sans MS";
}

.list-cell {
  -fx-background-color: azure;
}

.fruit {
  -fx-font-weight: bold;
  -fx-font-style: italic;
}

.apple {
  -fx-fill: forestgreen;
}

.orange {
  -fx-fill: orange;
}

.pear {
  -fx-fill: gold;
}

FruitsDisplay.java

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.util.*;

/**
 * Sample of using a FlowPane to create styled text in JavaFX
 */
public class FruitsDisplay extends Application {
    private static final String[] fruits = {"apple", "orange", "pear"};
    private static final String[] fruitImageLocs = {
            "http://weknowyourdreamz.com/images/apple/apple-02.jpg",
            "http://pic.1fotonin.com/data/wallpapers/165/WDF_2048871.png",
            "http://vignette1.wikia.nocookie.net/pikmin/images/c/cc/Pear-01.jpg"
    };
    private Map<String, Image> fruitImages = new HashMap<>();

    public static void main(String[] args) {
        Application.launch(FruitsDisplay.class);
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("Fruit Tales");

        for (int i = 0; i < fruits.length; i++) {
            Image image = new Image(fruitImageLocs[i], 0, 30, true, true);
            fruitImages.put(fruits[i], image);
        }

        ListView<String> list = new ListView<>(FXCollections.observableArrayList(fruits));
        list.setCellFactory(listView -> new FruitFlowCell());
        list.setPrefSize(440, 180);

        Scene scene = new Scene(list);
        scene.getStylesheets().add(getResource("fruits.css"));
        stage.setScene(scene);
        stage.show();
    }

    private String getResource(String resourceName) {
        return getClass().getResource(resourceName).toExternalForm();
    }

    private class FruitFlowCell extends ListCell<String> {
        static final String FRUIT_PLACEHOLDER = "%f";

        {
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        }

        @Override
        protected void updateItem(String s, boolean empty) {
            super.updateItem(s, empty);
            if (s != null && !"".equals(s) && !isEmpty()) {
                setGraphic(createFruitFlow(s));
            } else {
                setGraphic(null);
            }
        }

        private Node createFruitFlow(String s) {
            switch (s) {
                case "apple":
                    return createTextFlow("Eat an ", FRUIT_PLACEHOLDER, s, " a day.");
                case "orange":
                    return createTextFlow("An ", FRUIT_PLACEHOLDER, s, " has many vitamins.");
                case "pear":
                    return createTextFlow("A ", FRUIT_PLACEHOLDER, s, " has a funny shape.");
                default:
                    return null;
            }
        }

        private Node createTextFlow(String... msg) {
            FlowPane flow = new FlowPane();
            boolean isFruit = false;

            for (String s : msg) {
                if (FRUIT_PLACEHOLDER.equals(s)) {
                    isFruit = true;
                    continue;
                }

                Text text = new Text(s);
                if (isFruit) {
                    flow.getChildren().addAll(
                            new ImageView(fruitImages.get(s)),
                            createSpacer(5)
                    );
                    text.getStyleClass().addAll(
                            "fruit",
                            s
                    );
                    isFruit = false;
                } else {
                    text.getStyleClass().add("plain");
                }

                flow.getChildren().add(text);
            }

            return flow;
        }

        private Node createSpacer(int width) {
            HBox spacer = new HBox();
            spacer.setMinWidth(HBox.USE_PREF_SIZE);
            spacer.setPrefWidth(width);
            spacer.setMaxWidth(HBox.USE_PREF_SIZE);

            return spacer;
        }
    }
}
like image 159
jewelsea Avatar answered Feb 23 '23 22:02

jewelsea