Swing used in java

Introduction to Java Swing

Swing is a Java Foundation Classes [JFC] library and an extension of the Abstract Window Toolkit [AWT]. Swing offers much-improved functionality over AWT, new components, expanded components features, and excellent event handling with drag-and-drop support.

Introduction of Java Swing

Swing has about four times the number of User Interface [UI] components as AWT and is part of the standard Java distribution. By today’s application GUI requirements, AWT is a limited implementation, not quite capable of providing the components required for developing complex GUI’s required in modern commercial applications. The AWT component set has quite a few bugs and really does take up a lot of system resources when compared to equivalent Swing resources. Netscape introduced its Internet Foundation Classes [IFC] library for use with Java. Its Classes became very popular with programmers creating GUI’s for commercial applications.

  • Swing is a Set Of API ( API- Set Of Classes and Interfaces )
  • Swing is Provided to Design Graphical User Interfaces
  • Swing is an Extension library to the AWT (Abstract Window Toolkit)
  • Includes New and improved Components that have been enhancing the looks and Functionality of GUIs’
  • Swing can be used to build(Develop) The Standalone swing GUI Apps Also as Servlets And Applets
  • It Employs model/view design architecture
  • Swing is more portable and more flexible than AWT, The Swing is built on top of the AWT
  • Swing is Entirely written in Java
  • Java Swing Components are Platform-independent And The Swing Components are lightweight
  • Swing Supports a Pluggable look and feels And Swing provides more powerful components
  • such as tables, lists, Scrollpanes, Colourchooser, tabbedpane, etc
  • Further Swing Follows MVC.

Many programmers think that JFC and Swing are one and the same thing, but that is not so.

JFC contains Swing [A UI component package] and quite a number of other items:

  • Cut and paste: Clipboard support
  • Accessibility features: Aimed at developing GUI’s for users with disabilities
  • The Desktop Colors Features Has been Firstly introduced in Java 1.1
  • Java 2D: it has Improved colors, images, and also texts support

Features Of Swing Class

  • Pluggable look and feel
  • Uses MVC architecture
  • Lightweight Components
  • Platform Independent
  • Advanced features such as JTable, JTabbedPane, JScollPane, etc.
  • Java is a platform-independent language and runs on any client machine, the GUI look and feel, owned and delivered by a platform-specific O/S, simply does not affect an application’s GUI constructed using Swing components
  • Lightweight Components: Starting with the JDK 1.1, its AWT-supported lightweight component development. For a component to qualify as lightweight, it must not depend on any non-Java [O/s based) system classes. Swing components have their own view supported by Java’s look and feel classes.
  • Pluggable Look and Feel: This feature enables the user to switch the look and feel of Swing components without restarting an application. The Swing library supports components’ look and feels that remain the same across all platforms wherever the program runs. The Swing library provides an API that gives real flexibility in determining the look and feel of the GUI of an application
  • Highly customizable – Swing controls can be customized in a very easy way as visual appearance is independent of internal representation.
  • Rich controls– Swing provides a rich set of advanced controls like Tree TabbedPane, slider, colorpicker, and table controls.
Читайте также:  Butify html vs code

Swing Classes Hierarchy

The MVC Connection

  • In general, a visual component is a composite of three distinct aspects:
    1. The way that the component looks when rendered on the screen
    2. The way such that the component reacts to the user
    3. The state information associated With the component
  • Over the years, one component architecture has proven itself to be exceptionally effective:- Model-View-Controller or MVC for short.
  • In MVC terminology, the model corresponds to the state information associated with the Component
  • The view determines how the component is displayed on the screen, including any aspects of the view that are affected by the current state of the model.
  • The controller determines how the component reacts to the user

The simplest Swing components have capabilities far beyond AWT components as follows:

  • Swing buttons and labels can be displaying images instead of or in addition to text
  • The borders around most Swing components can be changed easily. For example, it is easy to put a 1 pixel border around the outside of a Swing label
  • Swing components do not have to be rectangular. Buttons, for example, can be round
  • Now The Latest Assertive technologies such as screen readers can easily get information from Swing components. For example: A screen reader tool can easily capture the text that is displayed on a Swing button or label

Example 1: Develop a program using label (swing) to display message “GFG WEB Site Click”;

Java

Example 2: Write a program to create three buttons with caption OK , SUBMIT, CANCLE.

Java

Java

Components of Swing Classthe task’s percentage

Class Description
Component A Component is the Abstract base class for about the non menu user-interface controls of SWING. Components are represents an object with a graphical representation
Container A Container is a component that can container SWING Components
JComponent A JComponent is a base class for all swing UI Components In order to use a swing component that inherits from JComponent, component must be in a containment hierarchy whose root is a top-level Swing container
JLabel A JLabel is an object component for placing text in a container
JButton This class creates a labeled button
JColorChooser A JColorChooser provides a pane of controls designed to allow the user to manipulate and select a color
JCheckBox A JCheckBox is a graphical(GUI) component that can be in either an on-(true) or off-(false) state
JRadioButton The JRadioButton class is a graphical(GUI) component that can be in either an on-(true) or off-(false) state. in the group
JList A JList component represents the user with the scrolling list of text items
JComboBox A JComboBox component is Presents the User with a show up Menu of choices
JTextField A JTextField object is a text component that will allow for the editing of a single line of text
JPasswordField A JPasswordField object it is a text component specialized for password entry
JTextArea A JTextArea object is a text component that allows for the editing of multiple lines of text
Imagelcon A ImageIcon control is an implementation of the Icon interface that paints Icons from Images
JScrollbar A JScrollbar control represents a scroll bar component in order to enable users to Select from range values
JOptionPane JOptionPane provides set of standard dialog boxes that prompt users for a value or Something
JFileChooser A JFileChooser it Controls represents a dialog window from which the user can select a file.
JProgressBar As the task progresses towards completion, the progress bar displays the tasks percentage on its completion
JSlider A JSlider this class is lets the user graphically(GUI) select by using a value by sliding a knob within a bounded interval.
JSpinner A JSpinner this class is a single line input where the field that lets the user select by using a number or an object value from an ordered sequence
Читайте также:  Все checked исключения java

Источник

Package javax.swing

Provides a set of «lightweight» (all-Java language) components that, to the maximum degree possible, work the same on all platforms. For a programmer’s guide to using these components, see Creating a GUI with JFC/Swing, a trail in The Java Tutorial. For other resources, see Related Documentation.

Swing’s Threading Policy

In general Swing is not thread safe. All Swing components and related classes, unless otherwise documented, must be accessed on the event dispatching thread.

Typical Swing applications do processing in response to an event generated from a user gesture. For example, clicking on a JButton notifies all ActionListeners added to the JButton . As all events generated from a user gesture are dispatched on the event dispatching thread, most developers are not impacted by the restriction.

Where the impact lies, however, is in constructing and showing a Swing application. Calls to an application’s main method, or methods in Applet , are not invoked on the event dispatching thread. As such, care must be taken to transfer control to the event dispatching thread when constructing and showing an application or applet. The preferred way to transfer control and begin working with Swing is to use invokeLater . The invokeLater method schedules a Runnable to be processed on the event dispatching thread. The following two examples work equally well for transferring control and starting up a Swing application:

import javax.swing.SwingUtilities; public class MyApp implements Runnable < public void run() < // Invoked on the event dispatching thread. // Construct and show GUI. >public static void main(String[] args) < SwingUtilities.invokeLater(new MyApp()); >>
import javax.swing.SwingUtilities; public class MyApp < MyApp(String[] args) < // Invoked on the event dispatching thread. // Do any initialization here. >public void show() < // Show the UI. >public static void main(final String[] args) < // Schedule a job for the event-dispatching thread: // creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() < public void run() < new MyApp(args).show(); >>); > >

This restriction also applies to models attached to Swing components. For example, if a TableModel is attached to a JTable , the TableModel should only be modified on the event dispatching thread. If you modify the model on a separate thread you run the risk of exceptions and possible display corruption.

Читайте также:  Selenium python parse table

Although it is generally safe to make updates to the UI immediately, when executing on the event dispatch thread, there is an exception : if a model listener tries to further change the UI before the UI has been updated to reflect a pending change then the UI may render incorrectly. This can happen if an application installed listener needs to update the UI in response to an event which will cause a change in the model structure. It is important to first allow component installed listeners to process this change, since there is no guarantee of the order in which listeners may be called. The solution is for the application listener to make the change using SwingUtilities.invokeLater(Runnable) so that any changes to UI rendering will be done post processing all the model listeners installed by the component.

As all events are delivered on the event dispatching thread, care must be taken in event processing. In particular, a long running task, such as network io or computational intensive processing, executed on the event dispatching thread blocks the event dispatching thread from dispatching any other events. While the event dispatching thread is blocked the application is completely unresponsive to user input. Refer to SwingWorker for the preferred way to do such processing when working with Swing.

More information on this topic can be found in the Swing tutorial, in particular the section on Concurrency in Swing.

Источник

Оцените статью