JAVA PROGRAMMING


Java Swings

What is Swings in java ?
  • A part of The JFC

    • Swing Java consists of

    • Look and feel

    • Accessibility

    • Java 2D

    • Drag and Drop, etc
  • Compiling & running programs
    • ‘javac <program.java>’ && ‘java <program>’
      Or JCreator / IDE
  • if you do not explicitly add a GUI component to a container, the GUI component will not be displayed when the container appears on the screen.
  • Swing, which is an extension library to the AWT, includes new and improved components that enhance the look and functionality of GUIs. Swing can be used to build Standalone swing gui Apps as well asServlets and Applets. It employs a model/view design architecture. Swing is more portable and more flexible than AWT.
Swing Model/view design: The “view part” of the MV design is implemented with a component object and the UI object. The “model part” of the MV design is implemented by a model object and a change listener object.

Swing is built on top of AWT and is entirely written in Java, using AWT’s lightweight component support. In particular, unlike AWT, t he architecture of Swing components makes it easy to customize both their appearance and behavior. Components from AWT and Swing can be mixed, allowing you to add Swing support to existing AWT-based programs. For example, swing components such as JSlider, JButton and JCheckbox could be used in the same program with standard AWT labels, textfields and scrollbars. You could subclass the existing Swing UI, model, or change listener classes without having to reinvent the entire implementation. Swing also has the ability to replace these objects on-the-fly.
  • 100% Java implementation of components
  • Pluggable Look & Feel
  • Lightweight components
  • Uses MVC ArchitectureModel represents the data
    View as a visual representation of the data
    Controller takes input and translates it to changes in data
  • Three parts 
    Component set (subclasses of JComponent)
    Support classes
    Interfaces
In Swing, classes that represent GUI components have names beginning with the letter J. Some examples are JButton, JLabel, and JSlider. Altogether there are more than 250 new classes and 75 interfaces in Swing — twice as many as in AWT.
Java Swing class hierarchy
The class JComponent, descended directly from Container, is the root class for most of Swing’s user interface components.


Swing contains components that you’ll use to build a GUI. I am listing you some of the commonly used Swing components. To learn and understand these swing programs, AWT Programming knowledge is not required.

Java Swing Examples

Below is a java swing code for the traditional Hello World program.
Basically, the idea behind this Hello World program is to learn how to create a java program, compile and run it. To create your java source code you can use any editor( Text pad/Edit plus are my favorites) or you can use an IDE like Eclipse.

import javax.swing.JFrame;
import javax.swing.JLabel;

//import statements
//Check if window closes automatically. Otherwise add suitable code
public class HelloWorldFrame extends JFrame {

	public static void main(String args[]) {
		new HelloWorldFrame();
	}
	HelloWorldFrame() {
		JLabel jlbHelloWorld = new JLabel("Hello World");
		add(jlbHelloWorld);
		this.setSize(100, 100);
		// pack();
		setVisible(true);
	}
}
Output





JTextField Source Code




// A program to demonstrate the use of JTextFields's
//Import Statements
swing.*;
import javax .
mport java.awt.*;
iimport java.awt.event.*;
JTextField jtfInput;
	JTextArea jtAreaOutput;
	String newline = "\n";
	public JTextFieldDemo2() {
		createGui();
	}
	public void createGui() {
		jtfInput = new JTextField(20);
		jtfInput.addActionListener(this);
		jtAreaOutput = new JTextArea(5, 20);
		jtAreaOutput.setEditable(false);

	JScrollPane scrollPane = new JScrollPane(jtAreaOutput,
	  
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
	JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		GridBagLayout gridBag = new GridBagLayout();
		Container contentPane = getContentPane();
		contentPane.setLayout(gridBag);
	GridBagConstraints gridCons1 = new GridBagConstraints();
		gridCons1.gridwidth = GridBagConstraints.REMAINDER;
		gridCons1.fill = GridBagConstraints.HORIZONTAL;
		contentPane.add(jtfInput, gridCons1);
	GridBagConstraints gridCons2 = new GridBagConstraints();
		gridCons2.weightx = 1.0;
		gridCons2.weighty = 1.0;
		contentPane.add(scrollPane, gridCons2);
	}
	public void actionPerformed(ActionEvent evt) {
		String text = jtfInput.getText();
		jtAreaOutput.append(text + newline);
		jtfInput.selectAll();
	}
	public static void main(String[] args) {
		JTextFieldDemo2 jtfTfDemo = new JTextFieldDemo2();
		jtfTfDemo.pack();
		jtfTfDemo.addWindowListener(new WindowAdapter() {

		public void windowClosing(WindowEvent e) {
			System.exit(0);
			}
		});
		jtfTfDemo.setVisible(true);
	}
}
OUTPUT
__________________________________________________________________


JAVA PROGRAMMING





Lesson 1-Programming Basics

Programming Languages



  • ·         Machine languages- it uses binary digits 0 and 1. It is the natural language of a computer thus, they are machine dependent.
  • ·         Assembly languages- it uses mnemonics or symbols to instructions and data. They used assemblers to convert source code to machine language then loaded run by a loader.
  • ·         High-level languages- they use English-like statements and it uses compilers or interpreters to translate the source code to machine language

Debugging
·         The process of eliminating mistakes in your program.

Three Types of Bugs/Errors



  • ·         Syntax Error – a grammatical mistake in your program.
  • ·         Run-time Error – detected when the program is run.
  • ·         Logic Error – the output of your program is wrong.

Object-Oriented Programming Language



  • ·         A type of high-level language.
  • ·         It is a programming paradigm that uses objects and their interactions to design applications and computer programs.
  • ·         It is a programming methodology that views a program as similarly consisting of objects that interact with one another by means of actions
