title: JavaFX, Universal size for TextArea dependent upon content as "Message Bubble"
body: <p>Thank you ahead of time for your time taken.</p>
  
  <p>Currently, I am in the process of creating a JavaFX GUI for a simple-enough client/server application.
  On the right side of a SplitPane is a GridPane, where-by every time a message is sent or received, that Message is displayed within the new ROW in the GridPane, and the message is basically an ImageView(image) followed by a TextArea with a String in it displaying the message sent/received. </p>
  
  <p>My issue is that I cannot figure out after over a week how to size the TextArea appropriately for the block of text within it.
  Before you mark this question as a duplicate, I have tried every implementation I could find. 
  Firstly, the ScrollBar listening solution does not work on runtime, this only appears to work WHILE a user is typing, so I have scratched that as a potential solution for my particular issue. </p>
  
  <p>The solution I'm currently using (which isn't working) is using a Text object and getting the layout bounds/height of THAT for the TextArea. 
  I am fine with my TextAreas (acting as message bubbles) all being the same width, as of now I am specifying the minWidth to be 300.0, the problem again is with the HEIGHT.</p>
  
  <p>My code is as follows: </p>
  
  <pre><code>    HBox messageBox = new HBox(10);
      messageBox.setPadding(new Insets(0, 0, 0, 25));
  
      TextArea textArea = new TextArea();
      textArea.setText(message);
      textArea.setFont(new Font(20));
      textArea.setWrapText(true);
      final Text helper = new Text();
      helper.setText(message);
      helper.setFont(textArea.getFont());
      helper.setWrappingWidth(300.0);
      double width = helper.getLayoutBounds().getWidth();
      double height = helper.getLayoutBounds().getHeight(); 
  
      textArea.setMinWidth(width);
      textArea.setPrefHeight(height);
  
      messageBox.getChildren().addAll(imageView, textArea);
      messagePane.add(messageBox, 0, rowCount);
      rowCount++;
  </code></pre>
  
  <p>Please note that I have also tried placing my helper Text object into a throw-away Pane, which renders almost identical results.
  Lastly, I have tried adding padding to the setPrefHeight() of the TextArea, I have tried MinHeight/MaxHeight combinations.</p>
  
  <p><a href="https://i.stack.imgur.com/XV3Bu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XV3Bu.png" alt="enter image description here"></a>
  This picture illustrates my FIRST problem, the 3rd message has far too much space below the end of the block of text, while preceding message look fine, (IMO). The second picture BELOW demonstrated my 2nd problem, larger blocks of text seem to gradually decrease the width of the TextAreas or perhaps the HBox's above them. Before these subsequent HBox's were, added, the highlighted TextArea had enough space, for instance. </p>
  
  <p><a href="https://i.stack.imgur.com/gKmTd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gKmTd.png" alt="Tgggggggg"></a></p>
  
  <p>Is there any solution that will work for my needs? 
  I would be very grateful, thank you for your time! </p>
  
  <p>Keith </p>