Top 48 Java Swing Interview Questions You Must Prepare 21.Apr.2024

Converter is basically an application that converts distance measurements between metric and U.S units.

JFC supports robust and portable user interfaces. The Swing classes are robust, compatible with AWT, and provide you with a great deal of control over a user interface. Since source code is available, it is relatively easy to extend the JFC to do exactly what you need it to do. But the number of third­party controls written for Swing is still relatively small.
WFC runs only on the Windows (32­bit) user interface, and uses Microsoft extensions to Java for event handling and ActiveX integration. Because ActiveX components are available to WFC programs, there are theoretically more controls available for WFC than for JFC. In practice, however, most ActiveX vendors do not actively support WFC, so the number of controls available for WFC is probably smaller than for JFC. The WFC programming model is closely aligned with the Windows platform.

An event handler is a part of a computer program created to tell the program how to act in response to a specific event.

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 unhandled events as is the case of the event-inheritance.

getParameters()
Method is used by the applet to recognize the height and width.

There are two different types of applets. that are defined as :

  1.  Trusted Applets.
  2. Untrusted applets.

Trusted Applets : are applets with predefined security.
Untrusted Applets : are applets without any security.

 

Changing the state of an object is called an event. An event is an event object that describes a state of change. In other words, event occurs when an action is generated, like pressing a key on keyboard, clicking mouse, etc. There different types of models for handling events are event-inheritance model and event-delegation model

The key difference is that JSF runs on server, It needs a server like Tomcat or WebLogic or WebSphere. It displays HTML to the client. But Swing program is a stand alone application.

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().

The Frame class extends Window to define a main application window that can have a menu bar. A window can be model.

Callbacks are invoked by the event dispatch thread. Event dispatch thread blocks processing of other events as long as call back method executes.

This method is used to ensure that Swing components are updated through the event­dispatching thread.

The Elements of a BorderLayout are organized at the borders(North, South, East and West) and the center of a container.

Clipping is the process of confining paint operations to a limited area or shape.

The setLayout() method is used to specify a container's layout.

The CheckboxMenuItem class extends the MenuItem class to support a menu item that may be checked or unchecked.

List is here :

  • Flow Layout Manager
  • Grid Layout Manager
  • Box Layout Manager
  • Border Layout Manager
  • Card Layout Manager
  • GridBag Layout Manager.

The Panel and Applet classes use the FlowLayout as their default layout.

JFC stands for Java Foundation Classes. 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.

JComponent is a subclass of Container and can contain other components and JComponents.

The Defaulf Layout for the ConteutPane in JFC is that :

  •  BorderLayout.

Changing the state of an object is called an event.

 

Applet is a java program that runs inside a web browser.

The preferred size of a component is the minimum component size that will allow the component to display normally.

The Elements of a GridBagLayout are organized according to a grid. However,the elements are of different sizes and may occupy more than row or column of the grid. In addition, the rows and columns may have diff sizes.

A Canvas object provides access to a graphics object via its paint() method.

The enableEvents() method is used to enable an event for a particular object.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.

setSecurityManager()
Method is used for setting security in applets .

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 trfer speed because one buffer can be filled while the other is being emptied.

setBounds
Method of the Component class is used to set the position and size of a component.

The difference are  :

  • A Choice is displayed in a compact from that requires we to pull ot down tosee the list of available choice, Oly one item may be selected from a Choice.
  • A List may be dsplyaed in such a way that several List items are visible. A List supports the selection of the one or more List items .

AWT stands for Abstract Window Toolkit. AWT enables programmers to develop Java applications with GUI components, such as windows, and buttons. The Java Virtual Machine (JVM) is responsible for trlating the AWT calls into the appropriate calls to the host operating system.

 

AWT is heavy-weight components, but Swing is light-weight components. AWT is OS dependent because it uses native components, But Swing components are OS independent. We can change the look and feel in Swing which is not possible in AWT. Swing takes less memory compared to AWT. For drawing AWT uses screen rendering where Swing uses double buffering.

The java.awt.AWTEvent class is the highest­level class in the AWT event­class hierarchy.

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.

The window, Frame and Dialog classes use a border layout as their default layout.

Swing is a GUI toolkit for Java. It is one part of the Java Foundation Classes (JFC). Swing includes graphical user interface (GUI) widgets such as text boxes, buttons, split-panes, and tables.
Swing widgets provide more sophisticated GUI components than the earlier Abstract Window Toolkit. Since they are written in pure Java, they run the same on all platforms, unlike the AWT which is tied to the underlying platform's windowing system. Swing supports pluggable look and feel – not by using the native platform's facilities, but by roughly emulating them. This me you can get any supported look and feel on any platform. The disadvantage of lightweight components is slower execution. The advantage is uniform behavior on all platforms.

A Canvas object provides access to a Graphics object via its paint() method.

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.

A layout manager is an object that is used to organize components in a container.

If the applet class is not in the same directory, codebase is used.

  • 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

The Key point is that the paint() method invokes three methods in the following order :

  • PaintComponent()
  • paintBorder()
  • paintChildren()

As a general rule, in Swing, we should be overriding the paintComponent method unless we know what we are doing paintComponent() paints only component (panel) but paint() paints component and all its children.

SWT : SWT is stands for Standard Widget Toolkit, It is a completely independent Graphical User Interface (GUI) toolkit from IBM. They created it for the creation of Eclipse Integrated Development Environment (IDE).AWT is from Sun Microsystems.

  • Canvas is a component. ScrollPane is a container. 
  • Canvas is a rectangular area where the application can draw or trap input events. ScrollPane implements horizontal and vertical scrolling.

 

When a window is repainted by the AWT painting thread, it sets the clipping regions to the area of the window that requires repainting.

A heavyweight component is one that is associated with its own native screen resource (commonly known as a peer).

A lightweight component is one that "borrows" the screen resource of an ancestor (which me it has no native resource of its own -- so it's "lighter").