// 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); }