Role-specific Swing AWT Interview Questions & Answers:
1. What is the purpose of action interface in Swing?
Action is performed on a state to allow it to change. It defines the interface that it is implementing. The library that is used for the action interface is javax.swing.Action. This action interface extends the ActionListener interface class that is being provided from the AWT. Action interface allow the concrete classes to implement the actionPerformed() method and provide the action that is associated with it. The use of actionPerformed() method allow the implementation of the behavior that is desired in the programming and allow it to show the functionality of the action class. The action can be added to the container class that accepts the parameters on an event like JMenu, JPopupMenu, or JtoolBar. The container used in this automatically registers the action that is taken on the event and it acts as an ActionListener of the user interface.
2. What is the function of Abstract Action class?
AbstractAction class allows the implementation of the abstract functions and the action interfaces. AbstractAction provides with the default functionality that are provided with the methods in the Action interface. The action can extend the class to create some specific actions for the users. The methods that is provided to implement the actionPerformed() method and it is used to provide the overall functionality for the action. ActionEvent is used to show the action command that can be used to add some features of the contents and provide ready to use action functionality on the event that can be carried for all the user interface fields.
class MyAction extends AbstractAction
{ }
public MyAction(String text, Icon icon)
{ super(text,icon); }
public void actionPerformed(ActionEvent e)
{ System.out.println("Action ["+e.getActionCommand()+"]!"); }
3. What is the use of JComponent Class in Swing?
Jcomponent is used as an abstract class that extends the functionality of the swing components. It allows the common swing component library to get involved in providing the functionality and the visual appearance that is required. The class that serves this purpose is given as: java.awt.Component class. This class also provides the framework for AWT components as well. The other class javax.swing.JComponent provides the role that is identical to other classes for the Swing components. JComponent class extends the class java.awt.Container that itself extend another class of java.awt.Component. This allows the AWT functionality to be used and added while writing the code for a program that needs to be executed. Jcomponents uses the swing component and allow the containers to handle the request for all the components like add() the component in the container, etc.
4. What is the use of double buffering in swings?
Double buffering can be achieved by using the Jcomponent class that allows the Swing component to enable this feature. Double buffering allows the component to execute faster or take less time to render the individual parts of the screen. It allows the component to be drawn and doesn't refresh it all the times. The refreshes done, in the background for the processes that requires the dynamic content to be displayed. Due to this, the flickering of the component gets reduced and the component appears to be smooth. The screen updates the monitor at very high speed so that the component appears to perform well. Copies of the component within the area are faster and the rendering can be performed at higher speed by knowing the position of the component. The program that can be used to activate the buffering is:
boolean doubleBuffered property of JComponent. Passing in true to the
setDoubleBuffered()
// This is the method that enables the double buffering for the component. It holds two values true or false
JButton button = new JButton("test");
button.setDoubleBuffered(true);
// The value true is used to turn on double buffering
5. What is the purpose of Serialization in Swings?
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.
6. How does Accessibility works in Swings?
The feature that is used in swing consists of accessibility technology that allows the class archives like JAR file. This file resides on separate machine and can not be accessed without the permissions given from the owner. Java is used to load the technology in the virtual machine that is used as an application. The technologies allow the replacement to be done using the virtual machine. It uses the queue method and the class as java.awt.EventQueue. The properties are used using the configuration file that is stored in awt.properties and it consists of all the specialized classes. Virtual machine in Java accesses the configuration file accessibility.properties that is contained in the jre/lib directory to allow the namespaces to be fetched to execute the process. The format is given of awt.properties file as:
AWT.alt=Alt
AWT.meta=Meta
# Assistive technologies
AWT.assistive_technologies=SpeechRecognition
AWT.EventQueueClass=com.sun.java.accessibility.util.EventQueueMonitor
# Key names
AWT.enter=Enter
AWT.backSpace=Backspace
7. How to Parse HTML used Swing?
JEditorPane class is used to display the HTML content that is being written using the classes. When there is a file consisting of the HTML text gives the request to read the file identify the method to read the HTML text using the read() method that is given in HTMLEditorKit. This kit provides lot of tools that delegates the file to a separate parser object. This kid also consists of tools and the abstract inner class consisting of the method as:
public abstract void parse(Reader r, ParserCallback cb) throws IOException
The read() method calls the getParser() method allowing the subclasses to parse the object and use the HTML text. The parser currently uses the package javax.swing.text.html.parser that allow the reading of the file and parse the HTML code. The parameter of the type ParserCallback is used to communicate the progress of the process that is parsing the HTML file.
In the parse() method we listed earlier, you'll notice a very important parameter of type
ParserCallback . This is how the parser communicates its progress.
8. Write a program to paint the off-screen buffer in swings?
To paint the off-screen buffer in swings requires the paint component that is using an image allowing easy to create set of images and its components in different states. This image that is stored, remain useful for automated regression testing that is also known as the golden image approach that is used to compare the images with the current and old images that uses the last stable build of the image. The code that displays it is as follows:
JButton b = new JButton("Image buffer");
button.setSize(100, 50);
// This set the initial size of the image.
BufferedImage image = new BufferedImage(b.getWidth(), b.getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
button.paint(g);
ImageIO.write(image, "png", new File("image.png"));
9. What are the steps required in creating an empty table drop?
The dragging and dropping for an empty table allows it to be portable and the data can be moved with ease. The steps that are followed for this is:
• The creation of an empty table that is used to get some data and the insertion gets performed in using the database techniques.
• After creation of an empty table then the creating and attaching of the TransferHandler class gets included that allow the proper transfer of the data from one place to another. It also handles the interrupts that may occur while transferring the data.
• Enable the data transfer to allow the calling of the object setDragEnabled(true). When this value is set to true then the table can be draggable and the data can be moved.
• Creation of the scroll pane takes place to allow the adding of the table so that it can be dragged from one pane to another pane with ease and comfort.
10. What is the purpose of Transfer Handler 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 support the classes as well. Components are provided with the default handler that can create own handler of their own. There are three methods that can be used to use theTransferHandler on a component and these are as follows:
• setDragEnabled(boolean)- this turn the drag support that is given as false and it defines the component that support the drag facility that links to the list given by Jlist.
• setDropMode(DropMode)- is used to configure the drop locations and determine the use of Jlist, Jtable, Jtree.
• setTransferHandler(TransferHandler)- is used to allow the plug in data to be imported or exported by using the Jcomponent that is defined for it.
https://InterviewQuestionsAnswers.ORG.