Search code examples
javaswingfontscolorsjtextpane

Text Pane color error


For some reason my text pane is white. It's a text pane (output) nested inside a j scrollpane.

        jScrollPane1.setBackground(new java.awt.Color(0, 0, 0));
        jScrollPane1.setBorder(null);
        jScrollPane1.setOpaque(false);

        output.setEditable(false);
        output.setBackground(new java.awt.Color(0, 0, 0));
        output.setBorder(null);
        output.setCaretColor(new java.awt.Color(255, 255, 255));
        output.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        output.setHighlighter(null);
        output.setOpaque(false);
        jScrollPane1.setViewportView(output);

enter image description here

That's the only code affecting it. I don't know why this is happening, but I want the text pane to be black.


Solution

  • First of all, setting the background color of the JTextPane should be more than enough

    Back in back

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class BlackTextPane {
    
        public static void main(String[] args) {
            new BlackTextPane();
        }
    
        public BlackTextPane() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JTextPane tp = new JTextPane();
                    tp.setForeground(Color.WHITE);
                    tp.setBackground(Color.BLACK);
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new JScrollPane(tp));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
    }
    

    How ever, you seem to making it transparent for some reason, output.setOpaque(false);. Now you've made the JScrollPane transparent as well, which is good, but you forgot to make the view port transparent jScrollPane1.getViewport().setOpaque(false);

    Scroll panes are made up three components, the JScrollPane itself, the JViewport which determines what gets displayed and you component (the view)

    ScrollPane

    Take a closer look at How to Use Scroll Panes for more details