JToolBar Background Image Example

Here is an example on how to add background image to JToolBar. This is quit simple job. Just override the paintComponent method of JComponent and draw the image using g.drawImage.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import javax.swing.*;

public class TooBarBackgroundDemo extends JFrame {

    JToolBar toolbar;

    public TooBarBackgroundDemo() {
        toolbar = new JToolBar() {

            public void paintComponent(Graphics g) {
                Dimension size = toolbar.getSize();
                g.drawImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/toolbarbg.png")), 0, 0, size.width, size.height, this);


        toolbar.add(new JButton("Item 1"));
        toolbar.add(new JButton("Item 2"));
        toolbar.add(new JButton("Item 3"));
        add(toolbar, BorderLayout.NORTH);

        setTitle("JToolBar Background Demo");
        setSize(300, 200);

    public static void main(String[] args) {
        TooBarBackgroundDemo tbd = new TooBarBackgroundDemo();




Hello! I am Md. Abdul Bari; founder and admin of javaknowledge; currently providing training as a J2EE faculty in The Computers Ltd. Dhaka, Bangladesh under an IT Scholarship project provided by IDB-BISEW. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

More Posts - Website

Follow Me:
TwitterFacebookGoogle Plus

6,577` Total Views

Leave a Reply

Your email address will not be published. Required fields are marked *

four × 1 =