OOP Concepts



  • ·         Objects- it is a program construction that has data (that is, information) associated with it and that can perform certain actions. It is a software bundle related state and behavior. Software objects are often used to model the real-world objects that you can find in everyday life.
  • ·         Methods- actions done by object.
  • ·         Class- it is a blueprint or prototype from which objects are created. Objects of the same type have same class.

OOP Design Principles



  • ·         Encapsulation- it is the process of hiding all the details of how a piece of software is written and telling only what a programmer needs to know. It is often called information hiding.
  • ·         Polymorphism- it means that one method, used as an instruction, can cause different actions, depending on the kind of object that performs the action.
  • ·         Inheritance- it is a way of organizing classes where the properties can be defined only once and yet applies to a whole collection of classes.





Lesson 2- A Glimpse to Java Programming

Java Programming Language
                It is a programming language originally developed by Sun Microsystem and released in 1995 as a core component of Sun Microsytems’ Java platform. The language derives much of its syntax from c and c++ but has a simpler object model and fewer low-level facilities.  Java applications are typically compiled to byte code that can run on any Java Virtual Machine (JVM) regardless of computer architecture.

History of Java Language
                At year 1991, James Gosling and his team at Sun Microsystems began designing the first version of Java. The first version of Java was intended to be a programming language for home appliances like toasters and TV’s.
                In this appliance language program that the team was designing, and now in the java language that it evolve into programs, ate first translated to an intermediate language that is the same for all appliances. This intermediate language is called Java Byte Coe or simply Byte code.
                In 1994, Gosling realized that his language would be ideal for developing a web browser. The Web browser was produced by Patrick Naughton and Jonathan Payne at Sun Microsystem and has evolved into the browser that is today known as Hot Java. This is the start of Java’s connection to the internet.
                In 1995, Netscape Inc. decided to make the next release of its Netscape(internet) Browser capable of running Java programs. The first public implementation was Java 1.0 in 1995.
                Java quickly become popular. With advent of Java 2, new versions had multiple configurations built for different types of platforms. For example, J2EE was for enterprise applications and the greatly stripped down version J2ME was for mobile applications. J2SE was the designation for the Standard Edition. In 2006, for marketing purposes, new J2 versions were renamed Java EE, Java ME and Java SE, respectively.

Characteristics of Java
The Java Programming language is not just popular because of its being an object oriented programming language. It has features that other languages don’t have, which are the following:
  • Platform Independent- the programs are characterized by write-once-run-anywhere; portable; architecture neural.
  • Simple- the programs are easy to write and debug
  • Object-Oriented – it is characterized by inheritance, encapsulation , polymorphism and dynamic building
  • Robust- it is reliable
  • Distributed- it has networking capability
  • Dynamic- it is designed to adapt to an evolving environment.
  • Secure- it uses public key encryption system to allow the java applications to transmit over the internet in the secure encrypted form.
  • Performance- it uses just-in-time compilation
  • Multithreaded-it means that a single java program that has different threads can execute independently at the same time.
  • Interpreted- programs are run directly from the source code.
Java Byte-Code
  • It is a machine language for a hypothetical computer called Java Virtual Machine(JVM)
  • It is a kind of object code generated by a Java compiler and interpreted by a Java Virtual Machine.
  • It is a platform independent.
Java Virtual Machine
  • It is a software tool that behaves like a high-level language.
  • It is platform independent.
Interpreter
  • It is used to translate byte-code to machine language.
Java Program Execution
  • Compiling the program- use a Just-in-Time (JIT) compiler to translate the Java program into bytecode.
  • Running the program- use the bytecode interpreter to translate each bytecode instruction to machine language and to run the machine language instructions

Kinds of Java Programs
  • Application- it is a regular program meant to be run your computer.
  • Applet- it is a little program meant to be run the internet. They programs that are embedded in other applications, typically in a Web page displayed in a Web developer
Java Platforms
  • Java Card- refers to a technology that allows small Java-based applications (applets) to be run securely on smart cards and similar small memory footprint devices.
  • Java ME (Micro Edition)- it is typical for creating programs intended for small, resource-constraints devices like cellular phone, palm-tops, mp4 players and PDA’s. it specifies several different sets of libraries (known as profiles for devices which are sufficiently limited that supplying the full set of Java libraries would take up unacceptably large amounts of storage.
  • Java SE (Standard Edition)- it is usually intended for workstation environments. It consists of a virtual machine and a set libraries for general purpose use in desktop PCs, servers and similar devices.
  • Java EE (Enterprise Edition)- it targets large distributed enterprises or internet environments that requires server programming.  Java SE plus various APIs useful for multi-tier client-server enterprise applications.


PLATFORM-ECLIPSE



  • Eclipse is a multi-language software development environment comprising an integrated development environment (IDE) and an extensible plug-in system. It is written primarily in Java and can be used to develop applications in Java and, by means of the various plug-ins, in other languages as well, including C, C++, COBOL, Python, Perl, PHP, and others. The IDE is often called Eclipse ADT for Ada, Eclipse CDT for C, Eclipse JDT for Java and Eclipse PDT for PHP.
  • The initial codebase originated from VisualAge. In its default form it is meant for Java developers, consisting of the Java Development Tools (JDT). Users can extend its capabilities by installing plug-ins written for the Eclipse software framework, such as development toolkits for other programming languages, and can write and contribute their own plug-in modules.
  • Released under the terms of the Eclipse Public License, Eclipse is free and open source software.
ECLIPSE VERSION

VERSION
DATE OF RELEASED
Helios
June 23, 2010
Galileo
June 24, 2009
Ganymede
June 25, 2008
Europa
June 29, 2007
Calisto
June 30, 2006
Eclipse 3.1
June 28, 2005
Eclipse 3.0
June 28, 2004

Webmaster Tools