1) What do you mean by MVC architecture? Explain its role in modern applications and list its advantages(may-2013,jan-2013,nov-2011). In addition to dividing the application into three kinds of components, the Model view controller (MVC) design defines the interactions between them MODEL UPDATES MANIPULATES VIEW CONTROLLER SEES USES USER A controller can send commands to the model to update the model's state (e.g., editing a document). It can also send commands to its associated view to change the view's presentation of the model (e.g., by scrolling through a document). A model notifies its associated views and controllers when there has been a change in its state. This notification allows the views to produce updated output, and the controllers to change the available set of commands. A passive implementation of MVC omits these notifications, because the application does not require them or the software platform does not support them. A view requests information from the model that it needs for generating an output representation to the user For example, in case of a checkbox, The Model contains a field that indicates if the box is checked or unchecked. The View determines how the component is displayed on the screen. The Controller determines how the component reacts to the user. Advantages 1. Multiple Views Using the Same Model. Representation the some model data in several views because the view and model are separated in MVC architecture 2. Efficient Modularity Allows developing view and model in different environment. 3. Easier Support for New Types of Clients Allows developing separated view and controller for new type of client and then integrating them the existing enterprise model. Disadvantages Increase Complexity/Effort Arjun V. Bala Page 1
2) Compare the AWT and Swing.(May-2013,June-2012) AWT AWT stands for Abstract windows toolkit. AWT components are called Heavyweight component. Swing Swing is also called as JFC s (Java Foundation classes). Swings are called light weight component because swing components sits on the top of AWT components and do the work. AWT components require java.awt package. Swing components require javax.swing package. AWT components are platform dependent. This feature is not supported in AWT. These feature is not available in AWT. Swing components are made in purely java and they are platform independent. We can have different look and feel in Swing. Swing has many advanced features like JTabel, Jtabbed pane which is not available in AWT. Also. Swing components are called "lightweight" because they do not require a native OS object to implement their functionality. JDialog and JFrame are heavyweight, because they do have a peer. So components like JButton, JTextArea, etc., are lightweight because they do not have an OS peer. With AWT, you have 21 "peers" (one for each control and one for the dialog itself). A "peer" is a widget provided by the operating system, such as a button object or an entry field object. With Swing, you would have only one peer, the operating system's window object. All of the buttons, entry fields, etc. are drawn by the Swing package on the drawing surface provided by the window object. This is the reason that Swing has more code. It has to draw the button or other control and implement its behavior instead of relying on the host operating system to perform those functions. AWT is a thin layer of code on top of the OS. Using AWT, you have to implement a lot of things yourself. Swing is much larger. Swing also has very much richer functionality. Swing has them built in. Arjun V. Bala Page 2
3) What is pluggable look and feel? How do you set the look and feel of the components? (June-2012) Swing provides programmer the facility to change the look and feel of components being displayed on any system, This is called plaf (pluggable look and feel). Look refers to the appearance of the component on the screen. Feel represents how the user can interact with the component. There are 3 types of Look & Feel available in Swing Metal Look & Feel Motif Look & Feel Windows Look & Feel By default, Swing programmer use metal look and feel. We can set any of the following look & feel using UIManager. We need to use setlookandfeel() method of the UIManager to set the Look & Feel. As an argument to above method we can pass either of the following string. javax.swing.metal.metallookandfeel com.sun.java.swing.plaf.motif.motiflookandfeel com.sun.java.swing.plaf.windows.windowslookandfeel The content of the content pane are updated using the updatecomponenttreeui() method. //Changing the look and feel of components import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.plaf.*; class LookFeel extends JFrame implements ItemListener //vars JButton b; JCheckBox cb;jk JTextField t; JRadioButton r1, r2, r3; ButtonGroup bg; Container c; LookFeel() //create content pane c = this.getcontentpane(); //set flow layout to c c.setlayout(null); //create components b = new JButton("Button"); cb = new JCheckBox("CheckBox"); t = new JTextField("TextField", 15); r1 = new JRadioButton("Metal"); r2 = new JRadioButton("Motif"); r3 = new JRadioButton("Windows"); Arjun V. Bala Page 3
//create ButtonGroup object and add radio buttons to specify //that they belong to same group bg = new ButtonGroup(); bg.add(r1); bg.add(r2); bg.add(r3); //set the location of components in content pane b.setbounds(100,50,75,40); cb.setbounds(100,100,100,40); t.setbounds(100,150,100,40); r1.setbounds(50,250,100,30); r2.setbounds(150,250,100,30); r3.setbounds(250,250,100,30); //add the components to content pane c.add(b); c.add(cb); c.add(t); c.add(r1); c.add(r2); c.add(r3); //add item listeners to radio buttons r1.additemlistener(this); r2.additemlistener(this); r3.additemlistener(this); //close the frame this.setdefaultcloseoperation(jframe.exit_on_close); public void itemstatechanged(itemevent ie) try //know which radio button is selected and accordingly change //the look and feel if(r1.getmodel().isselected()) UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); if(r2.getmodel().isselected()) UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); if(r3.getmodel().isselected()) UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //change the look and feel in the content pane SwingUtilities.updateComponentTreeUI(c); catch(exception e) Arjun V. Bala Page 4
public static void main(string args[]) //create the frame LookFeel lf = new LookFeel(); lf.setsize(400,400); lf.settitle("look and Feel"); lf.setvisible(true); Arjun V. Bala Page 5