Transparent background in java

3

I have a problem with background transparent of a JTextField , changing the content of JTextField it seems to overlap the text and everything is mixed:

I'msettingthebackgroundlikethis:

Field.setBackground(newColor(1.0f,1.0f,1.0f,0f));

//EDIT

Iwasabletodothisusingthe@utluizhintbutthenumbersareleftwithsomewhitepixelsaround:

Can you make the background completely transparent too?

    
asked by anonymous 18.02.2014 / 19:35

2 answers

2

I'm not very good with GUI, but I was able to reproduce the problem and solve with the repaint() method in the "parent" component.

Consider the following example:

public class TextFieldTest {

    public static void main(String[] args) {

        //janela
        final JFrame janela = new JFrame("Test JTextField");

        //campo de texto
        final JTextField textField = new JTextField("Texto de Teste");
        textField.setBackground(new Color(1.0f,1.0f,1.0f,0f));
        janela.getContentPane().add(textField);

        //listener de mudança
        textField.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void removeUpdate(DocumentEvent arg0) {
                janela.repaint();
            }

            @Override
            public void insertUpdate(DocumentEvent arg0) {
                janela.repaint();
            }

            @Override
            public void changedUpdate(DocumentEvent arg0) {
                janela.repaint();
            }
        });

        //exibe a janela
        janela.setSize(200,  200);
        janela.setVisible(true);

    }

}

Without the listener , the same code presents the problem described in the question.

    
18.02.2014 / 19:48
2

I do this:

package teste;

import java.awt.Color; 
import java.awt.Container; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JRootPane; 
import javax.swing.JTextField;

public class Teste extends JFrame {

    private JButton fechar, mostrar;
    private JTextField campo;
    private boolean x;

    public Teste() {
        final Container tela = getContentPane();
        tela.setLayout(null);
        this.setResizable(false);
        setUndecorated(true);
        setBackground(new Color(0f, 0f, 0f, 0f));
        this.setTitle("Teste");
        this.setBounds(0, 0, 400, 400);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.getContentPane().setLayout(null);
        this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
        this.setLocationRelativeTo(null);

        fechar = new JButton();
        fechar.setBounds(0, 0, 200, 200);
        fechar.setText("Fechar");
        fechar.setFont(new Font("Arial", Font.BOLD, 12));
        fechar.setBackground(new Color(190, 190, 190));
        this.add(fechar);

        mostrar = new JButton();
        mostrar.setBounds(200, 0, 200, 200);
        mostrar.setText("Ocultar");
        mostrar.setFont(new Font("Arial", Font.BOLD, 12));
        mostrar.setBackground(new Color(190, 190, 190));
        this.add(mostrar);

        campo = new JTextField("Campo de texto");
        campo.setBounds(150, 250, 120, 20);
        this.add(campo);

        mostrar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mostrar();
            }
        });
        fechar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fechar();
            }
        });

    }

    public void fechar() {
        System.exit(0);
    }

    public void mostrar() {

        if (!x) {
            setBackground(new Color(0f, 0f, 0f, 1f));
            mostrar.setText("Ocultar");
            x = true;
        } else {
            setBackground(new Color(0f, 0f, 0f, 0f));//Deixa o frame transparente.
            mostrar.setText("Mostrar");
            x = false;
        }
        fechar.setOpaque(x);
        repaint();
    } 
}
    
14.03.2014 / 13:57