for JTable
UIDelegate
UI
M anager
look-and-feel
• Component (e.g. JTable, JTree, and JList): coordinates actions of model and the UI delegate. Each generic component class handles its own individual view-and-controller responsibilities.
• Model (e.g. TableModel): charged with storing the data.
• UIDelegate: responsible for getting the data from model and rendering it to screen. It delegates any look-and- feel aspect of the component to the UI Manager.
Q 64:
Explain layout managers? LF FAQA 64:
Layout managers are used for arranging GUI components in windows. The standard layout managers are:• FlowLayout: Default layout for Applet and Panel. Lays out components from left to right, starting new rows if necessary.
• BorderLayout: Default layout for Frame and Dialog. Lays out components in north, south, east, west and center. All extra space is placed on the center.
• CardLayout: stack of same size components arranged inside each other. Only one is visible at any time. Used in TABs.
• GridLayout: Makes a bunch of components equal in size and displays them in the requested number of rows and columns.
• GridBagLayout: Most complicated but the most flexible. It aligns components by placing them within a grid of cells, allowing some components to span more than one cell. The rows in the grid aren’t necessarily all the same height, similarly, grid columns can have different widths as well.
• BoxLayout: is a full-featured version of FlowLayout. It stacks the components on top of each other or places them in a row.
Complex layouts can be simplified by using nested containers for example having panels within panels and each
panel can use its own LayoutManager. It is also possible to write your own layout manager or use manual positioning of the GUI components. Note: Further reading on each LayoutManagers is recommended for Swing developers.
Design pattern: The AWT containers like panels, dialog boxes, windows etc do not perform the actual laying out of the components. They delegate the layout functionality to layout managers. The layout managers make use of the strategy design pattern, which encapsulates family of algorithms for laying out components in the containers. If a particular layout algorithm is required other than the default algorithm, an appropriate layout manager can be instantiated and plugged into the container. For example, panels by default use the FlowLayout but it can be changed by executing:
panel.setLayout(new GridLayout(4,5));
This enables the layout algorithms to vary independently from the containers that use them. This is one of the key benefits of the strategy pattern.
Q 65:
Explain the Swing delegation event model? LFA 65:
In this model, the objects that receive user events notify the registered listeners of the user activity. In most cases the event receiver is a component.• Event Types: ActionEvent, KeyEvent, MouseEvent, WindowEvent etc. • Event Processors: JButton, JList etc.
• EventListeners: ActionListener, ComponentListener, KeyListener etc.
Swing Event Delegation Model
EVENT PROCESSOR (eg JButton, JList etc)
EVENT LISTENER (eg ActionListener etc)
EVENT no tif ies re gi st er s distributed
Java – Applet
Q 66:
How will you initialize an applet? LFA 66:
By writing your initialization code in the applet’s init() method or applet’s constructor.Q 67:
What is the order of method invocation in an applet? LF FAQA 67:
The Applet’s life cycle methods are as follows:• public void init() : Initialization method called only once by the browser.
• public void start() : Method called after init() and contains code to start processing. If the user leaves the page and returns without killing the current browser session, the start () method is called without being preceded by init ().
• public void stop() : Stops all processing started by start (). Done if user moves off page.
• public void destroy() : Called if current browser session is being terminated. Frees all resources used by the applet.
Q 68:
How would you communicate between applets and servlets? LF FAQA 68:
We can use the java.net.URLConnection and java.net.URL classes to open a standard HTTP connection and “tunnel” to a Web server. The server then passes this information to the servlet. Basically, the applet pretends to be a Web browser, and the servlet doesn’t know the difference. As far as the servlet is concerned, the applet is just another HTTP client. Applets can communicate with servlets using GET or POST methods.The parameters can be passed between the applet and the servlet as name value pairs.
http://www.foo.com/servlet/TestServlet?LastName=Jones&FirstName=Joe).
Objects can also be passed between applet and servlet using object serialization. Objects are serialized to and from the inputstream and outputstream of the connection respectively.
Q 69:
How will you communicate between two Applets? LF FAQA 69:
All the applets on a given page share the same AppletContext. We obtain this applet context as follows:AppletContext ac = getAppletContext();
AppletContext provides applets with methods such as getApplet(name), getApplets(), getAudioClip(url), getImage(url), showDocument(url) and showStatus(status).
Q 70:
What is a signed Applet? LF SE FAQA 70:
A signed Applet is a trusted Applet. By default, and for security reasons, Java applets are contained within a “sandbox”. Refer to the diagram below:This means that the applets can’t do anything, which might be construed as threatening to the user’s machine (e.g. reading, writing or deleting local files, putting up message windows, or querying various system parameters). Early browsers had no provisions for Java applets to reach outside of the sandbox. Recent browsers, however (Internet Explorer 4 on Windows etc), have provisions to give “trusted” applets the ability to work outside the sandbox. For this power to be granted to one of your applets, the applet’s code must be digitally signed with your unforgeable digital ID, and then the user must state that he trusts applets signed with your ID. The untrusted applet can request to have privileges outside the sandbox but will have to request the user for privileges every time it executes. But with the trusted applet the user can choose to remember their answer to the request, which means they won’t be asked again.