Transparent java background

Asked

Viewed 2,185 times

3

I have a problem with background transparent of a JTextField, by changing the content of JTextField it seems to superimpose the text and get all mixed up:

exemplo do erro

I’m setting the background like this:

 Field.setBackground(new Color(1.0f,1.0f,1.0f,0f));

//EDIT

I managed to do using the @utluiz hint but the numbers are white pixels around:

It has to make the fund fully transparent as well?

2 answers

2

I do so:

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();
    } 
}

2


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

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.

  • then my structure is the following, a borderlayout where one the position North of it, then insert a gridlayout and within that grid the Jtextfield, when repaint use in the last grid, nothing happens, when I use in that grid higher my bottom grid disappears all

  • well before I had put this at the end of the main and it happened up, now I put right after the add and nothing happened, the java asks to change my JPanel all in the end

  • @alleen94 There would be no way you could post a little more of your code in the question so I can test?

  • I’ll try to get this messy ^^

Browser other questions tagged

You are not signed in. Login or sign up in order to post.