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

46
LINES

< > BotCompany Repo | #1001523 // showColoredText

JavaX fragment (include)

// textAndColors = a list of Color objects and strings/chars
// Note: This functions changes the BACKGROUND colors
// (not the text, which remains black).
static JTextPane showColoredText(L textAndColors) {
  ret showColoredText(textAndColors, "Colored Text");
}

static JTextPane showColoredText(L textAndColors, S title) {
  JFrame jf = new JFrame(title);
  Container cp = jf.getContentPane();

  new JTextPane pane;
  showColoredText_fillPane(pane, textAndColors);

  cp.add(new JScrollPane(pane), BorderLayout.CENTER);

  jf.setSize(500, 600);
  centerFrame(jf);
  jf.setVisible(true);
  ret pane;
}

static void showColoredText_fillPane(JTextPane pane, L textAndColors) {
  pane.setText("");
  Color color = Color.white;
  for (O x : textAndColors) {
    if (x instanceof Color)
      color = (Color) x;
    else
      showColoredText_appendToTextPane(pane, color, String.valueOf(x));
  }
}

static void showColoredText(JTextPane pane, L textAndColors) {
  showColoredText_fillPane(pane, textAndColors);
}
  
static void showColoredText_appendToTextPane(JTextPane pane, Color color, S text) ctex {
  Document doc = pane.getStyledDocument();
  SimpleAttributeSet set = new SimpleAttributeSet();
  StyleConstants.setFontFamily(set, "Courier");
  StyleConstants.setBold(set, true);
  StyleConstants.setFontSize(set, 14);
  StyleConstants.setBackground(set, color);
  doc.insertString(doc.getLength(), text, set);
}

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: 638 / 869
Version history: 1 change(s)
Referenced in: [show references]