java swing interview questions

7
Java Swing interview questions 1) Can a class be it’s own event handler? Explain how to implement this.  Answer: 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. 2) Why does JComponent have add() and remove() methods but Component does not?  Answer: because JComponent is a subclass of Container, and can contain other components and  jcomponents. 3) How would you create a button with rounded edges?  Answer: 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 c an create a custom border that draws a circle or rounded rectangle around any component and set the button’s border to it. 4) If I wanted to use a SolarisUI for just a JTabbedPane, and the Metal UI for everything else, how would I do that?  Answer: in the UIDefaults table, override the entry for tabbed pane and put in the SolarisUI delegate. (I don’t know it offhand, bu t I think it’s "com.sun.ui.motiflookandfeel.MotifTabbedPaneUI" - anything simiar is a good answer.) 5) What is the difference between the ‘Font’ and ‘FontMetrics’ class?  Answer: 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) 6) What class is at the top of the AWT event hierarchy?  Answer: java.awt.AWTEvent. if they say java.awt.Event, they haven’t dealt with swing or AWT in a while. 7) Explain how to render an HTML page using only Swing.  Answer: Use a JEditorPane or JTextPane and set it with an HTMLEditorKit, then load the text into the pane. 8) How would you detect a keypress in a JComboBox?  Answer: 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.’ 9) Why should the implementation of any Swing callback (like a listener) execute quickly?

Upload: sarika-goel

Post on 07-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Swing Interview Questions

8/6/2019 Java Swing Interview Questions

http://slidepdf.com/reader/full/java-swing-interview-questions 1/7

Page 2: Java Swing Interview Questions

8/6/2019 Java Swing Interview Questions

http://slidepdf.com/reader/full/java-swing-interview-questions 2/7

Page 3: Java Swing Interview Questions

8/6/2019 Java Swing Interview Questions

http://slidepdf.com/reader/full/java-swing-interview-questions 3/7

Page 4: Java Swing Interview Questions

8/6/2019 Java Swing Interview Questions

http://slidepdf.com/reader/full/java-swing-interview-questions 4/7

Page 5: Java Swing Interview Questions

8/6/2019 Java Swing Interview Questions

http://slidepdf.com/reader/full/java-swing-interview-questions 5/7

Page 6: Java Swing Interview Questions

8/6/2019 Java Swing Interview Questions

http://slidepdf.com/reader/full/java-swing-interview-questions 6/7

Page 7: Java Swing Interview Questions

8/6/2019 Java Swing Interview Questions

http://slidepdf.com/reader/full/java-swing-interview-questions 7/7