Sathish
Sathish

Reputation: 1521

Gradient Background for JToolBar

I Want to set gradient background color for JToolBar in Java. Am able to set this gradient effect for JPanel.

Thanks, Sathish

Upvotes: 1

Views: 2400

Answers (1)

mre
mre

Reputation: 44240

Like any other Swing component, you must override its paintComponent(...) method. For instance,

@Override
protected void paintComponent(Graphics g){
    // Create the 2D copy
    Graphics2D g2 = (Graphics2D)g.create();

    // Apply vertical gradient
    g2.setPaint(new GradientPaint(0, 0, Color.WHITE, 0, getHeight(), Color.BLUE));
    g2.fillRect(0, 0, getWidth(), getHeight());

    // Dipose of copy
    g2.dispose();
}

If you want this gradient to show through the components on the JToolBar, you must invoke setOpaque(false) on each of those components.

Upvotes: 1

Related Questions