Search code examples
javaswingjpanelpaintcomponent

I seem to be painting under the JPanel


I've been trying to make a simple login program with a MYSQL database and when I added the JTextField and JPasswordField anything I paint under the paintComponent method appears to be covered. The problem where I can't see what I paint goes away when I don't add the panel which I need. Here is an example of my code.

public void createWindow(){
    frame.add(this);
    frame.setSize(1200, 720);
    etc.. all the necessary stuff
    //userfield is my JTextField
    panel.add(userfield);
    //This next line is the problem
    frame.add(panel);
}

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawString("Hello", 0, 0);
}

Solution

  • JPanel is opaque by default, meaning that anything that it covers will not be painted.

    Try making the JPanel transparent by using JPanel#setOpaque and passing it false