views:

394

answers:

1

I have the following code to paint a JPanel's background:

@Override
 public void paintComponent(Graphics g) {
    UIDefaults uid = UIManager.getDefaults();
    Graphics2D g2d = (Graphics2D)g;
    Dimension d = this.getSize();

    g2d.setPaint(new GradientPaint(0, 0, uid.getColor("ToolBar.light"), 0, d.height, uid.getColor("ToolBar.shadow"), true));
    g2d.fillRect(0, 0, d.width , d.height);

    g2d.setColor(uid.getColor("ToolBar.light"));
    g2d.drawLine(0, d.height-2, d.width, d.height-2);

    g2d.setColor(uid.getColor("ToolBar.highlight"));
    g2d.drawLine(0, d.height-1, d.width, d.height-1);
 }

I have a JLabel inside this JPanel (with opaque = false) but the JLabel's background is still some kind of gray. How can I paint a gradient on this JPanel background with a transparent background for the JLabel?

Full working example below:

DemoFrame.java:

import javax.swing.UIManager;

public class DemoFrame extends javax.swing.JFrame {

    public DemoFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        panelHeader = new Extras.JPanelHeader();
        labelTitle = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        panelHeader.setOpaque(false);
        panelHeader.setPreferredSize(new java.awt.Dimension(172, 29));
        panelHeader.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        labelTitle.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        labelTitle.setText("Criar Novo Utilizador");
        labelTitle.setOpaque(true);
        panelHeader.add(labelTitle);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelHeader, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelHeader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(83, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DemoFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel labelTitle;
    private Extras.JPanelHeader panelHeader;
    // End of variables declaration

}

JPanelHeader.java:

import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import javax.swing.UIDefaults;
import javax.swing.UIManager;

public class JPanelHeader extends JPanel {

    @Override
     public void paintComponent(Graphics g) {
        UIDefaults uid = UIManager.getDefaults();
        Graphics2D g2d = (Graphics2D)g;
        Dimension d = this.getSize();

        g2d.setPaint(new GradientPaint(0, 0, uid.getColor("ToolBar.shadow"), 0, d.height, uid.getColor("ToolBar.background"), false));
        g2d.fillRect(0, 0, d.width , d.height);

        g2d.setColor(uid.getColor("ToolBar.light"));
        g2d.drawLine(0, d.height-2, d.width, d.height-2);

        g2d.setColor(uid.getColor("ToolBar.highlight"));
        g2d.drawLine(0, d.height-1, d.width, d.height-1);
     }

}
+2  A: 

You must be using some wierd LAF. If that is the case then that should be posted in your question. In fact a SSCCE should be posted with every question so we can see exactly what you are doing.

Using an alpha value for backgrounds can cause problems as is demonstrated by Backgrounds With Transparency.

Here is a simple SSCCE that shows you don't need to do anything special to have the label paint properly:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FaceComponent2 extends JPanel
{
    protected void paintComponent(Graphics g)
    {
        UIDefaults uid = UIManager.getDefaults();
        Graphics2D g2d = (Graphics2D)g;
        Dimension d = this.getSize();

        g2d.setPaint(new GradientPaint(0, 0, uid.getColor("ToolBar.light"),
            0, d.height, uid.getColor("ToolBar.shadow"), true));
        g2d.fillRect(0, 0, d.width , d.height);

        g2d.setColor(uid.getColor("ToolBar.light"));
        g2d.drawLine(0, d.height-2, d.width, d.height-2);

        g2d.setColor(uid.getColor("ToolBar.highlight"));
        g2d.drawLine(0, d.height-1, d.width, d.height-1);
    }


    public static void main(String args[])
        throws Exception
    {
        JComponent face = new FaceComponent2();
        face.setLayout( new GridLayout(0, 1) );
        face.setPreferredSize( new Dimension(250, 250) );
        face.add( new JLabel("Line1") );
        face.add( new JLabel("Line2") );
        face.add( new JLabel("Line3") );

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add( face );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

    }
}
camickr
I forgot about the look and feel, I'm using this on `main()`: `UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());` and I'm experiencing the problem on Windows if that's relevant.
Nazgulled
Your example look weird by the way. I mean, I ran the code and the window was empty, I had to resize it to make it display something.
Nazgulled
There is nothing tricky about this code, The code as posted runs fine and at the proper size. I'm using JDK6.7 on XP. It works with the default Metal LAF and the Windows LAF. If you can't even get the posted code to work then maybe you have a problem with your system, although I have no idea what it might be. Anyway this is another reason to post a SSCCE so we can help determine if the problem is with the code or a Java version or OS problem.
camickr
I've updated the main post with a minimal example demonstrating the problem.
Nazgulled