Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a JTextField background transparent

I'm trying to make the background for a JTextField transparent, that way so the JLabel underneath it is still visible, but whenever entering text into the JTextField you can see the text. Here's what I have now basically.

The JTextField background is set to black in the below picture. enter image description here

In theory, if the JTextField's background was transparent it should look like this. enter image description here

So, my question is how would I make the JTextField's background transparent?

like image 752
RuneRebellion Avatar asked Mar 27 '14 22:03

RuneRebellion


People also ask

How do I make the background of a Jbutton transparent?

Creating Fully Transparent Buttons Start by creating a simple button in HTML and CSS. Then, change the background color value to transparent. Change the text color on the button so that its still visible under the new background.

How do you make a JLabel transparent?

JLabel is by default transparent and non-opaque, if you want to change background on mouse exit, then you have to: setBackground() for both states, enter and exit.

How do you make a JTextField not editable?

In order to create a non editable JTextField , all you have to do is: Create a class that extends JFrame . Create a new JTextField . Use setEditable(false) that sets the specified boolean to indicate whether or not this textfield should be editable.

Can JTextField be used as an alternative to JLabel?

a) JTextField cannot be used as an alternative to JLabel. b) JLabel cannot be used as an alternative to JTextField. c) Button grouped radio button cannot be used as an alternative to JComboBox. d) The class JPasswordField extends the class JLabel.


1 Answers

This example does simple use setOpaque(false). The labels text is always visible. I tested it with Java 1.7 and 1.8. So if it does not work for you, what else did you do, to initalize your frame?

public class TextField extends javax.swing.JFrame {
    public TextField() {
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

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

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        jLabel1.setText("Test");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(60, 40, 70, 14);

        jTextField1.setText("jTextField1");
        jTextField1.setOpaque(false);
        getContentPane().add(jTextField1);
        jTextField1.setBounds(50, 30, 90, 40);

        pack();
    }// </editor-fold>                        

    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TextField().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                   
}
like image 189
wumpz Avatar answered Sep 19 '22 10:09

wumpz