Search code examples
javaswingtransparencyjtextfield

JTextField with transparent background not behaving well


I am developing a chat app., where the user types all his info here, but the JTextField is not behaving really well, as shown in the image attached.


Solution

  • Wildly guessing: the field's opacity isn't set correctly to false, which produces the artefacts. The snippet below works just fine:

    JTextField name = new JTextField(20);
    name.setOpaque(false);
    JTextField pass = new JTextField(20);
    pass.setOpaque(false);
    pass.setForeground(Color.WHITE);
    // being lazy: use SwingX 
    JXPanel panel = new JXPanel();
    BufferedImage back = XTestUtils.loadDefaultImage("moon.jpg");
    panel.setBackgroundPainter(new ImagePainter(back));
    panel.add(name);
    panel.add(pass);