Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

26
LINES

< > BotCompany Repo | #1031205 // coloredTextToTextPane

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3046L/18K).

1  
// textAndColors = a list of Color objects and strings/chars
2  
// Note: This functions changes the BACKGROUND colors
3  
// (not the text, which remains black).
4  
static JTextPane coloredTextToTextPane(L textAndColors, AttributeSet defaultAttributes default null) ctex {
5  
  var pane = jTextPane();
6  
  var doc = pane.getStyledDocument();
7  
  Color color = Color.white;
8  
  for (O x : textAndColors) {
9  
    if (x instanceof Color)
10  
      color = (Color) x;
11  
    else {
12  
      SimpleAttributeSet set;
13  
      if (defaultAttributes == null) {
14  
        set = new SimpleAttributeSet;
15  
        StyleConstants.setFontFamily(set, "Courier");
16  
        StyleConstants.setBold(set, true);
17  
        StyleConstants.setFontSize(set, 14);
18  
      } else
19  
        set = new SimpleAttributeSet(defaultAttributes);
20  
        
21  
      StyleConstants.setBackground(set, color);
22  
      doc.insertString(doc.getLength(), String.valueOf(x), set);
23  
    }
24  
  }
25  
  ret pane;
26  
}

Author comment

Began life as a copy of #1001523

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031205
Snippet name: coloredTextToTextPane
Eternal ID of this version: #1031205/4
Text MD5: b3dc0f247a2769176aefc40d4b01ae2b
Transpilation MD5: cef9a304307c59947243af532593b635
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-05-20 15:23:27
Source code size: 939 bytes / 26 lines
Pitched / IR pitched: No / No
Views / Downloads: 123 / 193
Version history: 3 change(s)
Referenced in: [show references]