Search code examples
javaswingtransparencyjscrollpanejtextpane

JTextPane/JScrollPane display issue


I 'm trying to wrap a JTextPane with a JScrollPane, and I need the background to be transparent.

Here's my code:

public CharPanel(){

    super.setLayout(null);

    JButton LButton = new JButton("<");
    LButton.setBounds(0, 300, 50, 50);
    super.add(LButton);

    JButton RButton = new JButton(">");
    RButton.setBounds(950, 300, 50, 50);
    super.add(RButton);

    JTextPane Bio = new JTextPane();
    Bio.setBackground(new Color(0, 0, 0, 0));


    JScrollPane BioScroll = new JScrollPane(Bio);
    BioScroll.setBackground(new Color(0, 0, 0, 0));
    BioScroll.setBounds(0, 500, 1000, 150);
    super.add(BioScroll);


}

This is what it looks like normally: https://gyazo.com/db7e4d2a5668b36ffc617cefb1423fc4

This is what it looks like after I enter a character: https://gyazo.com/1509d952005195d6e14365f0ae2da69a

See the weird visual bug?


Solution

  • Bio.setBackground(new Color(0, 0, 0, 0));
    

    Don't use Colors with an alpha value. This breaks Swing painting rules and Swing don't know how to paint the component properly and you get painting artifacts.

    For full transparency just use:

    component.setOpaque( false );
    

    Check out Backgrounds With Transparency for more information on this and a solution if you need to use partial transparency.