0

I'm using StyledDocument and SimpleAttributeSet for my project but I'm having an UI issue.

Here is what I have with my code right now : enter image description here

And here is what I would like to have : enter image description here

The first thing I'm trying to have is the padding, because the background is too "tight" and so we see a lot of text and not a lot of blue but I don't know how to make the blue surface bigger.

And then, if I can apply a radius it will be perfect but I can't manage to do it neither.

Here is the code you'll need to reproduce what I have on the first image :

 StyledDocument doc = chatBox.getStyledDocument(); 

 SimpleAttributeSet rightO = new SimpleAttributeSet();
 StyleConstants.setBackground(rightO, new Color(59,130,247));
 StyleConstants.setForeground(rightO, new Color(255,255,255)); 
 StyleConstants.setFontSize(rightO, 14);

 SimpleAttributeSet blue = new SimpleAttributeSet(); 
 StyleConstants.setAlignment(blue, StyleConstants.ALIGN_RIGHT);
 StyleConstants.setRightIndent(blue, 10);
 StyleConstants.setLeftIndent(blue, 100);

 try {
 doc.insertString(doc.getLength(), "\n"+message.getContent(), rightO);
 doc.setParagraphAttributes(doc.getLength() -    message.getContent().length(), doc.getLength(), blue, false);
 } catch (BadLocationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
 }
   
Madar
  • 196
  • 2
  • 15
  • A JTextPane is designed to display text and default background painting is based on the size of the text. Maybe its possible to create a custom `Painter` that ignores the bounds of the text. Check out [Rectangle Painter](https://tips4java.wordpress.com/2008/10/28/rectangle-painter/) for a basic example. However, a simpler approach might be to just use a JLabel for your text. Then you can do custom painting of the label and just insert the label into the text pane using the `insertComponent(....)` method of the JTextPane. – camickr Jan 25 '22 at 18:23
  • See: https://stackoverflow.com/a/16909994/131872 for an example of a custom Border similar to what you want. – camickr Jan 25 '22 at 18:23
  • Unfortunately, that doesn't help me at all, I'm working with StyledDocument and not with JTextPane mate.. – Madar Jan 26 '22 at 08:03
  • Not sure what "that" refers to, I gave two suggestions. The insertComponent() method is just a convenience method. All data will ultimately be added to the Document. So if you only have access to the Document then you look at the source code to see what that method does and then replicate the code. See: https://github.com/openjdk/jdk/tree/master/src/java.desktop/share/classes/javax – camickr Jan 26 '22 at 15:21

0 Answers0