Search code examples
javaswingtransparencyjtextpane

Remove JTextPane's white background without setOpaque() over a translucent JFrame


I have a Java code where I implemented a translucent JPanel with an image drawn on it with Graphics 2D. This image is a PNG that includes a white rectangle, 80% opaque, all over the JFrame. Now I need to add a JTextPane to display datas (I set it to use a custom font from the app package BTW), but I can't manage to make it translucent : its white background is opaque (even with the textPane.setOpaque(false); setting) and makes the transparency of my JFrame a bit useless... Wich is not cool.

So I am looking for a way to remove this white background that freaks me out.

I rolled many Google searches but everything I found was a boolean to set the opacity of the JTextPane. I also found that with the Graphics 2D I could create a custom JTextPane and Override its background, but It didn't work... I HAVE already tried all of this.

public class MyWindow extends JFrame {

    private static class MyTextPane extends JTextPane {
         public MyTextPane() {
            super();

            setText("Hello World");
            setOpaque(false);
            setBackground(new Color(0,0,0,0));
         }

         @Override
         protected void paintComponent(Graphics g) {
            g.setColor(new Color(0, 0, 0, 0));
            g.fillRect(0, 0, getWidth(), getHeight());

            super.paintComponent(g);
         }
    }

    public static void main(String[] args) {

         MyTextPane textPane = new MyTextPane();

         JPanel panel = new JPanel() {

             private static final long serialVersionUID = 1L;

             @Override
                         protected void paintComponent(Graphics g) {
                              try {
                Image img = ImageIO.read(new File("images/bg.png"));
                g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
                              } catch (IOException e) {
                e.printStackTrace();
              }

                              if (g instanceof Graphics2D) {
                                final int R = 240;
                                final int G = 240;
                                final int B = 240;

                Paint p =
                    new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0),
                        0.0f, getHeight(), new Color(R, G, B, 0), true);
                Graphics2D g2d = (Graphics2D)g;
                g2d.setPaint(p);
                g2d.fillRect(0, 0, getWidth(), getHeight());
            }
        }
    };
    panel.add(textPane);
    setContentPane(panel);

    }

(Made the JFrame translucent with the explanations of Oracle, right here HERE) Thanks!


Solution

  • This is how I might approach this kind of idea...

    enter image description here

    public class OverlayTextArea {
    
        public static void main(String[] args) {
            new OverlayTextArea();
        }
    
        public OverlayTextArea() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setContentPane(new ImagePane());
                    frame.setLayout(new BorderLayout());
                    frame.add(new TransparentTextArea());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TransparentTextArea extends JTextArea {
    
            public TransparentTextArea() {
                setOpaque(false);
                setBorder(new CompoundBorder(new EmptyBorder(10, 10, 10, 10), new LineBorder(Color.LIGHT_GRAY)));
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                g.setColor(new Color(255, 255, 255, 128));
                Insets insets = getInsets();
                int x = insets.left;
                int y = insets.top;
                int width = getWidth() - (insets.left + insets.right);
                int height = getHeight() - (insets.top + insets.bottom);
                g.fillRect(x, y, width, height);
                super.paintComponent(g);
            }
    
        }
    
        public class ImagePane extends JPanel {
    
            private BufferedImage background;
    
            public ImagePane() {
                try {
                    background = ImageIO.read(new File("/path/to/background.img"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    
            @Override
            public Dimension getPreferredSize() {
                return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (background != null) {
                    int x = (getWidth() - background.getWidth()) / 2;
                    int y = (getHeight() - background.getHeight()) / 2;
                    g.drawImage(background, x, y, this);
                }
            }
    
        }
    
    }
    

    Feedback

    • You really should call super.paintComponent, failing to do so can lead it some serious trouble, especially when you're dealing with transparent components.
    • Don't perform any long running tasks within the paintXxx methods, like loading images. These methods are intended to return quickly and may be called multiple times within succession...