1 / 70

Object-Oriented Programming (Java), Unit 27

Object-Oriented Programming (Java), Unit 27. Kirk Scott. Focus. 27.1 Basic Focus 27.2 More on Focus 27.3 Assignment. Grooming claw From Wikipedia, the free encyclopedia   (Redirected from Toilet claw ) Jump to: navigation , search

min
Télécharger la présentation

Object-Oriented Programming (Java), Unit 27

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Programming (Java), Unit 27 Kirk Scott

  2. Focus • 27.1 Basic Focus • 27.2 More on Focus • 27.3 Assignment

  3. Grooming claw • From Wikipedia, the free encyclopedia •   (Redirected from Toilet claw) • Jump to: navigation, search • A grooming claw (or toilet claw) is the specialized claw or nail on the foot of certain primates, used for personal grooming. All prosimians have a grooming claw, but the digit that is specialized in this manner varies.[1]

  4. Tarsiers have a grooming claw on second and third toes. With one possible exception, in the suborder Strepsirrhini, which includes lemurs, galagos and lorises, the grooming claw is on the second toe. The possible exception is the aye-aye, which has claws instead of nails on toes 2 through 5. There is some debate concerning whether any of these claws (and if so which ones) are grooming claws.[citation needed] Less commonly known, a grooming claw is also found on the second pedal digit of owl monkeys (Aotus), titis (Callicebus), and possibly other New World monkeys.[2]

  5. The first toe is the large one, the equivalent of a human big toe. However, in all these prosimians the foot is more or less hand-like. The first toe is opposable, like a human thumb, and the second and third toes correspond approximately to the index and middle fingers. • Like a claw or a nail, the grooming claw is also made of keratin. It resembles a claw in both its lateral compression and longitudinal curvature. However, the tip is not as pointed, and it always stands at a steeper angle, a characteristic that also distinguishes it from a nail.[1]

  6. Function • The grooming claw is used in personal grooming to rake through the fur or scratch, particularly around the head and neck.[2] • Close-up of a ruffed lemur's foot, showing the toilet-claw on the second toe and nails on all other toes

  7. Meerkats

  8. (Cooperative Breeding) An older female watches over pups while alpha female is away.

  9. Because the topic of focus is so important in graphical applications, it is given its own unit. • However, the unit is short because it is limited to a brief introduction to the topic and a simple example. • You can find much more information about focus in the Java Tutorial or in the Java Focus subsystem specifications. • There is a link to both of these sources of information from the documentation for the Component class in the Java API documentation.

  10. One earlier example depended on the explicit use of focus—the example that was responsive to keystrokes. • The previous example programs that were responsive to mouse contained only one line that explicitly made use of focus. • It was necessary to make the panel focusable. • However, focus is a broader topic.

  11. Going back to ClickCup, for example, that application contained a panel, which contained rectangles representing cups. • The panel also contained a mouse listener. • When the mouse was clicked over the panel, this event was passed to the application. • The mouse listener contained code to determine whether the click occurred inside one of the rectangles representing the cups.

  12. When the application was running, the whole panel was active or responsive. • It would be possible to think of the panel as having the focus for the application. • Applications containing system supplied components help expand on this idea.

  13. Take the most recent program, MiscLabel, for example. • There are no mouse listeners for either the overall panel in the application or the sub-panels that components are added to. • This program contains text fields which have their own listeners.

  14. When the mouse is clicked over any one of the text fields, the cursor appears in that field and it is possible to type text into it. • When the cursor appears in a text field, that field has focus. • MiscLabel also has a button. • When the mouse is clicked over the button, the button receives the focus.

  15. Because text fields and buttons are system supplied components, the system takes care of focus for them. • The focus system is also related to the handling of key events and associating them with components in an application. • The ClickKey example made simple use of key events.

  16. The whole panel of the application was responsive to the clicked keys. • The call to setFocusable(true) was made in the panel class’s constructor • There was a listener that took certain actions depending on which cup was active and which key was clicked. • However, the focus in the application never changed.

  17. Only one component in an application at a time can have the focus. • It is possible to traverse the focusable components of an application by hitting the TAB key as well as clicking on them with the mouse. • The components can be traversed in reverse order by using the combination shift+TAB.

  18. You may already be familiar with this from using Windows based applications with multiple buttons. • Suppose you don’t have a mouse or the mouse is inactive. • You can tab from one button to the next causing each one in turn to have the focus. • Then the button can be selected by hitting the enter key or the space bar.

  19. If an application has a text area, it is possible to enter a TAB as a character in the area. • Instead of using TAB to traverse to the next component, it is possible to use the key combination CTRL+TAB.

  20. MiscFocus • In addition to the default focus behavior, it is possible to write specific focus behavior into application code. • The MiscFocus example illustrates this. • It is written so that when a new value is entered in one of the text fields the cursor moves to the other text field.

  21. In this application each register has an instance variable holding a reference to the other register, which should receive the focus next. • This instance variable is set to the other register when each register is being constructed. • There is no basic change in the appearance of the application. • It’s not easy to see, but the screenshot does show the cursor in the text field which currently has focus.

  22. In MiscFocus, the register objects have references to each other. • This could be indicated in a UML diagram by putting a “has-a” aggregation link from the register class to itself. • No diagram is given because this is only a minor change visually, and what it means can only be understood by referring to written explanations such as this paragraph anyway.

  23. The code at the beginning of the MiscFocusPanel class follows. • The panel has two registers, registerA and registerB. • At the beginning of the class constructor the two registers are constructed and they are passed references to each other.

  24. class MiscFocusPanel extends JPanel • { • private MiscFocusRegisterregisterA; • private MiscFocusRegisterregisterB; • public MiscFocusPanel() • { • registerA = new MiscFocusRegister("11110000"); • registerB = new MiscFocusRegister("00001111"); • registerA.setRegisterToPassFocusTo(registerB); • registerB.setRegisterToPassFocusTo(registerA);

  25. The code at the beginning of the MiscFocusRegister class follows. • Each register has these instance variables: registerByte, myField, and registerToPassFocusTo.

  26. class MiscFocusRegister • { • private MiscFocusByteregisterByte; • private JTextFieldmyField; • private MiscFocusRegisterregisterToPassFocusTo;

  27. The getMyField() method follows. • It is called in the actionPerformed() method of the text field listener.

  28. public JTextFieldgetMyField() • { • return myField; • }

  29. The setRegisterToPassFocusTo() method which is called in the panel constructor follows.

  30. public void setRegisterToPassFocusTo(MiscFocusRegisterregisterIn) • { • registerToPassFocusTo = registerIn; • }

  31. The code for the TextFieldListener, an inner class, follows. • The last line of code in the actionPerformed() method accomplishes the switch of focus between one register and the other.

  32. private class TextFieldListener implements ActionListener • { • public void actionPerformed(ActionEvent event) • { • String inputString = myField.getText(); • registerByte.setByteToThisString(inputString); • registerToPassFocusTo.getMyField().requestFocusInWindow(); • } • }

  33. In the listener attached to one register’s text field you have access to the reference to the other register. • You then call the get method to obtain a reference to its field. • Notice that technically this is a violation of encapsulation.

  34. The method requestFocusInWindow() is called on the field. • That method is inherited from the JComponent class. • It is that call which transfers the focus from one field to the other.

  35. 27.2 More on Focus • Focus is an extensive topic. • This section mentions a few aspects of focus more advanced than what was illustrated by MiscFocus. • By noting some ideas and classes, this section should serve as an entry into the Java API documentation and the tutorials on this topic. • More complete information on how to use these aspects of Java is available there.

  36. There are many different scenarios where code might affect focus in an application. • It might be desirable to create a non-system supplied component which is capable of receiving focus. • It might be desirable to override the default focus behavior of an application, such as the traversal order of components.

  37. It might be desirable to introduce new behavior, in addition to the default. • It might be desirable to have the application respond to keys other than the defaults. • It might be desirable to write special purpose code so that the application exhibits similar behavior across platforms.

  38. The tutorial points out that in Microsoft Windows, the frontmost window in an application has focus by default • In Solaris the window which the mouse pointer is over has focus. • Code could be written so that an application followed one of these two conventions consistently.

  39. A fuller understanding of focus depends on the ideas of containers and layouts. • Any Java component which is a container serves as the root of a traversal cycle for all of its focusable components. • A given application may have more than one container, so at an upper level it’s possible to cycle among the containers.

  40. Each container may have a layout. • Recall that the appearance of the components in a layout depends on the order in which they were added to the container. • In the same way, the default traversal order for the components in the container corresponds to the order in which the components were added to the container.

  41. In the MiscFocus example there is just one overall container, the frame, which has a content pane which contains the overall panel for the application. • This panel contains multiple components, each in a panel of its own. • Traversal occurs between these components in their individual panels according to the GridLayout of the overall panel.

  42. In order for a programmer-written component to be focusable, it has to have a visual representation and be enabled. • To make it focusable in code, this call is made: • component.setFocusable(true); • You may recall that this line of code appeared in the constructor of the ClickKeyPanel • setFocusable(true);

  43. Referring back to the ClickKey example, in that case only the panel received keystrokes. • It would also be possible to write the code in such a way that each cup had its own listener, and when the cup had focus, the listener would cause it to respond to various keystrokes in various ways. • A future example, MiscAction, will explore this idea further.

  44. If a change in the traversal order of the components of an application is desired, a simple, logical approach is to change the order in which components are added to their container. • It is also possible to remove a component from the traversal cycle by making it unable to receive focus. • That is accomplished with this call: • component.setFocusable(false);

  45. In general, focus traversal depends on this class: LayoutFocusTraversalPolicy. • As you can see by the name, the traversal policy is associated with the layout. • To make major changes in focus traversal policy, it is possible to extend the FocusTraversalPolicy class, make an instance of this class, and then add it to the container with a call like this: • container.setFocusTraversalPolicy(instance of policy);

  46. Just as a container has a layout, it can have a custom traversal policy associated with its layout, which overrides the default behavior described earlier. • It is also possible to change which keys in an application will cause traversal.

  47. Focus and traversal can interact with the logic of a program. • For example, Java has an InputVerifier class. • This can be used to make sure that text entered into fields agrees with a predefined format. • For a component like a text field, the focus may be passed to another component when text is received due to pressing the enter key.

  48. Input verification is triggered by the attempted change in focus, but if the input doesn’t agree with the specifications, the focus is not shifted. • The focus remains on the same field so that correct text can be entered. • In other words, the application won’t let you leave the text field until a correct value is entered. • You have probably encountered behavior like this in Web forms before.

More Related