title: Javafx Text multi-word colorization
body: <p>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. </p>
  
  <p>Example: </p>
  
  <pre><code>listView[0] = "Hello " + "world";
  </code></pre>
  
  <p>Where "<strong>Hello</strong>" would be green and "<strong>world</strong>" 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.</p>
  
  <p>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.</p>
  
  <p><strong><em>SOLUTION</em></strong></p>
  
  <p>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.</p>
  
  <p><strong><em>ListView</em></strong></p>
  
  <pre><code>ListView&lt;FlowPane&gt; consoleWindow = new ListView&lt;&gt;();
  ArrayList&lt;FlowPane&gt; consoleBuffer = FXCollections.observableArrayList();
  
  consoleWindow.setItems(consoleBuffer);
  
  inputBox.setOnKeyPressed(new EventHandler&lt;KeyEvent&gt;() {
          @Override
          public void handle(KeyEvent keyEvent) {
             if (keyEvent.getCode() == KeyCode.ENTER) {
                  consoleBuffer.add(parseInput.parseInputToArray(inputBox.getText()));
             }
              consoleWindow.scrollTo(consoleBuffer.size());
          }
  });
  </code></pre>
  
  <p><strong><em>ConsoleInputParse</em></strong>:</p>
  
  <pre><code>public class ConsoleInputParse {
  
      private String[] wordList = {};
  
      public ConsoleInputParse() {}
  
      public FlowPane parseInputToArray(String input) {
          wordList = input.trim().split("[ ]+");
  
          return colorize();
      }
  
      public FlowPane colorize() {
  
          ArrayList&lt;Text&gt; textChunks = new ArrayList&lt;&gt;();
          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();
      }
  
  }
  </code></pre>
  
  <p><img src="https://i.imgur.com/02RLmeY.jpg" alt="1"> "Working Example"</p>