Showing posts with label JLabel. Show all posts
Showing posts with label JLabel. Show all posts

Monday, April 23, 2012

How to put a JTextField, JLabel, and JButton above image

Program Description:

This Java program demonstrates on how to add JComponents above an image. The technique is simple, it is just a JPanel inside another JPanel.

We created a class called "backgroundClass" which extends to JPanel so that means our class "backgroundClass" is a JPanel. In our main JPanel the class "backgroundClass", we set there our background image then inside our main JPanel we added two JPanels which holds the JLabel, JTextField, and JButton.

By setting the background color of our JPanel 1 to transparent, we can see in the output that it is like the label, the buttons, and the textfields are actually placed above the image but it is not, instead it is a layer of panels, a JPanel placed inside another JPanel.

Output:
Code:

Main Program

/**
 * File: interfaceAndImage.java
 * Tiltle: How to put a JTextField, JLabel, and JButton above image
 * Author: http://java-program-sample.blogspot.com/
 */

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;

public class interfaceAndImage extends JFrame {
 
 //Constructing the class we created called "backgroundClass" so we can
 //use it here in our main program as a parent panel.
 backgroundClass bc;
 
 //Initializing our JComponents and the labels of our JButton and JLabel
 JPanel panel1, panel2;
 JLabel labels[];
 JButton choices[];
 JTextField inputs[];
 String lebelName[] = {"Name:","Age:","Sex:","Address:","Tel. No.:"};
 String buttonChoice[] = {"Register","Reset","Cancel"};

 //Setting up GUI
    public interfaceAndImage() {
     
     //Setting up the Title of the Window
     super("How to put a JTextField, JLabel, and JButton above image");

     //Set Size of the Window (WIDTH, HEIGHT)
     setSize(310,170);

     //Exit Property of the Window
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     //Constructing the class "backgroundClass" and call the keyword "this" so it can
     //be recognizable by our main program.
     bc = new backgroundClass(this);
     
     //Constructing JPanel 1 and set its layout property including the background property
     //which is transparent
  panel1 = new JPanel();
  panel1.setLayout(new GridLayout(5,2));
  
  //Constructing the class Color and set its property to 0,0,0,0 means no color.
  Color trans = new Color(0,0,0,0);
  panel1.setBackground(trans); //Setting JPanel 1 background to transparent
  
  
     //Constructing JPanel 2 and set its layout property
  panel2 = new JPanel();
  panel2.setLayout(new GridLayout());
  
  //Constructing our JComponents setting its specific array size
     labels = new JLabel[5];
     inputs = new JTextField[5];
     choices = new JButton[3];
     
     //Adding our JPanel 1 and 2 to our class "backgroundClass" which is our parent panel
     bc.add(panel1, BorderLayout.NORTH);
     bc.add(panel2, BorderLayout.SOUTH);

     //Setting up the container ready for the components to be added.
     Container pane = getContentPane();
     setContentPane(pane);
     
     //Constructing JLabel and JTextField using "for loop" in their desired order
     for(int count=0; count<inputs.length && count<labels.length; count++) {
      labels[count] = new JLabel(lebelName[count], JLabel.RIGHT);
      inputs[count] = new JTextField(30);
      
      //Adding the JLabel and the JTextFied in JPanel 1
      panel1.add(labels[count]);
      panel1.add(inputs[count]);
     }

  //Constructing all 4 JButtons using "for loop" and add them in the panel 1
     for(int count=0; count<choices.length; count++) {
      choices[count] = new JButton(buttonChoice[count]);
      panel2.add(choices[count]);
     }
  
  //Adding the class "backgroundClass" to our container as parent panel
  pane.add(bc);
  
     /**Set all the Components Visible.
      * If it is set to "false", the components in the container will not be visible.
      */
     setVisible(true);
     
     //Disable window size
     setResizable(false);
    }
    
 //Main Method
    public static void main (String[] args) {
     interfaceAndImage iai = new interfaceAndImage();
 }
}

Main JPanel (class "backgroundClass")

