10 Java Swing Interview Questions And AnswersPosted 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.
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. 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:
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.
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.
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.
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!More by this author |