Skip to content

Instantly share code, notes, and snippets.

@Dethnull
Created February 26, 2013 09:07
Show Gist options
  • Save Dethnull/5037146 to your computer and use it in GitHub Desktop.
Save Dethnull/5037146 to your computer and use it in GitHub Desktop.
Here is an example answer to my own question on GitHub, http://stackoverflow.com/questions/15081892/javafx-text-multi-word-colorization. This can be extended to do pretty much anything, but it was recommended to use CellFactory to do the same thing. Example of that is here, https://gist.github.com/jewelsea/5036908
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();
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();
}
}
ListView<FlowPane> consoleWindow = new ListView<>();
ArrayList<FlowPane> consoleBuffer = FXCollections.observableArrayList();
consoleWindow.setItems(consoleBuffer);
ConsoleInputParse parseInput = new ConsoleInputParse();
someTextField.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());
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment