Thursday, January 3, 2013

10 AWT Swing Interview Questions Answers in Java

AWT and Swing Interview Questions are part of any Java interviews which involves GUI development work. Since AWT(Abstract Windows Toolkit) and Swing offers most popular Java GUI solutions, Yes, JavaFX  has still lot of path to cover in terms of popularity and usability of Swing. In Investment banking, where Java rules on server side application development, Swing was heavily used in GUI development couple of years back. Now trends is shifting towards C# for GUI development due to couple of reasons e.g. good Swing developers are not easy to be found as compared to C# developers. By the way there are still many development work is going on Swing. One of the reason Swing developers are high in demand is because Swing is quickly becoming a niche technology. This is surprising because  Swing is part of Java, but given steep learning curve of Swing to master different layouts and components e.g. JTable or JList , I tend to believe it. In this article we will see some good Swing Interview questions for practice. Questions like InvokeLater vs InvokeAndWait are classic, which is always worth preparing.

Swing and AWT Interview Questions Answers

Here is my list of 10 useful Swing and AWT Interview questions in Java. As per my experience, Swing interview questions comes mainly from three parts : Swing API, Swing thread-safety issues and questions related to layout managers.

Question 1: What is EDT thread in Swing?
10 AWT and Swing Interview questions with AnswersAnswer : This is one of the basic questions in Swing and AWT interviews. EDT stands for Event dispatcher thread. EDT is one of the most important thing to learn about Swing, Since Swing is single-threaded all GUI drawing and event handling is done in EDT thread and that's why its recommended not to perform any time consuming task e.g. connecting to database or opening network connection in Event Dispatcher thread or EDT thread. If you do that, it may lead to frozen or hung GUI. This question leads to several other questions in Java, e.g. If you can not open database connection in EDT thread than how will you open db connection with button click etc. well this can be done by spawning a new thread from button click and opening db connection there.

Question 2: What is difference between invokeAndWait and invokeLater in Java?
Answer : This is one of my favourite Swing question in Java Interviews. Knowledge of invokeAndWait and invokeLater is must for a good Swing developer because Swing is not thread-safe and at same time you can not perform time consuming task in EDT thread, as discussed in first Swing interview question. InvokeAndWait and InvokeLater method allows to enqueue task for EDT thread to perform, InvokeAndWait is a blocking method in Java and does it synchronously and invokeLater does it asynchronously. Since GUI can only be updated in Event dispatcher thread, if you need to show progress of any task, you can use these two methods. See my post Difference between invokeAndWait and invokeLater in Java for more detailed answer of this Swing question.

Question 3: What is difference between Swing and AWT in Java
Answer : One of the most frequently asked AWT and Swing Interview question. One answer of this question is that, Swing is a considered as light weight and AWT is considered as heavy weight. Another difference between AWT and Swing is that, Swing offers uniform look and feel across platform while look and feel of AWT GUI application are platform dependent because AWT mostly use native components e.g. a AWT windows will look different in DOS and Windows operating system.

Question 4: What is difference between paint and repaint in Java Swing?
Answer : This can be a tough Java question if you are not familiar with Swing API. Well, this is similar to start() and run() method of thread class. As calling start() method will eventually calls run() method of Runnable interface, Calling repaint() will call paint() method in Swing. Since painting can only be done in EDT thread, repaint() just put a paint request in EDT Queue, later EDT thread may combine several repaint request to one and can perform repainting. repaint() is a not a blocking method in Java and returns quickly.

Question 5: What is difference between BorderLayout and GridLayout ?
Answer : This is one of interesting Swing interview question. BorderLayout and GridLayout are two widely used LayoutManager from Swing API, former arranges components in predefined position e.g. NORTH, SOUTH, EAST and WEST while later arranges components one after other until there is space and wraps them afterwards. This Swing question becomes more difficult when Interviewer asked to write code to implement a layout e.g. drawing a layout and ask you to implement as discussed in Top 10 Swing interview questions in Java.

Question 6: How to change a button from enable to disable after click ?
Answer : When button is clicked an action event is fired which can be captured by implementing ActionListener interface and actionPerformed(ActionEvent ae) method. You can then call button.setEnable(false) to disable this button.

Question 7: Why Swing is called light weight ?
Answer :  Most of Swing component are inherited form JComponent and doesn't required a native peer and that's why they are referred as light weight component. light weight component implement look and feel in Java rather than using look and feel of native component and that's why Swing's look and feel remains same across platform.

Question 8: Is Swing Thread safe in Java ?
Answer : This is one of the tricky question in Java Swing and AWT. No, Swing is not thread-safe in Java. Swing components are not thread-safe they can not be modified from multiple threads. All swing components are designed to be updated by using Event dispatcher thread or EDT thread. See How to write thread-safe code in Java to know more about thread-safety.  By the way you can use invokeAndWait and invokeLater to safely update swing components as discussed in previous Swing interview question.

Question 9: Which method of Swing are thread-safe?
Answer : This AWT and Swing question is asked as follow-up of previous Swing interview question. Only couple of methods like repaint() and revalidate() are thread-safe in Swing, i.e. they can be called from multiple threads without additional synchronization in Java.

Question 10: What is difference between Container and Component ?
Answer : Main difference between Container and Component is that former can hold other components e.g. JFrame which is used as container to hold other components e.g. JButton. This is rather a simple Swing question and mostly asked in telephonic or upto 2 years experienced programmers.

That's on this list of Swing Interview questions and answers in Java. Despite reducing popularity and losing competition with C#, Swing is a great GUI technology and one of the most preferred technology  for implementing thick clients in Java. Especially in finance and insurance domain, where client application need to work with high volume of data, Java Swing is the solution. Good Swing developers are hard to find and that's why well paid in this industry.

Other Java Interview Questions from Java67 Blog

1 comment:

  1. Do they still ask AWT questions in Interview ? I thought Swing and AWT as old technologies, surprise to see it's still used.

    ReplyDelete

Java67 Headline Animator