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