1 of 30

Unit 5

JAR & Swing

2 of 30

JAR file format creation

JAR (Java ARchive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file for distribution. JAR files are archive files that include a Java-specific manifest file

3 of 30

How to Create a Jar File in Java

  1. Open your text editor and type in the following Java statements. ...
  2. Save your file as CreateAJarFile.java .
  3. Open a command prompt and navigate to the directory containing your Java program. ...
  4. Test your program prior to placing it in a jar file. ...
  5. Now you will create a jar file containing your program

4 of 30

How to make an executable jar file in Java

The jar (Java Archive) tool of JDK provides the facility to create the executable jar file. An executable jar file calls the main method of the class if you double click it.

To create the executable jar file, you need to create .mf file, also known as manifest file.

Creating manifest file

To create manifest file, you need to write Main-Class, then colon, then space, then classname then enter. For example:

myfile.mf

  1. Main-Class: First  

As you can see, the mf file starts with Main-Class colon space class name. Here, class name is First.

In mf file, new line is must after the class name.

5 of 30

Java Archive (JAR) Files

Java Archive (JAR) is a platform-independent file format that allow you to compress and bundle multiple files associated with a Java application, applet, or WebStart application into a single file. JAR is based on the popular ZIP algorithm, and mimic the Unix's tar (or tape archive) file format (e.g., jar and tar tools have the same command-line options).

JAR files provide the following functions:

  1. Data compression (using the ZIP algorithm).
  2. Ease in distribution: All files in a Java package can be placed in a single file to facilitate distribution. Furthermore, transfer one big file over the network instead of many small files is faster and more efficient because it involves less overhead.
  3. Authentication: JAR file can be digitally signed by its author. You can authenticate the author of the JAR file by checking the signature and the author's digital certificate.

The Java Runtime (JRE) (or Java applications) can load classes from the JAR file directly, without un-jarring the file. Furthermore, the JAR files use the same file format as ZIP files, and can be opened and manipulated via ZIP programs such as WinZIP or WinRAR.

6 of 30

Creating JAR File

  1. Via the command-line jar tool: JDK provides a command-line tool called "jar.exe", to create, manage and manipulate JAR files. The jar tool reference is available at http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jar.html.
  2. Via the "export" option of IDE such as Eclipse or NetBeans, in practice.

Needless to say, using IDE to create a JAR file is much more easier than the command-line jar tool.

7 of 30

Create JAR File using Command-Line "jar" Tool

Creating JAR file using command-line jar tool is clumpy! It is presented here for your basic understanding and completeness. In practice, you should use your IDE (such as Eclipse/NetBeans) to create JAR file instead (which are presented in the following sections).

Syntax

To create a JAR file using the jar tool, issue the jar command (on CMD shell) with 'c' option:

> jar cvf jarFile inputFileDir1 inputFileDir2 ...

where:

  • 'c' option indicates that you want to create a new JAR file;
  • 'v' option asks for verbose mode, which displays information messages;
  • 'f' specifies that the output go to a file specified in jarfile (instead of the standard output by default). Option 'f' and jarfile are a pair.
  • inputFileDir give the input filenames or directory names for the files to be jarred. Multiple names are separated by space. The name may contain wildcard '*'.

Example

For example, suppose that "images" is an sub-directory under the current directory, the following command jar-up all the ".class" files of the current directory and the entire "images" sub-directory into "hello.jar". The 'v' option instructs the tool to produce the information messages.

8 of 30

Internationalization

Internationalization is also abbreviated as I18N because there are total 18 characters between the first letter 'I' and the last letter 'N'.

Internationalization is a mechanism to create such an application that can be adapted to different languages and regions.

Internationalization is one of the powerful concept of java if you are developing an application and want to display messages, currencies, date, time etc. according to the specific region or language.

9 of 30

Localization

Localization is also abbreviated as I10N because there are total 10 characters between the first letter 'L' and last letter 'N'. Localization is the mechanism to create such an application that can be adapted to a specific language and region by adding locale-specific text and component.

10 of 30

Understanding the culturally dependent data before starting internationalization

Before starting the internationalization, Let's first understand what are the informations that differ from one region to another. There is the list of culturally dependent data:

  • Messages
  • Dates
  • Times
  • Numbers
  • Currencies
  • Measurements
  • Phone Numbers
  • Postal Addresses
  • Labels on GUI components etc.

11 of 30

Fields of Locale class

There are fields of Locale class:

  • public static final Locale ENGLISH
  • public static final Locale FRENCH
  • public static final Locale GERMAN
  • public static final Locale ITALIAN
  • public static final Locale JAPANESE
  • public static final Locale KOREAN
  • public static final Locale CHINESE
  • public static final Locale SIMPLIFIED_CHINESE
  • public static final Locale TRADITIONAL_CHINESE
  • public static final Locale FRANCE
  • public static final Locale GERMANY
  • public static final Locale ITALY
  • public static final Locale JAPAN
  • public static final Locale KOREA
  • public static final Locale CHINA
  • public static final Locale PRC
  • public static final Locale TAIWAN
  • public static final Locale UK
  • public static final Locale US
  • public static final Locale CANADA
  • public static final Locale CANADA_FRENCH
  • public static final Locale ROOT

12 of 30

Constructors of Locale class

There are three constructors of Locale class.They are as follows:

  1. Locale(String language)
  2. Locale(String language, String country)
  3. Locale(String language, String country, String variant)

Commonly used methods of Locale class

There are given commonly used methods of Locale class.

  1. public static Locale getDefault() it returns the instance of current Locale
  2. public static Locale[] getAvailableLocales() it returns an array of available locales.
  3. public String getDisplayCountry() it returns the country name of this locale object.
  4. public String getDisplayLanguage() it returns the language name of this locale object.
  5. public String getDisplayVariant() it returns the variant code for this locale object.
  6. public String getISO3Country() it returns the three letter abbreviation for the current locale's country.
  7. public String getISO3Language() it returns the three letter abbreviation for the current locale's language.

13 of 30

Example of Local class that prints the informations of the default locale

  1. import java.util.*;
  2. public class LocaleExample {
  3. public static void main(String[] args) {
  4. Locale locale=Locale.getDefault();
  5. //Locale locale=new Locale("fr","fr");//for the specific locale
  6. System.out.println(locale.getDisplayCountry());
  7. System.out.println(locale.getDisplayLanguage());
  8. System.out.println(locale.getDisplayName());
  9. System.out.println(locale.getISO3Country());
  10. System.out.println(locale.getISO3Language());
  11. System.out.println(locale.getLanguage());
  12. System.out.println(locale.getCountry());
  13. }
  14. }

Output:United States

English

English (United States)

USA

eng

en

US

14 of 30

Example of Local class that prints english in different languages

  1. import java.util.*;
  2. public class LocaleExample2 {
  3. public static void main(String[] args) {
  4. Locale enLocale = new Locale("en", "US");
  5. Locale frLocale = new Locale("fr", "FR");
  6. Locale esLocale = new Locale("es", "ES");
  7. System.out.println("English language name (default): " +
  8. enLocale.getDisplayLanguage());
  9. System.out.println("English language name in French: " +
  10. enLocale.getDisplayLanguage(frLocale));
  11. System.out.println("English language name in spanish: " +
  12. enLocale.getDisplayLanguage(esLocale));
  13. }
  14. }

15 of 30

Example of Local class that print display language of many locales

  1. import java.util.*;
  2. public class LocaleEx {
  3. public static void main(String[] args) {
  4. Locale[] locales = { new Locale("en", "US"),
  5. new Locale("es", "ES"), new Locale("it", "IT") };
  6. for (int i=0; i< locales.length; i++) {
  7. String displayLanguage = locales[i].getDisplayLanguage(locales[i]);
  8. System.out.println(locales[i].toString() + ": " + displayLanguage);
  9. }
  10. }
  11. }

Output:en_US: English

es_ES: espa?ol

it_IT: italiano

16 of 30

Swing Programming

Java Swing is a part of Java Foundation Classes (JFC) that is used to create window-based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in java.

Unlike AWT, Java Swing provides platform-independent and lightweight components.

The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.

17 of 30

No.

Java AWT

Java Swing

1)

