90+ TOP Java Swings Interview Questions and Answers
1. What’s Java Swing?
Swing is basscially a type of Toolkit which is 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 means 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.
2. What is JFC?
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
3. What is AWT?
AWT is bassically 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 translating the AWT calls into the appropriate calls to the host operating system.
4. What are the differences between Swing and AWT?
Many diff are there :
> 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.
5. What are heavy weight components ?
A Heavyweight component is bassically associated with its own native screen resource which is generally known as a peer.
6. What is lightweight component?
A Lightweight component is the bassically one that “borrows” the screen resource of an ancestor , which means it has no native resource of its own — so it’s “lighter”.
7. What is double buffering ?
Double buffering is bassically 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.
8. What is an event in Swing?
Event is basscially as Changing the state of an object is called an event.
9. What is an event handler in swing?
An Event handler is bassically a part of a computer program created to tell the program how to act in response to a specific event.
10. What is a layout manager?
A layout manager is an object that is used to organize components in a container.
11. What is clipping?
Clipping is the bassically the process of confining paint operations to a limited area or shape.
12. Which containers use a border Layout as their default layout in swing?
many containers are there :
> The window,
> Frame and
> Dialog classes use a border layout as their default layout.
13. What is the preferred size of a component?
The preferred size of a component is the minimum component size, whihc will be allow the component to display normally.
14. What method is used to specify a container’s layout?
The setLayout() method is used to specify a container’s layout.
15. Which containers use a FlowLayout as their default layout?
The Panel and Applet classes use the FlowLayout as their default layout.
16. Which method of the Component class is used to set the position and size of a component?
Method of the Component class is used to set the position and size of a component. hat is defined as :
17. What is the What is the difference between invokeAndWait() and invokeLater()? ?
Mnay diff are there which is defined as :
> 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.
18. 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.
19. What is an applet?
Applet is bassically a java program that runs inside a web browser.
20. What is the difference between applications and applets?
Many diff are there :
> 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.
21. Which method is used by the applet to recognize the height and width?
Method is used by the applet to recognize the height and width which is defined as :
22. When we should go for codebase in applet?
If the applet class is not in the same directory,when we used to codebase.
23. What is the lifecycle of an applet?
An Applet is basscially lifecyacle is defined as :
> 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.
24. Which method is used for setting security in applets?
The Method is used for setting security in applets whihc is defined as :
25. What is an event and what are the models available for event handling?
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
26. What are the advantages of the event-delegation model over the event-inheritance model?
Event-delegation model has two advantages over event-inheritance model.
=> 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.
=> 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.
27. Can a class be it�s own event handler? Explain how to implement this?
Sure.a Class can be its own event handler. For an example could be a class that extends Jbutton and implements ActionListener. In the actionPerformed method, put the code to perform when the button is pressed.
28. Why does JComponent have add() and remove() methods but Component does not?
JComponent have add() and remove() methods but Component does not because JComponent is a subclass of Container, and can contain other components and jcomponents.
29. How would you create a button with rounded edges?
there’s 2 ways to create a button with rounded edges.
> The first thing is to know that a JButton�s edges are drawn by a Border. so we can override the Button�s paintComponent or Graphics method and draw a circle or rounded rectangle and turn off the border. Or we can create a custom border that draws a circle or rounded rectangle around any component and set the button�s border to it.
30. What is the difference between the ‘Font’ and ‘FontMetrics’ class?
many diff are there which is defined as :
> The Font Class is used to render ‘glyphs’, The characters we see on the screen.
> FontMetrics encapsulates information about a specific font on a specific Graphics object.
31. What class is at the top of the AWT event hierarchy?
java.awt.AWTEvent is that class which is at the top of the AWT event hierarchy. if they say java.awt.Event, they haven�t deal with swing or AWT in a while.
32. Explain how to render an HTML page using only Swing?
HTML page is bassically render the page using the JEditorPane or JTextPane and set it with the HTMLEditorKit, and load it that the use of swing in HTML pages.
33. How would you detect a keypress in a JComboBox?
“KeyListner in a JComboBox” is the abssically tyoe of the trick . many programmer is said thet: “add the keyListner in a JComboBox “. But its main use in the ” add the keyListner in a JComboBox as a editor component.
34. Why should the implementation of any Swing callback (like a listener) execute quickly?
Any swing callback execute quickly should be implementation Because callbacks are invoked by the event dispatch thread which will be blocked processing other events for as long as our method takes to execute.
35. In what context should the value of Swing components be updated directly?
Any ther context is not thread safe. But in callback methods , Swing components should be updated directly only in the context.In callback methods invoked from the event dispatch thread.
36. Why would you use SwingUtilities.invokeAndWait or SwingUtilities.invokeLater?
I want to update a Swing component but I�m not in a callback. If I want the update to happen immediately or perhaps for a progress bar component, then I�d use invokeAndWait. If I don�t care when the update occurs, I�d use invokeLater.
37. If your UI seems to freeze periodically, what might be a likely reason?
A callback implementation like ActionListener.actionPerformed or MouseListener.mouseClicked is taking a long time to execute thereby blocking the event dispatch thread from processing other UI events.
38. Which Swing methods are thread-safe?
The only thread-safe methods are following here :
> Revalidate(), and
39. Why won’t the JVM terminate when I close all the application windows?
The AWT event dispatcher thread is not a daemon thread. You must explicitly call System.exit to terminate the JVM.
40. 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.
41. What is an event?
Event is bassically a changing the state of an object .
42. What is an event handler ?
An event handler is bassically is a part of a computer program, Which is created mainly for the programs, Its signifies the program how to act in response to a specific event.
43. Which containers use a border Layout as their default layout?
any containers are there which is use border layout as their default layout. that are fillowing as :
> The window,
> Frame and
> Dialog classes .
44. What method is used to specify a container’s layout?
MAny methods are used to specify a container’s layout But the main method is :
> The setLayout()
45. What is the difference between invokeAndWait() and invokeLater()?
MAny diff are there which is defined as :
> InvokeAndWait : is synchronous. It blocks until Runnable task is complete.
> InvokeLater is asynchronous. It posts an action event to the event queue and returns immediately. It will not wait for the task to complete .
46. Why should any swing call back implementation execute quickly?
Any cal back implementation execute quickly should be imp becouse Callbacks are invoked by the event dispatch thread. Event dispatch thread blocks processing of other events as long as call back method executes.
47. Which method is used by the applet to recognize the height and width?
Method is there :
48. What is source and listener ?
They are defined as :
> A source : It is an object that generates an event. This occurs when the internal state of that object changes in some way.
> A listener : It is an object that is notified when an event occurs. It has two major requirements.
* It must have been registered with a source to receive notifications about specific event.
* It must implement necessary methods to receive and process these notifications.
49. What is controls and what are different types of controls in AWT?
Controls are bassically a components, Which is allow a user to interact with your application. AWT supports the following types of controls :
> Push Buttons,
> Check Boxes,
> Choice Lists,
> Text Components.
These controls are subclasses of Component.
50. What is the difference between choice and list?
They are defined as :
> A Choice : Choice is displayed in a compact form that requires us to pull it down to see the list of available choices and only one item may be selected from a choice.
> A List : It may be displayed in such a way that several list items are visible and it supports the selection of one or more list items.
51. What is the difference between scrollbar and scrollpane?
Main diff are there :
> A Scrollbar is a Component, but not a Container whereas Scrollpane is a Container and handles its own events and perform its own scrolling.
52. What is a layout manager and what are different types of layout managers available?
A layout manager is bassically an object, Its mainly used to organize components in a container. The different layouts are available here :
> GridLayout ,
> Boxlayout and
53. How are the elements of different layouts organized?
The elements of a FlowLayout are organized in a top to bottom, left to right fashion. The elements of a BorderLayout are organized at the borders (North, South, East and West) and the center of a container. The elements of a CardLayout are stacked, on top of the other, like a deck of cards. The elements of a GridLayout are of equal size and are laid out using the square of a grid. The elements of a GridBagLayout are organized according to a grid. However, the elements are of different size and may occupy more than one row or column of the grid. In addition, the rows and columns may have different sizes. It is the most flexible layout.
54. What are types of applets?
There are two different types of applets. that are defined as :
> Trusted Applets and
> Untrusted applets.
> Trusted Applets : are applets with predefined security .and
> Untrusted Applets : are applets without any security.
55. What are the restrictions imposed by a Security Manager on Applets?
Applets cannot read or write files on the client machine that’s executing it. They cannot load libraries or access native libraries. They cannot make network connections except to the host that it came from. They cannot start any program on the client machine. They cannot read certain system properties. Windows that an applet brings up look different than windows that an application brings up.
56. What is the difference between the Font and FontMetrics classes?
Main diff are there :
> The FontMetrics class is used to define implementation-specific properties, such as ascent and descent, of a Font object.
57. What is the relationship between an event-listener interface and an event-adapter class?
An Event-listener interface bassically defines the methods , WHich must be implemented by an event handler for a particular kind of event. An event adapter provides a default implementation of an event-listener interface.
58. 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.
59. What is the difference between the paint() and repaint() methods?
Main diff are there :
> The paint() method : Its supports painting via a Graphics object.
> The Repaint() method : It is used to cause paint() to be invoked by the AWT painting thread.
60. What interface is extended by AWT event listeners?
java.util.EventListener interface is extented by all AWT event listeners.
61. What is Canvas ?
Canvas is bassically a Component of the subclass which is used for drawing and painting. Canvas is a rectangular area where the application can draw or trap input events.
62. What is default Look-and-Feel of a Swing Component?
Java Look-and-Feel is the default Look-and-Feel of the Swing Component.
63. What are the features of JFC?
Mainly features are there :
> Pluggable Look-and-Feel,
> Accessibility API,
> Java 2D API,
> Drag and
> Drop Support.
64. What does x mean in javax.swing?
x mean in javax.swing is that :
> Extension of java.
65. What are invisible components?
Invisible components are bassically light weight components, Which is perform no painting, but can take space in the GUI. This is mainly used for layout management.
66. What is the default layout for a ContentPane in JFC?
The Defaulf Layout for the ConteutPane in JFC is that :
67. What does Realized mean?
Realized mean bassically 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 :
> Show() or
68. What is difference between Swing and JSF?
Many diff are there which is folowing here :
> 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.
69. Why does JComponent class have add() and remove() methods but Component class does not?
JComponent is a subclass of Container and can contain other components and JComponents.
70. What method is used to specify a container’s layout?
The SetLayout() method is bassically used to specify a container’s layout.
71. What is the difference between AWT and SWT?
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.
72. What is the difference between JFC & WFC?
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.
73. What is a convertor?
Converter is bassically an application that converts distance measurements between metric and U.S units.
74. What is the difference between a Canvas and a Scroll Pane?
Many diff are there :
> 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.
75. What is the purpose of the enableEvents() 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.
76. What is the difference between a MenuItem and a CheckboxMenuItem?
The CheckboxMenuItem class extends the MenuItem class to support a menu item that may be checked or unchecked.
77. 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.
78. How the Canvas class and the Graphics class are related?
A Canvas object provides access to a Graphics object via its paint() method.
79. What is the difference between a Window and a Frame?
The Frame class extends Window to define a main application window that can have a menu bar. A window can be modal.
80. What is the relationship between clipping and repainting?
When a window is repainted by the AWT painting thread, it sets the clipping regions to the area of the window that requires repainting.
81. What advantage do Java’s layout managers provide over traditional windowing systems?
Java uses layout managers to lay out components in a consistent manner across all windowing platforms. Since Java’s layout managers aren’t tied to absolute sizing and positioning, they are able to accommodate platform-specific differences among windowing systems.
82. When should the method invokeLater() be used?
This method is used to ensure that Swing components are updated through the event-dispatching thread.
83. What is the diff b/w choice and the List ?
Many diff are there :
> A Choice is displayed in a compact from that requires we to pull ot down to see 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 .
84. Which component subclass is used for the drawing and painting ?
85. Which containers may have a menu bar?
86. Which class is the immediate supar class of the menuComponent class?
87. What is the relationship b/w the Canvas class and the Graphics Class?
A Canvas object provides access to a graphics object via its paint() method.
88. How are the elements of a borderLayout organized?
The Elements of a BorderLayout are organized at the borders(North, South, East and West) and the center of a container.
89. How are the elements of a GridBagLayout organized?
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.
90. Difference b/w paint() and paintComponent()?
The Key point is that the paint() method invokes three methods in the following order :
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.
91. Give us the Name of the List LayoutManagers in Java?
List is here :
> Flow Layout Manager
> Grid Layout Manager
> Box Layout Manager
> Border Layout Manager
> Card Layout Manager
> GridBag Layout Manager.