Report swings


Published on

Published in: Technology, Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Report swings

  1. 1. Internet And JavaTopic: - Java SwingsSubmitted To: Submitted By:As Prof. Shruti Jain Alisha Korpal Nivia Jain Sharuti Jain 1|Page
  2. 2. Topic Page no.1 Introduction 32 History 43 Swing Architecture 54 Foundation 6-75 Relationship to AWT 86 Swing Components 97 JFrame 108 JLabel 119 JButton 1210 Example 1311 References 15 2|Page
  3. 3. IntroductionSwing is the primary Java GUI widget toolkit. It is part of Java FoundationClasses (JFC) — an API for providing a graphical user interface (GUI) for Javaprograms.Swing was developed to provide a more sophisticated set of GUI components than theearlier Abstract Window Toolkit. Swing provides a native look and feel that emulates thelook and feel of several platforms, and also supports a pluggable look and feel thatallows applications to have a look and feel unrelated to the underlying platform. It hasmore powerful and flexible components than AWT. In addition to familiar componentssuch as buttons, check box and labels, Swing provides several advanced componentssuch as tabbed panel, scroll panes, trees, tables and lists.Unlike AWT components, Swing components are not implemented by platform-specificcode. Instead they are written entirely in Java and therefore are platform-independent.The term "lightweight" is used to describe such an element. 3|Page
  4. 4. HistoryThe Internet Foundation Classes (IFC) was a graphics library for Java originallydeveloped by Netscape Communications Corporation and first released on December16, 1996.On April 2, 1997, Sun Microsystems and Netscape CommunicationsCorporation announced their intention to incorporate IFC with other technologies to formthe Java Foundation Classes. The "Java Foundation Classes" were later renamed"Swing".Swing introduced a mechanism that allowed the look and feel of every component in anapplication to be altered without making substantial changes to the application code.The introduction of support for a pluggable look and feel allows Swing components toemulate the appearance of native components while still retaining the benefits ofplatform independence. This feature also makes it easy to make an application writtenin Swing look very different from native programs if desired.Originally distributed as a separately downloadable library, Swing has been included aspart of the Java Standard Edition since release 1.2. The Swing classes and componentsare contained in the javax.swing package hierarchy. 4|Page
  5. 5. Swing Architecture • Foundations o Extensible o Customizable o Configurable o Light weight UI • Relationship to AWT 5|Page
  6. 6. FoundationsSwing is platform independent both in terms of expression (Java) and implementation(Look-and-Feel).ExtensibleSwing is a highly partitioned architecture, which allows for the "plugging" of variouscustom implementations of specified framework interfaces: Users can provide their owncustom implementation(s) of these components to override the default implementations.In general, Swing users can extend the framework by extending existing (framework)classes and/or providing alternative implementations of core components.CustomizableGiven the programmatic rendering model of the Swing framework, fine control over thedetails of rendering of a component is possible in Swing. As a general pattern, thevisual representation of a Swing component is a composition of a standard set ofelements, such as a "border", "inset", decorations, etc. Typically, users willprogrammatically customize a standard Swing component (such as a JTable) byassigning specific Borders, Colors, Backgrounds, opacities, etc., as the properties ofthat component. The core component will then use these properties (settings) todetermine the appropriate renderers to use in painting its various aspects. However, it isalso completely possible to create unique GUI controls with highly customized visualrepresentation. 6|Page
  7. 7. ConfigurableSwings heavy reliance on runtime mechanisms and indirect composition patternsallows it to respond at runtime to fundamental changes in its settings. For example, aSwing-based application can change its look and feel at runtime. Further, users canprovide their own look and feel implementation, which allows for uniform changes in thelook and feel of existing Swing applications without any programmatic change to theapplication code.Light weight UISwings configurability is a result of a choice not to use the native host OSs GUIcontrols for displaying itself. Swing "paints" its controls programmatically through theuse of Java 2D APIs, rather than calling into a native user interface toolkit. Thus, aSwing component does not have a corresponding native OS GUI component, and isfree to render itself in any way that is possible with the underlying graphics APIs.However, at its core every Swing component relies on an AWT container, since(Swings) JComponent extends (AWTs) Container. This allows Swing to plug into thehost OSs GUI management framework, including the crucial device/screen mappingsand user interactions, such as key presses or mouse movements. Swing simply"transposes" its own (OS agnostic) semantics over the underlying (OS specific)components. So, for example, every Swing component paints its rendition on thegraphic device in response to a call to component.paint(), which is defined in (AWT)Container. But unlike AWT components, which delegated the painting to their OS-native"heavyweight" widget, Swing components are responsible for their own rendering. 7|Page
  8. 8. Relationship to AWTSince early versions of Java, a portion of the Abstract Window Toolkit (AWT) hasprovided platform-independent APIs for user interface components. In AWT, eachcomponent is rendered and controlled by a native peer component specific to theunderlying windowing system.By contrast, Swing components are often described as lightweight because they do notrequire allocation of native resources in the operating systems windowing toolkit. TheAWT components are referred to as heavyweight components.Much of the Swing API is generally a complementary extension of the AWT rather thana direct replacement. In fact, every Swing lightweight interface ultimately exists withinan AWT heavyweight component because all of the top-level components in Swing(JApplet, JDialog,JFrame, and JWindow) extend an AWT top-level container. Priorto Java 6 Update 10, the use of both lightweight and heavyweight components withinthe same window was generally discouraged due to Z-order incompatibilities. However,later versions of Java have fixed these issues, and both Swing and AWT componentscan now be used in one GUI without Z-order issues.The core rendering functionality used by Swing to draw its lightweight components isprovided by Java 2D, another part of JFC. 8|Page
  9. 9. Java Components 9|Page
  10. 10. JFrameThe components added to the frame are referred to as its contents; these are managedby the content Pane. To add a component to a JFrame, we must use its content Paneinstead. JFrame is a Window with border, title and buttons. When JFrame is set visible,an event dispatching thread is started. JFrame objects store several objects including aContainer object known as the content pane. To add a component to a JFrame, add it tothe content pane.Creating JFrame WindowStep 1: Construct an object of the JFrame class.Step 2: Set the size of the JFrame.Step 3: Set the title of the JFrame to appear in the title bar (title bar will be blank if notitle is set).Step 4: Set the default close operation. When the user clicks the close button, theprogram stops running.Step 5: Make the JFrame visible. 10 | P a g e
  11. 11. JLabelJLabel, descended from JComponent, is used to create text labels.A JLabel object provides text instructions or information on a GUI — display a singleline of read-only text, an image or both text and image.We use a Swing JLabel when we need a user interface component that displays amessage or an image. 11 | P a g e
  12. 12. JButtonA button is a component the user clicks to trigger a specific action.There are several types of buttons in Java, all are subclasses of AbstractButton. • command buttons: is created with class JButton. It generates ActionEvent. • toggle buttons: have on/off or true/false values. • check boxes: a group of buttons. It generates ItemEvent. • radio buttons: a group of buttons in which only one can be selected. It generates ItemEvent. 12 | P a g e
  13. 13. Example:-// Import the swing and AWT classes neededimport java.awt.FlowLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.SwingUtilities;public class SwingExample{ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame f = new JFrame("Swing Example Window"); f.setLayout(new FlowLayout()); f.add(new JLabel("Hello, world!")); f.add(new JButton("Press me!")); f.pack(); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.setVisible(true); } } 13 | P a g e
  14. 14. ); }} 14 | P a g e
  15. 15. References:   Matthew Robinson, Pavel Vorobiev: Swing, Second Edition, Manning, ISBN 1-930110-88-X  =1&oq=swings+in+java&aq=f&aqi=g5&aql=&gs_sm=e&gs_upl=170399l176171l0 l176515l21l14l1l0l0l2l1764l7815l4-,or.r_gc.r_pw.&fp=cc63 231dd3ba60f6&biw=1280&bih=869  downloads/JavaSwing/Java %2520Swing.ppt+swings+in+java+ppt&hl=en&gl=in&pid=bl&srcid=ADGEESiEz8 NwezOYwyipXPonXBC0nWcz1oAqRU3dKPsqMlEJzV9_ZTtfxoyBy3gyROz859X KJYIoS7867YIKWIztvGsvf1wG68QBiyaV65GYPYkeDFfJ8XujEaoQOFWJF- vmzmGZe0VH&sig=AHIEtbRatSwTaKEOa7DH8iV2tFaZPyCAbQ 15 | P a g e