Q) What's Java Swing?
A) 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 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.
Q) What is the difference between Swing and AWT components?
A) AWT components are heavy-weight, whereas Swing components are lightweight. Hence Swing works faster than AWT. Heavy weight components depend on the local windowing toolkit. For example, java.awt.Button is a heavy weight component. Pluggable look and feel possible using java Swing. Also, we can switch from one look and feel to another at runtime in swing which is not possible in AWT.
Q) What is JFC?
A) 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.
Q) What is AWT?
A) 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 translating the AWT calls into the appropriate calls to the host operating system.
Q) What are the differences between Swing and AWT?
A) A 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.
Q) What are heavyweight components ?
A) A heavyweight component is one that is associated with its own native screen resource (commonly known as a peer).
Q) What is lightweight component?
A) 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").
Q) What is double buffering ?
A) 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.
Q) What is an event?
A) Changing the state of an object is called an event.
Q) What is an event handler ?
A) An event handler is a part of a computer program created to tell the program how to act in response to a specific event.
Q) What is a layout manager?
A) A layout manager is an object that is used to organize components in a container.
Q) What is clipping?
A) Clipping is the process of confining paint operations to a limited area or shape.
Q) What is the preferred size of a component?
A) The preferred size of a component is the minimum component size that will allow the component to display normally.
Q) What method is used to specify a container's layout?
A) The setLayout() method is used to specify a container's layout.
Q) Which containers use a FlowLayout as their default layout?
A) The Panel and Applet classes use the FlowLayout as their default layout.
Q) Which method of the Component class is used to set the position and size of a component?
A) setBounds
Q) Name the containers which use Border Layout as their default layout?
A) window, Frame and Dialog classes.
Q) Name Container classes.
A) Window, Frame, Dialog, FileDialog, Panel, Applet, or ScrollPane
Q) How can a GUI component handle its own events?
A) A component can handle its own events by implementing the required event-listener interface and adding itself as its own event listener.
Q) What is the difference between the paint() and repaint() methods?
A) 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.
Q) Which package has light weight components?
A) javax.Swing package contains light weight components. All components in Swing, except JApplet, JDialog, JFrame and JWindow are lightweight components.
Q) What are peerless components?
A) The peerless components are called light weight components.
Q) 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.
Q) How are the elements of a GridBagLayout organized?
Or
What is a layout manager and what are different types of layout managers available in java Swing?
Or
How are the elements of different layouts organized?
A) A layout manager is an object that is used to organize components in a container. The different layouts available are FlowLayout, BorderLayout, CardLayout, GridLayout and GridBagLayout.
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.
Q) What advantage do Java's layout managers provide over traditional windowing systems?
A) 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.
Q) What method is used to specify a container's layout?
A) The setLayout() method is used to specify a container's layout. For example, setLayout(new FlowLayout()); will be set the layout as FlowLayout.
Q) Which Container method is used to cause a container to be laid out and redisplayed?
A) validate()
Q) Name Component subclasses that support painting.
A) The Canvas, Frame, Panel, and Applet classes support painting.
Q) What is the purpose of the enableEvents() method?
A) 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.
Q) What is the difference between a Window and a Frame?
A) The Frame class extends Window to define a main application window that can have a menu bar.
Q) What do heavy weight components mean?
A) Heavy weight components like Abstract Window Toolkit (AWT) depend on the local windowing toolkit. For example, java.awt .Button is a heavy weight component.
Q) What is the difference between a Scrollbar and a ScrollPane?
A) A Scrollbar is just a Component, but not a Container. A ScrollPane is a Container. A ScrollPane handles its own events and performs its own scrolling.
Q) What is the preferred size of a component?
A) The preferred size of a component is the minimum component size that will allow the component to display normally.
Q) Which containers use a FlowLayout as their default layout?
A) The Panel and Applet classes use the FlowLayout as their default layout.
Q) What is the difference between a Choice and a List?
A) Choice is displayed in a compact form that requires you to pull it down to see the list of available choices. Only one item may be selected from a Choice. A List may be displayed in such a way that several List items are visible. A List supports the selection of one or more List items.
Q) What interface is extended by AWT event listeners?
A) All AWT event listeners extend the java.util.EventListener interface.
Q) What is a layout manager?
A) A layout manager is an object that is used to organize components in a container.
Q) Which Component subclass is used for drawing and painting?
A)Canvas
Q) What is the difference between a Scrollbar and a ScrollPane?
A) Scrollbar is a Component, but not a Container. A ScrollPane is a Container. A ScrollPane handles its own events and performs its own scrolling.
Q) Which Swing methods are thread-safe?
A) The only thread-safe methods are repaint(), revalidate(), and invalidate()
Q) Which containers use a border Layout as their default layout?
A) The Window, Frame and Dialog classes use a border layout as their default layout
Q) What is the preferred size of a component?
A) The preferred size of a component is the minimum component size that will allow the component to display normally
Q) Which containers use a FlowLayout as their default layout?
A) The Panel and Applet classes use the FlowLayout as their default layout
Q) What is the immediate superclass of the Applet class?
A) Panel
Q) Name three Component subclasses that support painting
A) The Canvas, Frame, Panel, and Applet classes support painting
Q) What is the immediate superclass of the Dialog class?
A) Window
Q) What is clipping?
A) Clipping is the process of confining paint operations to a limited area or shape.
Q) What is the difference between a MenuItem and a CheckboxMenuItem?
A) The CheckboxMenuItem class extends the MenuItem class to support a menu item that may be checked or unchecked.
Q) What class is the top of the AWT event hierarchy?
A) The java.awt.AWTEvent class is the highest-level class in the AWT event-class hierarchy
Q) In which package are most of the AWT events that support the event-delegation model defined?
A) Most of the AWT-related events of the event-delegation model are defined in the java.awt.event package. The AWTEvent class is defined in the java.awt package.
Q) Which class is the immediate superclass of the MenuComponent class
A) Object
Q) Which containers may have a MenuBar?
A) Frame
Q) What is the relationship between the Canvas class and the Graphics class?
A) Canvas object provides access to a Graphics object via its paint() method.
Q) How are the elements of a BorderLayout organized?
A) The elements of a BorderLayout are organized at the borders (North, South, East, and West) and the center of a container.
Q) What is the difference between a Window and a Frame?
A) The Frame class extends Window to define a main application window that can have a menu bar.
Q) What is the difference between the Font and FontMetrics classes?
A) The FontMetrics class is used to define implementation-specific properties, such as ascent and descent, of a Font object.
Q) How are the elements of a CardLayout organized?
A) The elements of a CardLayout are stacked, one on top of the other, like a deck of cards.
Q) What is the relationship between clipping and repainting?
A) When a window is repainted by the AWT painting thread, it sets the clipping regions to the area of the window that requires repainting.
Q) What is the relationship between an event-listener interface and an event-adapter class?
A) An event-listener interface defines the methods that 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.
Q) How can a GUI component handle its own events?
A) A component can handle its own events by implementing the required event-listener interface and adding itself as its own event listener.
Q) How are the elements of a GridBagLayout organized?
A) The elements of a GridBagLayout are organized according to a grid. However, the elements are of different sizes and may occupy more than one row or column of the grid. In addition, the rows and columns may have different sizes.
Q) What advantage do Java's layout managers provide over traditional windowing systems?
A) 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 accomodate platform-specific differences among windowing systems.
Q) How can the Checkbox class be used to create a radio button?
A) By associating Checkbox objects with a CheckboxGroup
Q) Difference between paint() and paintComponent()?
A) The key point is that the paint() method invokes three methods in the following order: a) paintComponent()
b) paintBorder()
c) paintChildren()
As a general rule, in Swing, you should be overriding the paintComponent method unless you know what you are doing. paintComponent() paints only component (panel) but paint() paints component and all its children.
Q) What is the difference between paint(), repaint() and update() methods within an applet which contains images?
A) paint : is only called when the applet is displayed for the first time, or when part of the applet window has to be redisplayed after it was hidden. repaint : is used to display the next image in a continuous loop by calling the update method. update : you should be aware that, if you do not implement it yourself, there is a standard update method that does the following : it will reset the applet window to the current background color (i.e. it will erase the current image) it will call paint to construct the new image
Q) What is the name of the design pattern that Java uses for all Swing components?
A) MVC(Model View Controller) pattern
Q) Name few LayoutManagers in Java.
A) Flow Layout Manager
Grid Layout Manager
Border Layout Manager
Box Layout Manager
Card Layout Manager
GridBag Layout Manager
Q) What is the What is the difference between invokeAndWait() and invokeLater()?
A) 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
Q) Why should any swing call back implementation execute quickly?
A) Callbacks are invoked by the event dispatch thread. Event dispatch thread blocks processing of other events as long as call back method executes.
Q) Can a class be it's own event handler? Explain how to implement this.
A) Sure. 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.
Q) Why does JComponent have add() and remove() methods but Component does not?
A) Because JComponent is a subclass of Container, and can contain other components and jcomponents.
Q) How would you create a button with rounded edges?
A) There's 2 ways. The first thing is to know that a JButton's edges are drawn by a Border. so you can override the Button's paintComponent(Graphics) method and draw a circle or rounded rectangle (whatever), and turn off the border. Or you can create a custom border that draws a circle or rounded rectangle around any component and set the button's border to it.
Q) If I wanted to use a SolarisUI for just a JTabbedPane, and the Metal UI for everything else, how would I do that?
A) In the UIDefaults table, override the entry for tabbed pane and put in the SolarisUI delegate. (I don't know it offhand, but I think it's "com.sun.ui.motiflookandfeel.MotifTabbedPaneUI" - anything simiar is a good answer.)
Q) What is the difference between the 'Font' and 'FontMetrics' class?
A) The Font Class is used to render 'glyphs' - the characters you see on the screen. FontMetrics encapsulates information about a specific font on a specific Graphics object. (width of the characters, ascent, descent)
Q) What class is at the top of the AWT event hierarchy?
A) java.awt.AWTEvent. if they say java.awt.Event, they haven't dealt with swing or AWT in a while.
Q) Explain how to render an HTML page using only Swing.
A) Use a JEditorPane or JTextPane and set it with an HTMLEditorKit, then load the text into the pane.
Q) How would you detect a keypress in a JComboBox?
A) This is a trick. most people would say 'add a KeyListener to the JComboBox' - but the right answer is 'add a KeyListener to the JComboBox's editor component.'
Q) Why should the implementation of any Swing callback (like a listener) execute quickly?
A) Because callbacks are invoked by the event dispatch thread which will be blocked processing other events for as long as your method takes to execute.
Q) In what context should the value of Swing components be updated directly?
A) Swing components should be updated directly only in the context of callback methods invoked from the event dispatch thread. Any other context is not thread safe?
Q) Why would you use SwingUtilities.invokeAndWait or SwingUtilities.invokeLater?
A) I want to update a Swing component but I'm not in a callback. If I want the update to happen immediately (perhaps for a progress bar component) then I'd use invokeAndWait. If I don't care when the update occurs, I'd use invokeLater.
Q) If your UI seems to freeze periodically, what might be a likely reason?
A) 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.
Q) Which Swing methods are thread-safe?
A) The only thread-safe methods are repaint(), revalidate(), and invalidate()
Q) Why won't the JVM terminate when I close all the application windows?
A) The AWT event dispatcher thread is not a daemon thread. You must explicitly call System.exit to terminate the JVM.
Q) What does JComponent.invalidate() or JComponent.revalidate() do?
A) This component and all parents above it are marked as needing to be laid out. This means the Layout Manager will try to realign the components. Often used after removing components. It is possible that some really sharp swing people may miss this. I would think that you will only know this if you are actually using Swing.
Q) How to create your own JTable TableModel?
A) Extend AbstractTableModel (preferred) or
Extend DefaultTableModel
Q) Explain Swing threading?
A) Swing is single-threaded. Might talk about EventThread.
Q) In a Swing application if you click a button that does a long running process what do you need to do? Can you call the long method directly inside of the actionPerformed() method?
A) Create a new thread to run the long task in. This is the question that will fail 99% of the people I interview for Swing. The basic steps are: A. Listener catches the event. B. Spawn a new thread to do the long running event. C. Have that thread use SwingUtilities.invokeLater or EventThread to update the GUI when it is finished
Q) What happens if you call the long running method directly from the actionPerformed() method?
A) App will not repaint. You will often have gray areas when you had something over the application then remove it. The screen may flicker. Minimize then maximize will often not show the app. It will only show a gray background.
Q) Which containers use a border Layout as their default layout?
A) The window, Frame and Dialog classes use a border layout as their default layout.