Libraryless. Click here for Pure Java version (3046L/18K).
// textAndColors = a list of Color objects and strings/chars // Note: This functions changes the BACKGROUND colors // (not the text, which remains black). static JTextPane coloredTextToTextPane(L textAndColors, AttributeSet defaultAttributes default null) ctex { var pane = jTextPane(); var doc = pane.getStyledDocument(); Color color = Color.white; for (O x : textAndColors) { if (x instanceof Color) color = (Color) x; else { SimpleAttributeSet set; if (defaultAttributes == null) { set = new SimpleAttributeSet; StyleConstants.setFontFamily(set, "Courier"); StyleConstants.setBold(set, true); StyleConstants.setFontSize(set, 14); } else set = new SimpleAttributeSet(defaultAttributes); StyleConstants.setBackground(set, color); doc.insertString(doc.getLength(), String.valueOf(x), set); } } ret pane; }
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: | 121 / 189 |
Version history: | 3 change(s) |
Referenced in: | [show references] |