Search code examples
javaswingjscrollpanejtextareajscrollbar

How to set JScrollPane Background transparent and JScrollBarr Visible with JTextArea?


This is what I want to achieve:

https://i.sstatic.net/g7pOE.png

What I tried:

  1. jTextArea.setOpaque(false); this makes JTextArea transparent.
  2. jScrollPane.setOpaque(false); this gives no effect
  3. then I tried this which hides both JScrollPane and JTextArea.

    jScrollPane.getViewPort().setOpaque(false); 
    jScrollPane.setOpaque(false);
    
  4. then I tried this which hides both JScrollPane and JTextArea.

    jScrollPane.setViewPort(new MyViewPort());
    class MyViewPort() extends JViewPort{
        public MyViewPort(){
            setOpaque(false);
        }
    }
    

What I want to achieve is JScrollPane background transparent and transparent JTextArea where I should able to add text and visible JScrollPane.

Update: I did like this I can add text in textArea but the jscrollPane is not transparent:

public class TransparentBackground extends javax.swing.JFrame {

    public TransparentBackground() {
        jScrollPane = new javax.swing.JScrollPane();
        jTextArea = new javax.swing.JTextArea();
        lblBackground = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTextArea.setColumns(20);
        jTextArea.setRows(5);
        jScrollPane.setViewportView(jTextArea);
        jScrollPane.getViewport().setOpaque(false);
        jScrollPane.setOpaque(false);
        jTextArea.setOpaque(false);

        getContentPane().add(jScrollPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 40, 580, 300));

        lblBackground.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bg.png"))); // NOI18N
        getContentPane().add(lblBackground, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 680, 390));

        pack();
    }

And When I use

jScrollPane.setViewPort(new MyViewPort());
class MyViewPort() extends JViewPort{
    public MyViewPort(){
        setOpaque(false);
    }
}

Both textArea and JScrollPane disappears(transparent) but need jTextArea transparent and editable or can add text in it.

When I set custom view port result is like this


Solution

  • I don't know whats wrong with privious code, may be due to the use of drag and drop. Here is code that worked. Thank you Camickr and MadProgrammer for you suggestion. :)

    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import javax.swing.*;
    
    public class TransparentBackground1 extends JFrame {
        private javax.swing.JScrollPane jScrollPane;
        private javax.swing.JTextArea jTextArea;
        private javax.swing.JLabel lblBackground;
    
        public TransparentBackground1() {
            setPreferredSize(new Dimension(675, 375));
            jScrollPane = new JScrollPane();
            jTextArea = new JTextArea();
            lblBackground = new JLabel();
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            getContentPane().setLayout(new FlowLayout());
    
            jTextArea.setColumns(20);
            jTextArea.setRows(5);
            jScrollPane.setViewportView(jTextArea);
    
            //Code To make transparent
            jScrollPane.getViewport().setOpaque(false);
            jScrollPane.setOpaque(false);
            jTextArea.setOpaque(false);
    
    
            lblBackground.setIcon(new ImageIcon(getClass().getResource("/bg.png"))); // NOI18N
    
            pack();
        }
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TransparentBackground().setVisible(true);
                }
            });
        }
    }
    

    Here is Output