My issues is that I have created a JPanel that draws a gradient as a background. But when I go to add components to it (like a JButton) it does nothing...
Please help!
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CocoaTrashBar extends JPanel {
String titleText = "";
Color topLineColor = new Color(128, 128, 128);
Color bottomLineColor = new Color(69, 69, 69);
Color gradientTop = new Color(116, 116, 116);
Color gradientBottom = new Color(81, 81, 81);
public CocoaTrashBar() {
setDefaults();
}
public CocoaTrashBar(String title) {
setDefaults();
this.titleText = title;
}
public void setTitle(String title) {
this.titleText = title;
this.repaint();
}
private void setDefaults() {
super.setOpaque(true);
this.setPreferredSize(new Dimension(0, 24));
installWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
topLineColor = new Color(128, 128, 128);
bottomLineColor = new Color(69, 69, 69);
gradientTop = new Color(116, 116, 116);
gradientBottom = new Color(81, 81, 81);
repaintComponent();
}
public void windowLostFocus(WindowEvent e) {
topLineColor = new Color(171, 171, 171);
bottomLineColor = new Color(103, 103, 103);
gradientTop = new Color(156, 156, 156);
gradientBottom = new Color(121, 121, 121);
repaintComponent();
}
}, this);
}
private static void installWindowFocusListener(
WindowFocusListener focusListener, JComponent component) {
component.addPropertyChangeListener("Frame.active",
createFrameFocusPropertyChangeListener(focusListener, component));
}
private static PropertyChangeListener createFrameFocusPropertyChangeListener(
final WindowFocusListener focusListener, final JComponent component) {
return new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
Window window = SwingUtilities.getWindowAncestor(component);
boolean hasFocus = (Boolean) component.getClientProperty("Frame.active");
if (hasFocus) {
focusListener.windowGainedFocus(
new WindowEvent(window, WindowEvent.WINDOW_GAINED_FOCUS));
} else {
focusListener.windowLostFocus(
new WindowEvent(window, WindowEvent.WINDOW_LOST_FOCUS));
}
}
};
}
private void repaintComponent() {
this.repaint();
}
@Override
public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2d =(Graphics2D)g;
// Draw first line
g2d.setPaint(topLineColor);
g2d.drawLine(0, 0, this.getWidth(), 0);
// Draw last line
g2d.setPaint(bottomLineColor);
g2d.drawLine(0, 23, this.getWidth(), 23);
// Draw gradient
GradientPaint gradient = new GradientPaint(
this.getX(),
this.getY()+1,
gradientTop,
this.getX(),
this.getHeight()-1,
gradientBottom);
g2d.setPaint(gradient);
g2d.fillRect(this.getX(), this.getY()+1, this.getWidth(), this.getHeight()-2);
if(titleText != null) {
g2d.setFont(new Font("", Font.BOLD, 13));
g2d.setColor(Color.WHITE);
g2d.drawString(titleText, 10, 16);
}
g.dispose();
}
}