AWT components are platform-dependent.

Java swing components are platform-independent.

2)

AWT components are heavyweight.

Swing components are lightweight.

3)

AWT doesn't support pluggable look and feel.

Swing supports pluggable look and feel.

4)

AWT provides less components than Swing.

Swing provides more powerful components such as tables, lists, scrollpanes, colorchooser, tabbedpane etc.

5)

AWT doesn't follows MVC(Model View Controller) where model represents data, view represents presentation and controller acts as an interface between model and view.

Swing follows MVC.

18 of 30

Java AWT

  • AWT components are platform-dependent.
  • AWT components are heavyweight.
  • AWT doesn't support pluggable look and feel.
  • AWT provides less components than Swing.
  • AWT doesn't follows MVC(Model View Controller) where model represents data, view represents presentation and controller acts as an interface between model and view.

Java Swing

  • Java swing components are platform-independent.
  • Swing components are lightweight.
  • Swing supports pluggable look and feel.
  • Swing provides more powerful components such as tables, lists, scrollpanes, colorchooser, tabbedpane etc.
  • Swing follows MVC.

19 of 30

  1. import javax.swing.*;  
  2. public class FirstSwingExample {  
  3. public static void main(String[] args) {  
  4. JFrame f=new JFrame();//creating instance of JFrame  
  5. JButton b=new JButton("click");//creating instance of JButton  
  6. b.setBounds(130,100,10040);//x axis, y axis, width, height  
  7. f.add(b);//adding button in JFrame  
  8. f.setSize(400,500);//400 width and 500 height  
  9. f.setLayout(null);//using no layout managers  
  10. f.setVisible(true);//making the frame visible  
  11. }  
  12. }  

