10 Java Swing Interview Questions And Answers

Posted by Infocampus HR on February 10th, 2018

1) What is Event-Dispatcher-Thread (EDT) in Swing?

The Event Dispatcher Thread or EDT is a special thread in Swing and AWT.  The Event-Driven Thread is used to render graphics and listen for events in Swing. You will get a bonus point if you are able to highlight that time-consuming operation like connecting to database, opening a file, or connecting to network should not be done on EDT thread because it could lead to freezing GUI because of blocking and time-consuming nature of these operations instead they should be done on separate thread and EDT can just be used to spawn those thread on a button click or mouse click.


2) Does Swing is thread-safe? What do you mean by swing is not thread-safe?

This swing interview question is getting very popular nowadays. Though it’s pretty basic many developers don't understand thread-safety issue in Swing. Since Swing components are not thread-safe it means you can not update these components in any thread other than Event-Dispatcher-Thread.

If you do so you will see unexpected behavior e.g. freezing GUI due to the deadlock of incorrect values etc. Some time interviewer will also ask what are thread-safe methods in the Swing API? Those are the methods which can be safely called from any thread and there are only a couple of them e.g. repaint() and revalidate(). See this article for more discussion on Swing and thread-safety.

3) What are differences between Swing and AWT?

One of the classic Java swing interview questions and mostly asked on phone interviews. There are a couple of differences between Swing and AWT:

  1. The AWT component are considered to be heavyweight while Swing component are considered  lightweights
  2. The Swing GUI has pluggable look and feel.
  3. The AWT is platform dependent i.e. the same GUI will look different on different platforms because they use native components e.g. a Frame will look different on Windows and Linux if you pick native components. On the other hand, Swing components are developed in Java and are platform dependent. It also provide consistent GUIs across platform e.g. if you run NetBeans which is created in Java on Linux or Windows you will see consistency in GUI. 

4) Why Swing components are called lightweight components?

Another popular Java swing interview question, I guess the oldest that generally comes as a follow-up to the earlier question based on your answer provided. Since the AWT components are associated with native screen resource they are called heavyweight component while Swing components use the screen resource of an ancestor instead of having their own and that's why called lightweight or lighter component. See here for more AWT base Interview Questions in Java. 

5) What is the difference between invokeAndWait and invokeLater?

This swing interview question is asked differently at different points. Some time interviewer asks how do you update swing component from a thread other than EDT instead of directly asking the difference between invokeAndWait() and invokeLater() method of Swing API.

6) Write code for JTable with custom cell editor and custom cell renderer?

Now comes the harder part of Swing interviews, questions asked in this part of Swing interviews is mostly about writing code and checking developer’s experience of API and GUI development and understanding of the key concept of swing code etc.


7) How do you handle opening of the database, file, or network connection on a click of a button?

This one is one of the easy Java swing interview questions. The interviewer is interested in whether you know the basic principle of Java GUI development or not. The answer is, you should not do this operation in EDT thread instead spawn a new thread from ActionListener or button and disable the button until operation gets completed to avoid resubmitting the request.


8) Write code to print following layout (mainly focused on GridBag layout)?

After JTable, the second favorite topic of the swing interviewers is GridBagLayout. The GridBagLayout in the swing is most powerful but at the same time, most complex layout and a clear cut experience and expertise around GridBagLayout is desired for developing Swing GUI for trading systems.


9) Prediction of the output of code?

This is another category of Swing interview questions asked in IB whether they will give you code and asked what would be the output, how will the GUI look like. This type of question is based upon how well you understand and visualize the code.


10) The question around JList is like Creating a JList component such which should contain all the asset classes like Stocks, Futures, Options, and FX etc in the form of String. But constraint is that the JList should always be sorted in the ascending order except that all assets which begin with "Electronic trading” appears on top?

This is an excellent Java Swing interview questions which are based on the real-world task and focuses on JList component, Sorting, and customizing JList model. For sorting, you can use comparable in Java and by customizing the JList model you can display content as requested. Once you successfully answer this question there may be some follow up on customizing the color of cell etc which can be done by customizing renderer.

Like it? Share it!


Infocampus HR

About the Author

Infocampus HR
Joined: December 10th, 2016
Articles Posted: 792

More by this author