Take the 2-minute tour ×

I have been learning Java for a few weeks now and I am really stuck when it comes to applying a background image to a JFrame. Every tutorial I've come across doesn't create Frames the way I do ( I extend JFrame ) or if they do, the instructions aren't clear enough for me to understand.

The code below is from a project of my own so help me practice what I've learned so far. Please could you build on the code below and explain to me what to add and where, so I may have an image as the background to my frame?

One thing I would really appreciate is if you could explain how things work and why there are needed and what they are actually doing - I don't like the idea of blindly copying and pasting what you've done without any clue of how it works. The more depth in explanation, the better; even if it sounds patronising.

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

class MiniPad extends JFrame implements ActionListener {

    JPanel pan = new JPanel();
    ClassLoader ldr = this.getClass().getClassLoader();
    ImageIcon closeImg = new ImageIcon(ldr.getResource("\\images\\buttons\\closeBtn.png"));
    JTextArea note = new JTextArea("", 6, 21);
    JScrollPane notes = new JScrollPane(note);
    JButton close = new JButton(closeImg);

    public static void main(String[] args) {
        MiniPad padgui = new MiniPad();
    } //Instance of GUI

    public MiniPad() {
        setSize(265, 191);
//Adding to JPanel 'pan'

    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == close) {
share|improve this question
You may want to use a LayoutManager on pan as it's default LayoutManager is a FlowLayout which might not give you the best results. Have a look at docs.oracle.com/javase/tutorial/uiswing/layout/using.html & docs.oracle.com/javase/tutorial/uiswing/layout/visual.html for some more details –  MadProgrammer Aug 7 '12 at 21:14
@AlexanderPower : Please do have a look at this link for HOW TO ADD IMAGES TO YOUR PROJECT and this small example by me, where I had tried to explain a bit. If you still need something more, please ask for it anytime :-) And here is one example, related to your code sequence as to what you doing wrong in your code. –  nIcE cOw Aug 8 '12 at 15:08

2 Answers 2

up vote 3 down vote accepted

As to your question as it's titled

You will want to setup a custom Component (such as a JPanel) and override the paintComponent method

Take a look at

Now, you're going to have some issues, the note pane will block most of the background, obscuring the image. This shouldn't discourage from trying though ;)

You'll want to familiarise yourself with how Swing components can be made transparent as well.

Some feedback ;)

I don't think you need to keep a reference to the Classloader, for what you're using it for, it's just adding weight and complexity to your code. It's not a massive issue, but my first question was, "What's he using the class loader for??"

I'd probably adopt some kind of naming convention as well. Generally been a little more verbose will make it easier to understand the code.

close ?? Is that an action? Maybe something like "closeButton", at least I know it's some type of button

share|improve this answer

Better to use:

ImageIcon closeImg = new ImageIcon(getClass().getResource("\\images\\buttons\\closeBtn.png"));

Make sure your image is located in the same location relative to where your class file is output.

Also your close button calls setVisible(false) but does not dispose of any resources or exit the application.

share|improve this answer
Instead of setVisible(false) use dispose(), it will release the resources and exit the application (as per your request setDefaultCloseOperation(EXIT_ON_CLOSE)) –  MadProgrammer Aug 7 '12 at 21:12
Thanks for this. As I'm learning I don't know many of these little tricks you can do - I'm basically following what my book tells me, and that was never mentioned. I'll keep a note of it. :-) –  Alex Aug 7 '12 at 21:35

Your Answer


