Neither of the two topics in this document and essential or important. They're just extra things that you can do.

How to add images to JButtons

We'll be looking at how to put images on a JButton.
We'll also look at how to position components on a JPanel without using a layout manager.

ImageIcons:

One has to use an ImageIcon for this task. (Normally we use BufferedImages.) It's really easy to load an imageIcon.

	//Load the imageIcon
	String filename = "android-128x128.png";
	ImageIcon icon = new ImageIcon(filename);

Note that I'm not specifying where the image is located. That's the subject for a different document.

	JButton btnIconOnly = new JButton(icon);

That's it! Now just add the JButton to your JPanel

The JButton is resized to the size of the image

Absolute Positioning:

Most of the time we use layout managers. Aside from BorderLayout, it's awkward and a lot of work to position things where you want them. FlowLayout is easy but often to simple. MigLayout will do exactly what we want, but takes a bit more work to install.

First set the layout manager to be null (instead of the default FlowLayout):

	panel.setLayout(null);

Since we don't have a layoutmanager applied, we have to add code to get the preferredSize, and then use setBounds() to set the size(?) of the component. Only then can it be added to the JPanel.
If you don't setBounds() then it won't display at all.

	JButton btnIconOnly = new JButton(icon);
	Dimension size = btnIconOnly.getPreferredSize();
	btnIconOnly.setBounds(120,20,size.width, size.height);
	panel.add(btnIconOnly);

Complete Code:

import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/****************************************************************************
 * Program to illustrate using ImageIcon to 
 * add images to JButtons.
 *
 * It also demonstrates to to do absolute positioning (no layout manager)
 ****************************************************************************/
public class ImageIcon_1 {

	public static void main(String[] args) {
		
		//Load the imageIcon
		String filename = "android-128x128.png";
		ImageIcon icon = new ImageIcon(filename);

		JFrame window = setupJFrame(350,400, "Android Icon Buttons");
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		
		JButton btnTextOnly = new JButton("Android");
		// We use getPreferredSize to find the smallest width and height that will display everything properly.
		Dimension size = btnTextOnly.getPreferredSize(); 
		btnTextOnly.setBounds(20, 20, size.width, size.height);
		panel.add(btnTextOnly);
		
		//The JButton is always the size of the imageIcon (unless otherwise specified)
		JButton btnIconOnly = new JButton(icon);
		//Since we don't have a layoutmanager applied, we have to add code to get the preferredSize
		size = btnIconOnly.getPreferredSize();
		btnIconOnly.setBounds(120,20,size.width, size.height);
		panel.add(btnIconOnly);
		
		//This won't show up since I don't have setBounds() set, so it doesn't know where to display it.
		panel.add(new JLabel("A button with text and icon:"));
		
		//Both text and an Icon
		JButton btnTextIcon = new JButton("Android",icon);
		size = btnTextIcon.getPreferredSize();
		btnTextIcon.setBounds(20,200,size.width, size.height);
		panel.add(btnTextIcon);
		
		window.add(panel);
		window.setVisible(true);
	}
	
	static JFrame setupJFrame(int x, int y, String s) {
		JFrame frame = new JFrame();
		frame.setTitle(s);
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);	
		frame.setSize(x,y);
		frame.setLocationRelativeTo(null);		
		return frame;
	}
}

Screenshot of output: