Swing is a GUI (Graphical User Interface) widget toolkit for Java. It is part of Oracle’s JFC (Java Foundation Classes) an API for providing a GUI for Java programs. It provides a richer set of components than AWT. Swing is an advanced GUI toolkit. It has a rich set of widgets. From basic widgets like buttons, labels, scrollbars to advanced widgets like trees and tables. Swing itself is written in Java.
Java Swing is also known as Java GUI widget toolkit. It is part of JFC (Java Foundation Classes) which was designed for enabling large-scale enterprise development of Java applications. Swing API is a set of extensible GUI Components to ease the developer’s life to create JAVA based Front End/GUI Applications. A layout manager is an object that is used to organize components in a container. The different layouts available are FlowLayout: The elements of a FlowLayout are organized in a top to bottom, left to right fashion. BorderLayout: The elements of a BorderLayout are organized at the borders (North, South, East and West) and the center of a container. CardLayout: The elements of a CardLayout are stacked, on top of the other, like a deck of cards. GridLayout: The elements of a GridLayout are of equal size and are laid out using the square of a grid. GridBagLayout: The elements of a GridBagLayout are organized according to a grid. However, the elements may be different sizes and may occupy more than one row or column of the grid. In addition, the rows and columns may have different sizes. Java AWT: Java Swing: Various components classes used in this topic are as follows The Java Foundation Classes (JFC) are a set of Java class libraries provided as part of Java 2 Platform, Standard Edition (J2SE) to support building graphics user interface (GUI) and graphics functionality for client applications that will run on popular platforms such as Microsoft Windows, Linux, and Mac OSX. A Container contains and arranges other components (including other containers) through the use of layout managers, which use specific layout policies to determine where components should go as a function of the size of the container. Some container classes are Window, Frame, Dialog, FileDialog, Panel, Applet, or ScrollPane. Heavy weight components like Abstract Window Toolkit (AWT) depend on the local windowing toolkit. For example, java.awt .Button is a heavy weight component. A lightweight component is one that “borrows” the screen resource of an ancestor (which means it has no native resource of its own — so it’s lighter). AWT (Abstract Window Toolkit) enables programmers to develop Java applications with GUI components, such as windows, and buttons. The Java Virtual Machine (JVM) is responsible for translating the AWT calls into the appropriate calls to the host operating system The JButton class is used to create a labelled button that has platform independent implementation. The application result in some action when the button is pushed. It inherits AbstractButton class. The object of JLabel class is a component for placing text in a container. It is used to display a single line of read only text. The text can be changed by an application but a user cannot edit it directly. It inherits JComponent class. Double buffering is the process of use of two buffers rather than one to temporarily hold data being moved to and from an I/O device. Double buffering increases data transfer speed because one buffer can be filled while the other is being emptied. 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. The setLayout() method is used to specify a container’s layout. For example, setLayout(new FlowLayout()); will be set the layout as FlowLayout. A component can handle its own events by implementing the required event-listener interface and adding itself as its own event listener. Callbacks are invoked by the event dispatch thread. Event dispatch thread blocks processing of other events as long as call back method executes. The paint() method supports painting via a Graphics object. The repaint() method is used to cause paint() to be invoked by the AWT painting thread The preferred size of a component is the minimum component size that will allow the component to display normally. The enableEvents() method is used to enable an event for a particular component. Normally, an event is enabled when a listener is added to an object for a particular event. The enableEvents() method is used by objects that handle events by overriding their event-dispatch methods. invokeAndWait() method in swing is synchronous. It blocks until Runnable task is complete. InvokeLater() method in swing is asynchronous. It posts an action event to the event queue and returns immediately. It will not wait for the task to complete. Application must be run explicitly within Java Virtual Machine whereas applet loads and runs itself automatically in a java-enabled browser. Application starts execution with its main method whereas applet starts execution with its init method. Application can run with or without graphical user interface whereas applet must run within a graphical user interface. In order to run an applet we need a java enabled web browser or an appletviewer. TransferHandler class is used for data transfer and it provides an easy way to transfer the data from the Jcomponent. All the details of the data are contained in the class and it supports the classes as well. The Components are provided with the default handler that can create own handler on their own. There are three methods of TransferHandler class and these are as follows: setDragEnabled(boolean): This turns the drag support on and it defines the component to support the drag facility and links to the list given by Jlist. setDropMode(DropMode):It is used to configure the drop locations and determines the use of Jlist, Jtable, Jtree. setTransferHandler(TransferHandler):It is used to allow the plug in data to be imported or exported by using the Jcomponent Realized mean basically is component, which has been painted on screen or that is ready to be painted. Realization can take place by invoking any of these methods. Which are following as : The java.awt.AWTEvent class is the highest¬level class in the AWT event¬class hierarchy. The Defaulf Layout for the ConteutPane in JFC is that : BorderLayout. init( ) method – called when an applet is first loaded start( ) method – called each time an applet is started paint( ) method – called when the applet is minimized or maximized stop( ) method – called when the browser moves off the applet’s page destroy( ) method – called when the browser is finished with the applet Event-delegation model has two advantages over event-inheritance model. a)Event delegation model enables event handling by objects other than the ones that generate the events. This allows a clean separation between a component’s design and its use. b)It performs much better in applications where many events are generated. This performance improvement is due to event-delegation model does not have to be repeatedly process unhandle events as is the case of the event-inheritance. Serialization is used when an object extends the Jcomponent class. The object’s data in this case points out to the stream that is written or serialized and gives an output stream. This output stream can be sent through then network and it can be saved in the file if required. The serialized file can be de-serialized in the memory. This will allow the operation to continue to perform the original state functions that were getting used. The serialization used in object allows easy retrieval of the object data and easy transmission of state data from one place to another. This can also be placed in custom made storage files that provide the transfer of the components to be fast and it uses the concept of virtual machine that transfers from one machine to another machine. It uses the remote method invocation to allow the use of distributed computing that provide with some exceptions to the use of transient keyword. The components can be serialized using the classes that can pass the reference to an object using the writeObject() method of the ObjectOutputStream object this way it calls the serialization function recursively A Canvas object provides access to a Graphics object via its paint () method. Internal frame allow one window to be displayed within another window. Adding of internal frame can be used to the desktop pane or the content pane. The user interface allows choosing the base of the application that is surrounded around the frames. The frames can be switched and other frames can be chosen over the other one. This internal frame has the same function that is provided in the normal Frame object, this object is confined inside the visible area of the container. The internal frame can have icons that can be used inside the application frame and have the properties of maximize and minimize so that the frame consists of main application frame. It closes the frame using the standard controls that is given for a popup window. Internal frame can be given in a layer that uses the display function to display the relative to the other internal frames. In this model, the objects that receive user events notify the registered listeners of the user activity. In most cases the event receiver is a component. Event Types: ActionEvent, KeyEvent, MouseEvent, WindowEvent etc. Event Processors: JButton, JList etc. EventListeners: ActionListener, ComponentListener, KeyListener etc.What is Java Swing?
What are the new features of Java Swing?
How are the elements of different layouts organized?
What are the differences between Java AWT and Java Swing?
What are the different Swing Components?
What is JFC?
What is a Container in a GUI?
What is the heavyweight component?
What is the Lightweight Component?
What is AWT?
What is JButton?
What is JLabel?
What is Double Buffering?
How to make an executable jar file in Java
What method is used to specify a container’s layout?
How can a GUI component handle its own events?
Why should any swing call back implementation execute quickly?
What is the function of the paint() and repaint() methods?
What is the preferred size of a component?
What is the use of the enableEvents() method?
What are the Layoutmanagers in Java?
What is the difference between invokeandwait() and invokelater()?
What is the difference between applications and applets?
Can you explain TransferHandler Class?
What is Realized?
Which is the super class of all event classes?
What is the default layout for a content pane in JFC?
What is the lifecycle of an applet?
What are the advantages of the event-delegation model over the event-inheritance model?
Can you explain Serialization in Swing?
How the canvas class and the graphics class are related?
Can you explain Internal Frame in Swing?
Can you explain the Swing delegation event model?
Related posts:
- Core Java Interview Questions and Answers
- Hibernate Interview Questions and Answers 2021
- Java Multithreading Interview Question and Answers
- Java Threading Interview Questions and Answers
- JAX-WS Interview Questions and Answers
- JDBC Interview Questions and Answers
- JSF Interview Questions and Answers
- Portal and Portlet Interview Questions and Answers