Lesson 5.2: Graphical User Interface Design Flashcards
Why are the AWT components referred to as heavyweight?
The AWT defines a basic set of components that support a usable، but limited، graphical interface. One reason for the limited nature of the AWT is that it translates its various visual components into their corresponding، platform-specific equivalents، or peers. This means that the look and feel of an AWT component is defined by the platform، not by Java. Because the AWT components use native code resources، they are referred to as heavyweight.
What were the three main problems in the use of native peers in the AWT environment?
The use of native peers led to several problems. First، because of differences between operating systems، a component might look، or even act، differently on different platforms. This potential variability threatened the overarching philosophy of Java: write once، run anywhere. Second، the look and feel of each component was fixed (because it is defined by the platform) and could not be (easily) changed. Third، the use of heavyweight components caused some frustrating restrictions. For example، a heavyweight component was always opaque.
When and how was swing introduced to java?
Not long after Java’s original release، it became apparent that the limitations and restrictions present in the AWT were sufficiently serious that a better approach was needed. The solution was Swing. Introduced in 1997، Swing was included as part of the Java Foundation Classes (JFC). Swing was initially available for use with Java 1.1 as a separate library. However، beginning with Java 1.2، Swing (and the rest of JFC) was fully integrated into Java.
How does Swing address the limitations associated with the AWT’s components?
Swing addresses the limitations associated with the AWT’s components through the use of two key features: lightweight components and a pluggable look and feel.
What does it mean when we say that Swing has lightweight components?
With very few exceptions، Swing components are lightweight. This means that a component is written entirely in Java. They do not rely on platform-specific peers. Lightweight components have some important advantages، including efficiency and flexibility. Furthermore، because lightweight components do not translate into platform-specific peers، the look and feel of each component is determined by Swing، not by the underlying operating system. This means that each component can work in a consistent manner across all platforms.
What does MVC mean and what do its parts do individually?
Swing’s pluggable look and feel is made possible because Swing uses a modified version of the classic model-view-controller (MVC) architecture. In MVC terminology، the model corresponds to the state information associated with the component. For example، in the case of a check box، 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، including any aspects of the view that are affected by the current state of the model. The controller determines how the component reacts to the user. For example، when the user clicks a check box، the controller reacts by changing the model to reflect the user’s choice (checked or unchecked). This then results in the view being updated.
How does Java implement its own version of MVC?
Although the MVC architecture and the principles behind it are conceptually sound، the high level of separation between the view and the controller was not beneficial for Swing components. Instead، Swing uses a modified version of MVC that combines the view and the controller into a single logical entity called the UI delegate. For this reason، Swing’s approach is called either the model-delegate architecture or the separable model architecture. Therefore، although Swing’s component architecture is based on MVC، it does not use a classical implementation of it
What is are events in Java? What superclasses are they found in?
An event is an object that describes a state change in an event source. It can be generated as a consequence of a person interacting with an element in a graphical user interface or generated under program control. For example، an event is generated when the user clicks a button، moves the mouse، types a key، or selects an item from a list. Events can also be generated in other ways. The superclass for all events is java.util.EventObject. Many events are declared in java.awt.event. Events specifically related to Swing are found in javax.swing.event.
How does the event handling mechanism work، what is it called، and what are its advantages?
The event handling mechanism used by Swing is called the delegation event model. Its concept is quite simple. An event source generates an event and sends it to one or more listeners. With this approach، the listener simply waits until it receives an event. Once an event arrives، the listener processes the event and then returns. The advantage of this design is that the application logic that processes events is cleanly separated from the user interface logic that generates the events. Therefore، a user interface element is able to “delegate” the handling of an event to a separate piece of code. In the delegation event model، a listener must register with a source in order to receive an event.
What are source events and how do listeners interact with them? What is the general form of the code that is used to register a listener to an event source?
An event source is an object that generates an event. When a source generates an event، it sends that event to all registered listeners. Therefore، in order for a listener to receive an event، it must register with the source of that event. In Swing، listeners register with a source by calling a method on the event source object. Each type of event has its own registration method. Typically، events use the following naming convention: public void addTypeListener(TypeListener el) Here، Type is the name of the event and el is a reference to the event listener. For example، the method that registers a keyboard event listener is called addKeyListener( ). The method that registers a mouse motion listener is called addMouseMotionListener( ). When an event occurs، the event is passed to all registered listeners.
In addition to registering a listener with an event source، what must you also do in that relationship? Think of what happens after the trigger of an event، what happens after that? What is the general form of this next action?
A source must also provide a method that allows a listener to unregister an interest in a specific type of event. In Swing، the naming convention of such a method is this: public void removeTypeListener(TypeListener el) Again، Type is the name of the event and el is a reference to the event listener. For example، to remove a keyboard listener، you would call removeKeyListener( ).
How fast should an event handler do its job، as a rule of thumb?
An event handler should do its job quickly and then return. In most cases، it should not engage in a long operation because doing so will slow down the entire application. If a time-consuming operation is required، then a separate thread should be created for this purpose.
What is the root of event class hierarchy? Where is the AWTEvent declared and what is it the super class of? Swing uses the AWT events but where are those events located?
At the root of the event class hierarchy is EventObject، which is in java.util. It is the superclass for all events in Java. The class AWTEvent، declared in the java.awt package، is a subclass of EventObject. It is the superclass (either directly or indirectly) of all AWT-based events used by the delegation event model. Although Swing uses the AWT events، it also adds several of its own. As mentioned، these are in javax.swing.event. Thus، Swing supports a large number of events. However، in this chapter only three are used. They are shown here، along with their corresponding listener.
What do the swing event classes ActionEvent، ItemEvent، and ListSelectionEvent do?
–Event Class / Description / Corresponding Event Listener -ActionEvent / Generated when an action occurs within a control، such as when a button is clicked. / ActionListener ItemEvent / Generated when an item is selected such as when a check box is clicked. / ItemListener ListSelectionEvent / Generated when a list selection changes. / ListSelectionListener
Create result image using swing (A simple GUI box with two buttons inside it labled “Up” and “Down”).
ButtonDemo Program with Swing.png