Aiman Muzafar Aiman Muzafar - 2 years ago 141
Java Question

Netbeans making a JTextField transparent not working

I've put a background image on a JFrame by putting it as an icon of JLabel. But the problem is my textfield and password fields are not getting transparent. I've disable the opaque in the the properties of both fields. It looks transparent inside the preview of netbeans like this https://imgur.com/Yve4E9x

It has also generated the code https://imgur.com/Q1MTkNl

But still when I run the instance of the code, the fields have their own white background.

https://imgur.com/ICrUCLG

Any help is appreciated. Thanks.

Answer Source

The problem is the background color and the color chooser of the jtextfield. It resets the transparent component of the color.

Solution: Don't use the color chooser. Use the option "Custom code". Then set the color to

new java.awt.Color(255, 255, 255, 0)

(opaque can still be active)

Your initComponents should look like this...

private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBackground(new java.awt.Color(255, 255, 0));

    jPanel1.setBackground(new java.awt.Color(255, 51, 51));

    jLabel1.setText("jLabel1");

    jTextField1.setBackground(new java.awt.Color(255, 255, 255, 0));
    jTextField1.setText("jTextField1");
    [...]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download