Friday, November 11, 2016

Adding re-sizable background image to any JFrame

In this post, we will create a method which can add background image to any JFrame. You can simply use this method in your swing application and get background images over your frames easily.

Language Used
Java

Code repository:
https://github.com/csanuragjain/extra/tree/master/Set%20Background%20JFrame

Program:

setBackground method:
      public static JLabel setBackground(JFrame frame, String backgroundFilePath)  
      {  
           frame.setLayout(new BorderLayout());  
           JLabel background=new JLabel(new ImageIcon(backgroundFilePath));  
           frame.add(background);  
           background.setLayout(new FlowLayout(0));  
           return background;  
      }  

How it Works:

1) Pass the frame for which you want to add background and the path of the background image.
2) First we will change the user frame layout to Border layout
3) We define a ImageIcon using the background image path and pass the same to a new label
4) Now we add the label to the user provided frame.
5) Since we set frame layout as BorderLayout so the label image will cover the full JFrame window.
6) Now we will change the layout of the label to FlowLayout and return the same to the user.
7) The frame passed by user has the background added.

Main method:
      public static void main(String[] args) {  
           JFrame frame=new JFrame("My frame");  
           JLabel background=setBackground(frame, "background.png");  
           /**  
            * Your usual work on the frame  
            */  
           JLabel l1=new JLabel("Enter your name");  
           JTextField t1=new JTextField(10);  
           JButton b1=new JButton("Submit");  
           background.add(l1);  
           background.add(t1);  
           background.add(b1);  
           frame.setTitle("My Application");  
           frame.setSize(640,480);  
           frame.setDefaultCloseOperation(EXIT_ON_CLOSE);  
           frame.setVisible(true);  
      }  

How it Works:

1) User calls the setBackground function with his frame and the background image path.
2) setBackground returns a JLabel object.
3) User will now add everything on the label returned by the function instead of JFrame directly. Reason is because JLabel contains the background image and we want all user form to come within background image. That's why user adds his form in the JLabel returned.

Full Program:
 package com.cooltrickshome;  
 import java.awt.BorderLayout;  
 import java.awt.FlowLayout;  
 import javax.swing.ImageIcon;  
 import javax.swing.JButton;  
 import javax.swing.JFrame;  
 import javax.swing.JLabel;  
 import javax.swing.JTextField;  
 public class SetBackgroundJFrame extends JFrame{  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           JFrame frame=new JFrame("My frame");  
           JLabel background=setBackground(frame, "background.png");  
           /**  
            * Your usual work on the frame  
            */  
           JLabel l1=new JLabel("Enter your name");  
           JTextField t1=new JTextField(10);  
           JButton b1=new JButton("Submit");  
           background.add(l1);  
           background.add(t1);  
           background.add(b1);  
           frame.setTitle("My Application");  
           frame.setSize(640,480);  
           frame.setDefaultCloseOperation(EXIT_ON_CLOSE);  
           frame.setVisible(true);  
      }  
      public static JLabel setBackground(JFrame frame, String backgroundFilePath)  
      {  
           frame.setLayout(new BorderLayout());  
           JLabel background=new JLabel(new ImageIcon(backgroundFilePath));  
           frame.add(background);  
           background.setLayout(new FlowLayout(0));  
           return background;  
      }  
 }  

Output:

No comments:

Post a Comment