This is our main JPanel which holds our background image. This is where we also place our JButtons, JLabel, JTextField, and another JPanel by calling this class to our main Program.

/**
 * File: backgroundClass.java
 * Tiltle: Adding Image Above Another Image (class extention)
 * Author: http://java-program-sample.blogspot.com/
 */

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;

public class backgroundClass extends JPanel {
  
 //Initializing the class Image
 Image background;

 //Setting up GUI
    public backgroundClass(interfaceAndImage iai) {
     
     //Constructing the class "Toolkit" which will be used to manipulate our images.
     Toolkit kit = Toolkit.getDefaultToolkit();
     
     //Getting the "background.jpg" image we have in the folder
     background = kit.getImage("background.jpg");
    }
    
    //Manipulate Images with JAVA2D API. . creating a paintComponent method.
     public void paintComponent(Graphics comp) {
      
      //Constructing the class Graphics2D. Create 2D by casting the "comp" to Graphics2D
     Graphics2D comp2D = (Graphics2D)comp;
     
     //creating a graphics2d using the images in the folder and place it in a specific coordinates.
         comp2D.drawImage(background, 0, 0, this);
     }
}

Sunday, September 11, 2011

Creating a JDesktopPane

Program Description:

One of the interesting and useful part in making Java Program is creating a JDesktopPane which is an MDI (Multiple Document Interface) commonly used in today's applications. It is a main window called parent window that contains other windows called child windows. It is used to manage several open documents that are being processed in parallel. The Java Program below is a short simple code on how to create a JDesktopPane and JInternalFrame.

Output:


Code:

/**
 * File: createJDesktoPane.java
 * Tiltle: Creating a JDesktopPane
 * Author: http://java-program-sample.blogspot.com/
 */

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;
import java.awt.event.*;

public class createJDesktoPane extends JFrame {
 
 //Initializing program components
 private JDesktopPane desktopTest;
 private JLabel labels[];
 private JTextField inputs[];
 private JButton buttons[];
 private String labelName[]={"Enter Name: ","Enter Age: ","Enter Address: ","Enter Mobile#: "};
 private String buttonName[] = {"Open","Save","Exit"};
 private JPanel panel1, panel2;

 //Setting up GUI
    public createJDesktoPane() {
     
     //Setting up the Title of the Window
     super("Creating a JDesktopPane");

     //Set Size of the Window (WIDTH, HEIGHT)
     setSize(600,500);

     //Exit Property of the Window
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     JMenuBar bar = new JMenuBar(); //Constructing JMenuBar
     JMenu menu = new JMenu("File"); //Constructing JMenu name "File"
     JMenuItem newFile = new JMenuItem("Add New Data"); //Constructing JMenuItem with "Add New Data" label
     
     menu.add(newFile); //Adding JMenuItem in the JMenu
     bar.add(menu); //Adding JMenu in the JMenuBar
     
  setJMenuBar(bar); //Adding JMenuBar in the container
  
  desktopTest = new JDesktopPane(); //Creating a JDesktopPane
  desktopTest.setBackground(Color.BLACK); //Setting JDesktopPane background color
  
     //Setting up the container ready for the components to be added.
     Container pane = getContentPane();
     setContentPane(pane);
     
     pane.add(desktopTest); //Adding JDesktopPane in the container
     
     //Implemeting Even-Listener on newFile JMenuItem
     newFile.addActionListener(
  new ActionListener() {
   
   //Handle JMenuItem "newFile" event if it is clicked
   public void actionPerformed(ActionEvent e) {
   
   //Constructing an Internal Frame inside JDesktopPane
   JInternalFrame frame = new JInternalFrame(null,true,true,true,true);
   
   Container container = frame.getContentPane(); //Creating a container inside the JInternalFrame
   
   //Constructing JLabel, JButton, and JTextField inside JInternalFrame
   labels = new JLabel[4];
   inputs = new JTextField[4];
   buttons = new JButton[3];
   
   //Creating a JPanel 1 with GridLayout of 4 rows and 2 columns inside JInternalFrame
   panel1 = new JPanel();
   panel1.setLayout(new GridLayout(4,2));
   
   //Constructing JLabel and JTextField using "for loop" and add to JPanel 1
   for(int count=0; count<labels.length && count<inputs.length; count++) {
    labels[count] = new JLabel(labelName[count]);
    inputs[count] = new JTextField(10);
    panel1.add(labels[count]);
    panel1.add(inputs[count]);
   }
   
   //Creating a JPanel 2 with GridLayout of 1 row and 3 columns inside JInternalFrame
   panel2 = new JPanel();
   panel2.setLayout(new GridLayout(1,3));
   
   //Constructing JButton using "for loop" and add to JPanel 2
   for(int count=0; count<buttons.length; count++) {
    buttons[count] = new JButton(buttonName[count]);
    panel2.add(buttons[count]);
   }
   
   //Adding JPanel 1 and 2 to the JInternalFrame container
   container.add(panel1,BorderLayout.NORTH);
   container.add(panel2,BorderLayout.CENTER);
   
   frame.setTitle("Add New Data"); //Set the Title of the JInternalFrame
   frame.setResizable(false); //Lock the size of the JInternalFrame
   frame.setMaximizable(false); //Disable the Maximize function of JInternalFrame
   
   //Set the size of JInternalFrame to the size of its content
   frame.pack();
   
   //Attached the JInternalFrame to JDesktopPane and show it by setting the visible in to "true"
   desktopTest.add(frame);
   frame.setVisible(true);
   }
  }
  );

     /**Set all the Components Visible.
      * If it is set to "false", the components in the container will not be visible.
      */
     setVisible(true);
    }
    
