0

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
CrimsonK9
  • 31
  • 1
  • 6
  • 1
    Please follow Java naming conventions, variable/attributes/method must start with lowercase letter – azro Jun 21 '18 at 12:34
  • Oh, thanks for the help! – CrimsonK9 Jun 21 '18 at 12:34
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 3) **Swing does not handle transparent components well.** – Andrew Thompson Jun 21 '18 at 12:56

1 Answers1

1
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.

camickr
  • 321,443
  • 19
  • 166
  • 288