Sunday, September 4, 2011

Creating a JMenuBar with JMenu and Mnemonics

Program Description:

One of the interesting part in programming java that I enjoyed the most is making JMenu, JMenuBar, and JMenuItem because it looks good in the program and it helps you manage and organize everything that is in your program. It is considered as one of the important part of a software. What I like about making JMenu, JMenuBar, and JMenuItem in Java is that it is very easy to create and understand.

Output:
Code:

/**
 * File: menuBarWithMenu.java
 * Tiltle: Creating a JMenuBar with JMenu and Mnemonics
 * Author: http://java-program-sample.blogspot.com/
 */

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

public class menuBarWithMenu extends JFrame {
	
	//Initializing JMenu, JMenuBar, Mnemonics, and Specified Menu Strings
	private String menuNames[] = {"File","Edit","View","Project","Build","Run","Tools","Configure","Window","Help"};
	private char mnemonic[] = {'F','E','V','P','B','R','T','C','W','H'};
	private JMenu fileMenu[];
	private JMenuBar menuBar;

	//Setting up GUI
    public menuBarWithMenu() {
    	
    	//Setting up the Title of the Window
    	super("Creating a JMenuBar with JMenu and Mnemonics");

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

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

		//Constructing JMenu and JMenuBar
    	fileMenu = new JMenu[10];
    	menuBar = new JMenuBar();
    	
    	//Setting up the JMenuBar in the container or automtically add JMenuBar in the container
    	setJMenuBar(menuBar);
    	
    	//Constructing 10 JMenu using "for loop" with Mnemonics and automatically add it in the JMenuBar
    	for(int count=0; count<fileMenu.length; count++) {
    		fileMenu[count] = new JMenu(menuNames[count]); //Constructing JMenu using the specified strings above
    		fileMenu[count].setMnemonic(mnemonic[count]); //Adding mnemonics on JMenu using the specified characters above
    		menuBar.add(fileMenu[count]); //Adding all 10 JMenu in the JMenuBar
    	}

    	//Setting up the container ready for the components to be added.
    	Container pane = getContentPane();
    	setContentPane(pane);  	

    	/**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) {
    	menuBarWithMenu mbwm = new menuBarWithMenu();
	}
}

Basic JPanel Borders

Program Description:

The Java Program below is just a simple java code showing different JPanel borders. I have created 15 types of JPanel borders that is commonly used but there are more borders available using BorderFactory class, all you have to do is explore. The purpose of this program is to show how to use the class BorderFactory in creating different kinds of borders.

Output:
Code:

/**
 * File: jpanelBorders.java
 * Tiltle: Basic JPanel Borders
 * Author: http://java-program-sample.blogspot.com/
 */

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

public class jpanelBorders extends JFrame {
	
	//Initializing JPanel
	private JPanel panels[];

	//Setting up GUI
    public jpanelBorders() {
    	
    	//Setting up the Title of the Window
    	super("Basic JPanel Borders");

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

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

    	//Constructing JPanel
    	panels = new JPanel[15];

    	//Setting up the container ready for the components to be added.
    	Container pane = getContentPane();
    	setContentPane(pane);

    	//Setting up the container layout
    	GridLayout grid = new GridLayout(3,5);
    	pane.setLayout(grid);
    	
    	//Constructing 15 JPanels using "for loop" and add automatically in the container
    	for(int count = 0; count<panels.length; count++) {
    		panels[count] = new JPanel();
    		pane.add(panels[count]);
    	}
    	
    	//Beveled Borders
    	panels[0].setBorder(BorderFactory.createBevelBorder(0));
    	panels[1].setBorder(BorderFactory.createBevelBorder(1));
    	panels[2].setBorder(BorderFactory.createBevelBorder(0, Color.red, Color.green));
    	panels[3].setBorder(BorderFactory.createBevelBorder(1, Color.red, Color.green));
    	panels[4].setBorder(BorderFactory.createBevelBorder(0, Color.red, Color.green, Color.blue, Color.cyan));
    	panels[5].setBorder(BorderFactory.createBevelBorder(1, Color.red, Color.green, Color.blue, Color.cyan));
    	
    	//Line Borders
    	panels[6].setBorder(BorderFactory.createLineBorder(Color.blue));
    	panels[7].setBorder(BorderFactory.createLineBorder(Color.blue,10));
    	
    	//Titled Border
    	panels[8].setBorder(BorderFactory.createTitledBorder("String Border"));
    	panels[9].setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(1), "Custom SB"));
    	
    	//Etched Border
    	panels[10].setBorder(BorderFactory.createEtchedBorder(0));
    	panels[11].setBorder(BorderFactory.createEtchedBorder(1));
    	panels[12].setBorder(BorderFactory.createEtchedBorder(Color.red, Color.blue));
    	panels[13].setBorder(BorderFactory.createEtchedBorder(0, Color.black, Color.cyan));
    	panels[14].setBorder(BorderFactory.createEtchedBorder(1, Color.black, Color.cyan));

    	/**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) {
    	jpanelBorders jpb = new jpanelBorders();
	}
}

Important Part of the Program:

//Constructing 15 JPanels using "for loop" and add automatically in the container
    	for(int count = 0; count<panels.length; count++) {
    		panels[count] = new JPanel();
    		pane.add(panels[count]);
    	}
    	
    	//Beveled Borders
    	panels[0].setBorder(BorderFactory.createBevelBorder(0));
    	panels[1].setBorder(BorderFactory.createBevelBorder(1));
    	panels[2].setBorder(BorderFactory.createBevelBorder(0, Color.red, Color.green));
    	panels[3].setBorder(BorderFactory.createBevelBorder(1, Color.red, Color.green));
    	panels[4].setBorder(BorderFactory.createBevelBorder(0, Color.red, Color.green, Color.blue, Color.cyan));
    	panels[5].setBorder(BorderFactory.createBevelBorder(1, Color.red, Color.green, Color.blue, Color.cyan));
    	
    	//Line Borders
    	panels[6].setBorder(BorderFactory.createLineBorder(Color.blue));
    	panels[7].setBorder(BorderFactory.createLineBorder(Color.blue,10));
    	
    	//Titled Border
    	panels[8].setBorder(BorderFactory.createTitledBorder("String Border"));
    	panels[9].setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(1), "Custom SB"));
    	
    	//Etched Border
    	panels[10].setBorder(BorderFactory.createEtchedBorder(0));
    	panels[11].setBorder(BorderFactory.createEtchedBorder(1));
    	panels[12].setBorder(BorderFactory.createEtchedBorder(Color.red, Color.blue));
    	panels[13].setBorder(BorderFactory.createEtchedBorder(0, Color.black, Color.cyan));
    	panels[14].setBorder(BorderFactory.createEtchedBorder(1, Color.black, Color.cyan));

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