 //Main Method
    public static void main (String[] args) {
     createJDesktoPane dp = new createJDesktoPane();
 }
}

Saturday, August 27, 2011

JLabel Positions

Program Description:

The Java program below is a JLabel demonstration that shows different JLabel positions.

Output:
Code:

/**
 * File: jlablePositions.java
 * Tiltle: JLabel Positions
 * Author: http://java-program-sample.blogspot.com/
 */

//Java Extension Packages
import javax.swing.*;
//Java Core Packages
import java.awt.*;

public class jlablePositions extends JFrame {

	//Setting up GUI
	public jlablePositions() {
		
	//Setting up the Title of the Window
	super("JLabel Positions");

	//Set Size of the Window (WIDTH, HEIGHT)
	setSize(350,200);

	//Exit Property of the Frame of Window
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	//Constructing JLabel with different positions
	JLabel trailing = new JLabel("TRAILING POSITION",JLabel.TRAILING);
	JLabel left = new JLabel("LEFT POSITION",JLabel.LEFT);
	JLabel right = new JLabel("RIGHT POSITION",JLabel.RIGHT);
	JLabel center = new JLabel("CENTER POSITION",JLabel.CENTER);
	JLabel leading = new JLabel("LEADING POSITION",JLabel.LEADING);
	
	//Setting up the container ready for the components to be added
	Container pane = getContentPane();

	//Setting the position of the JLabel
	GridLayout flo = new GridLayout(5,1);

	//Adding the Layout and JLabel in the container
	pane.setLayout(flo);
	pane.add(trailing);
	pane.add(left);
	pane.add(right);
	pane.add(center);
	pane.add(leading);
	setContentPane(pane);
	setVisible(true);
	}
	
	//Main Method
	public static void main(String[] args) {
	jlablePositions jls = new jlablePositions();
	}
}

Important Part of the Program:

//Constructing JLabel with different positions
	JLabel trailing = new JLabel("TRAILING POSITION",JLabel.TRAILING);
	JLabel left = new JLabel("LEFT POSITION",JLabel.LEFT);
	JLabel right = new JLabel("RIGHT POSITION",JLabel.RIGHT);
	JLabel center = new JLabel("CENTER POSITION",JLabel.CENTER);
	JLabel leading = new JLabel("LEADING POSITION",JLabel.LEADING);

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hostgator Discount Code