Вопрос

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?

Это было полезно?

Решение

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                   
}

Другие советы

Change look and feel setting to metal or window It will work if u have set it transparent already Nimbus laf makes textareas backgroung visible as soon as the program runs

It looks like Nimbus does not support transparent background in JTextField, JTextArea etc. Instead of making the background transparent, here is code to make the component set its background color to match its parent's background:

private static final HierarchyListener _hl = new HierarchyListener() {

    @Override
    public void hierarchyChanged(HierarchyEvent e) {
        Component c = e.getComponent();
        for (Component p = c; p != null; p = p.getParent()) {
            if (p.isOpaque()) {
                int bk = p.getBackground().getRGB();
                c.setBackground(new Color(bk));
                break;
            }
        }

    }
};

public static void makeComponentCopyParentBackground(Component c) {
    c.removeHierarchyListener(_hl); // Guard against client calling multiple times.
    c.addHierarchyListener(_hl);
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top