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

46
LINES

< > BotCompany Repo | #1001523 // showColoredText

JavaX fragment (include)

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 showColoredText(L textAndColors) {
5  
  ret showColoredText(textAndColors, "Colored Text");
6  
}
7  
8  
static JTextPane showColoredText(L textAndColors, S title) {
9  
  JFrame jf = new JFrame(title);
10  
  Container cp = jf.getContentPane();
11  
12  
  new JTextPane pane;
13  
  showColoredText_fillPane(pane, textAndColors);
14  
15  
  cp.add(new JScrollPane(pane), BorderLayout.CENTER);
16  
17  
  jf.setSize(500, 600);
18  
  centerFrame(jf);
19  
  jf.setVisible(true);
20  
  ret pane;
21  
}
22  
23  
static void showColoredText_fillPane(JTextPane pane, L textAndColors) {
24  
  pane.setText("");
25  
  Color color = Color.white;
26  
  for (O x : textAndColors) {
27  
    if (x instanceof Color)
28  
      color = (Color) x;
29  
    else
30  
      showColoredText_appendToTextPane(pane, color, String.valueOf(x));
31  
  }
32  
}
33  
34  
static void showColoredText(JTextPane pane, L textAndColors) {
35  
  showColoredText_fillPane(pane, textAndColors);
36  
}
37  
  
38  
static void showColoredText_appendToTextPane(JTextPane pane, Color color, S text) ctex {
39  
  Document doc = pane.getStyledDocument();
40  
  SimpleAttributeSet set = new SimpleAttributeSet();
41  
  StyleConstants.setFontFamily(set, "Courier");
42  
  StyleConstants.setBold(set, true);
43  
  StyleConstants.setFontSize(set, 14);
44  
  StyleConstants.setBackground(set, color);
45  
  doc.insertString(doc.getLength(), text, set);
46  
}

Author comment

Began life as a copy of #1001522

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001523
Snippet name: showColoredText
Eternal ID of this version: #1001523/2
Text MD5: 8e1a6ed89a49a1359f2c07e6e3f64e06
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-05-20 14:59:41
Source code size: 1463 bytes / 46 lines
Pitched / IR pitched: No / No
Views / Downloads: 697 / 933
Version history: 1 change(s)
Referenced in: [show references]