What is Java Swing?
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.
What are the new features of Java Swing?
- Pluggable Look-and-Feels
- Lightweight Components: New components like tables, trees, sliders, progress bars, internal frames, and text components.
- Swing components contain support for replacing their insets with an arbitrary number of concentric borders.
- Swing components can have tooltips placed over them. A tooltip is a textual popup that momentarily appears when the mouse cursor rests inside the component’s painting region. Tooltips can be used to give more information about the component in question.
- You can arbitrarily bind keyboard events to components, defining how they will react to various keystrokes under given conditions.
- There is additional debugging support for the rendering of your own lightweight Swing components.
How are the elements of different layouts organized?
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.
What are the differences between Java AWT and Java Swing?
Java AWT:
- It is platform dependent
- AWT components are heavy-weight,
- It doesn’t support pluggable look and feel.
- IT doesn’t follow MVC
- IT provides fewer components than swing
Java Swing:
- It is platform independent
- Swing components are light-weight,
- It supports pluggable look and feel
- Swing follows MVC pattern
- It provides more powerful components like tables, lists, scroll panes, color chooser, tabbed pane, and text areas to display HTTP or rich text format (RTF)
What are the different Swing Components?
Various components classes used in this topic are as follows
- Abstract Button-Abstract super class for swing buttons.
- Button Group-Encapsulates a mutually exclusive set of buttons.
- Image Icon-Encapsulates in icon.
- Japplet-The swing version of applet.
- JButton-Class of swing push button.
- JCheck Box-Class of swing check button.
- JCombo Box-Encapsulates a combo box.
- JLabel : The swing version of a label.
- JRadio Button:The swing version of a radio button.
- JScroll Pane:Encapsulates a scroll pane window.
- JTabbed Pane:Encapsulates a tabbed pane window.
- JTable:Encapsulates a table-based control.
- JText Field:The swing version of a text field.
- JTree:Encapsulates a tree based control.
What is JFC?
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.
What is a Container in a GUI?
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.
What is the heavyweight component?
Heavy weight components like Abstract Window Toolkit (AWT) depend on the local windowing toolkit. For example, java.awt .Button is a heavy weight component.
What is the Lightweight 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).
What is AWT?
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
What is JButton?
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.
What is JLabel?
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.
What is Double Buffering?
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.
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.
What method is used to specify a container’s layout?
The setLayout() method is used to specify a container’s layout. For example, setLayout(new FlowLayout()); will be set the layout as FlowLayout.
How can a GUI component handle its own events?
A component can handle its own events by implementing the required event-listener interface and adding itself as its own event listener.
Why should any swing call back implementation execute quickly?
Callbacks are invoked by the event dispatch thread. Event dispatch thread blocks processing of other events as long as call back method executes.
What is the function of the paint() and repaint() methods?
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
What is the preferred size of a component?
The preferred size of a component is the minimum component size that will allow the component to display normally.
What is the use of the enableEvents() method?
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.
What are the Layoutmanagers in Java?
- Flow Layout Manager
- Grid Layout Manager
- Box Layout Manager
- Border Layout Manager
- Card Layout Manager
- GridBag Layout Manager.
What is the difference between invokeandwait() and invokelater()?
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.
What is the difference between applications and applets?
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.
Can you explain TransferHandler Class?
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
What is Realized?
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 :
- SetVisible(true).
- Show() .
- Pack().
Which is the super class of all event classes?
The java.awt.AWTEvent class is the highest¬level class in the AWT event¬class hierarchy.
What is the default layout for a content pane in JFC?
The Defaulf Layout for the ConteutPane in JFC is that :
BorderLayout.
What is the lifecycle of an applet?
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
What are the advantages of the event-delegation model over the event-inheritance model?
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.
Can you explain Serialization in Swing?
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
How the canvas class and the graphics class are related?
A Canvas object provides access to a Graphics object via its paint () method.
Can you explain Internal Frame in Swing?
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.
Can you explain the Swing delegation event model?
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.