Javafx Text Multilingual Coloring

Ok, I have a ListView object. I use this as a kind of console window for my server. It was really the only way to display colored text in such a frame. That it works great so far. Now I want to color another text with a single index or line.

Example:

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

Where " Hello " is green and " world " is blue. If this can be done using Java text or any other way, I would like to know how to do it. I use Javafx Text as the main culprit, since you can customize it with it.

I hope everyone can understand what I'm trying to do here, if not, let me know and I will try to rephrase this a bit.

DECISION

Thanks to Jewelsea, I was able to find a solution. I used a slightly different approach, instead of using a cell factory.

View list

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"

+5
source share
1 answer

Create your own cellfactory for ListView and it generates cells containing FlowPane with different Text , each of which has different styles. To demonstrate this method, I created a sample.

Output Example:

colorized text

In Java 8, you can use TextFlowto style text, rather than a combination of different instances Textin FlowPane.

+9
source

All Articles