20 of 30

JQuery: Introduction-

jQuery tutorial for beginners and professionals provides deep knowledge of jQuery technology. Our jQuery tutorial will help you to learn jQuery fundamentals, example, selectors, events, effects, traversing, CSS and attributes.

What is jQuery

  • jQuery is a small and lightweight JavaScript library.
  • jQuery is cross-platform.
  • jQuery means "write less do more".
  • jQuery simplifies AJAX call and DOM manipulation.

21 of 30

jQuery Example

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.  <title>First jQuery Example</title>  
  5. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">  
  6.  </script>  
  7.  <script type="text/javascript" language="javascript">  
  8.  $(document).ready(function() {  
  9.  $("p").css("background-color", "pink");  
  10.  });  
  11.  </script>  
  12.  </head>  
  13. <body>  
  14. <p>This is first paragraph.</p>  
  15. <p>This is second paragraph.</p>  
  16. <p>This is third paragraph.</p>  
  17. </body>  
  18. </html>  

Test it Now

Output:

This is first paragraph.

This is second paragraph.

This is third paragraph.

22 of 30

Many of the biggest companies on the Web use jQuery, such as:

  • Google
  • Microsoft
  • IBM
  • Netflix

23 of 30

There are several ways to start using jQuery on your web site. You can:

  • Download the jQuery library from jQuery.com
  • Include jQuery from a CDN, like Google

The jQuery library is a single JavaScript file, and you reference it with the HTML <script> tag (notice that the <script> tag should be inside the <head> section):

<head>�<script src="jquery-3.3.1.min.js"></script>�</head>

24 of 30

Syntax

Description

Example

$("*")

Selects all elements

$(this)

Selects the current HTML element

$("p.intro")

Selects all <p> elements with class="intro"

$("p:first")

Selects the first <p> element

$("ul li:first")

Selects the first <li> element of the first <ul>

$("ul li:first-child")

Selects the first <li> element of every <ul>

$("[href]")

Selects all elements with an href attribute

$("a[target='_blank']")

Selects all <a> elements with a target attribute value equal to "_blank"

$("a[target!='_blank']")

Selects all <a> elements with a target attribute value NOT equal to "_blank"

$(":button")

Selects all <button> elements and <input> elements of type="button"

$("tr:even")

Selects all even <tr> elements

$("tr:odd")

Selects all odd <tr> elements

25 of 30

jQuery Animations - The animate() Method

The jQuery animate() method is used to create custom animations.

Syntax:

$(selector).animate({params},speed,callback);

The required params parameter defines the CSS properties to be animated.

The optional speed parameter specifies the duration of the effect. It can take the following values: "slow", "fast", or milliseconds.

The optional callback parameter is a function to be executed after the animation completes.

The following example demonstrates a simple use of the animate() method; it moves a <div> element to the right, until it has reached a left property of 250px:

Example

$("button").click(function(){�  $("div").animate({left: '250px'});�}); 

26 of 30

jQuery animate() - Manipulate Multiple Properties

Notice that multiple properties can be animated at the same time:

Example

$("button").click(function(){� $("div").animate({�   left: '250px',�   opacity: '0.5',�   height: '150px',�   width: '150px'�  });�}); 

27 of 30

jQuery animate() - Using Relative Values

It is also possible to define relative values (the value is then relative to the element's current value). This is done by putting += or -= in front of the value:

Example

$("button").click(function(){�  $("div").animate({�    left: '250px',�   height: '+=150px',�   width: '+=150px'�  });�}); 

28 of 30

jQuery animate() - Using Pre-defined Values

You can even specify a property's animation value as "show", "hide", or "toggle":

Example

$("button").click(function(){� $("div").animate({�   height: 'toggle'� });�}); 

29 of 30

jQuery animate() - Uses Queue Functionality

By default, jQuery comes with queue functionality for animations.

This means that if you write multiple animate() calls after each other, jQuery creates an "internal" queue with these method calls. Then it runs the animate calls ONE by ONE.

So, if you want to perform different animations after each other, we take advantage of the queue functionality:

Example 1

$("button").click(function(){�  var div = $("div");�  div.animate({height: '300px', opacity: '0.4'}, "slow");�  div.animate({width: '300px', opacity: '0.8'}, "slow");�  div.animate({height: '100px', opacity: '0.4'}, "slow");�  div.animate({width: '100px', opacity: '0.8'}, "slow");�}); 

30 of 30

The example below first moves the <div> element to the right, and then increases the font size of the text:

Example 2

$("button").click(function(){�  var div = $("div");�  div.animate({left: '100px'}, "slow");�  div.animate({fontSize: '3em'}, "slow");�});