object-oriented programming (java), unit 27 kirk scott 1

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

Upload: kerry-barker

Post on 26-Dec-2015

224 views

Category:

Documents


1 download

TRANSCRIPT

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

1

Object-Oriented Programming (Java), Unit 27

Kirk Scott

Page 2: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

2

Focus

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

Page 3: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

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]

Page 4: Object-Oriented Programming (Java), Unit 27 Kirk Scott 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]

Page 5: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

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]

Page 6: Object-Oriented Programming (Java), Unit 27 Kirk Scott 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

Page 7: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

7

Page 8: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

8

Meerkats

Page 9: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

9

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

Page 10: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

10

• 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.

Page 11: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

11

• 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.

Page 12: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

12

• 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.

Page 13: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

13

• 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.

Page 14: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

14

• 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.

Page 15: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

15

• 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.

Page 16: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

16

• 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.

Page 17: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

17

• 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.

Page 18: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

18

• 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.

Page 19: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

19

• 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.

Page 20: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

20

• 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.

Page 21: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

21

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.

Page 22: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

22

• 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.

Page 23: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

23

Page 24: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

24

• 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.

Page 25: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

25

• 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.

Page 26: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

26

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

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

27

• The code at the beginning of the MiscFocusRegister class follows.

• Each register has these instance variables: registerByte, myField, and registerToPassFocusTo.

Page 28: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

28

• class MiscFocusRegister• {• private MiscFocusByte registerByte;• private JTextField myField;• private MiscFocusRegister registerToPassFocusTo;

Page 29: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

29

• The getMyField() method follows. • It is called in the actionPerformed() method of

the text field listener.

Page 30: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

30

• public JTextField getMyField()• {• return myField;• }

Page 31: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

31

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

Page 32: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

32

• public void setRegisterToPassFocusTo(MiscFocusRegister registerIn)• {• registerToPassFocusTo = registerIn;• }

Page 33: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

33

• 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.

Page 34: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

34

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

registerToPassFocusTo.getMyField().requestFocusInWindow();• }• }

Page 35: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

35

• 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.

Page 36: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

36

• 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.

Page 37: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

37

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.

Page 38: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

38

• 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.

Page 39: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

39

• 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.

Page 40: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

40

• 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.

Page 41: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

41

• 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.

Page 42: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

42

• 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.

Page 43: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

43

• 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.

Page 44: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

44

• 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);

Page 45: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

45

• 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.

Page 46: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

46

• 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);

Page 47: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

47

• 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);

Page 48: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

48

• 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.

Page 49: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

49

• 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.

Page 50: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

50

• 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.

Page 51: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

51

• When writing code that explicitly works with focus, it may be desirable to track the focus changes over multiple components.

• That is, at various points you may want to get information on which component currently has focus.

• There are three classes that are relevant to this. • An instance of the FocusListener class can

inquire about focus.

Page 52: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

52

• The KeyBoardFocusManager class is the main class for focus handling.

• It has API’s for inquiring about focus, for initiating focus changes, and for replacing default focus event dispatching with custom dispatching.

Page 53: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

53

• An instance of the PropertyChangeListener class can inquire about focus when applied to an instance of the KeyBoardFocusManager class for the application.

• The property change listener can then be coded to take appropriate action based on the focus situation it finds.

Page 54: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

54

• The Java documentation lists eight concepts involved in the KeyBoardFocusManager class.

• The general idea of most of them has been touched on empirically in the discussion above.

• They are listed on the next overhead.

Page 55: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

55

• 1. The focus owner (a component).• 2. The permanent focus owner (a component).• 3. The focused Window (the frame or dialog which

contains the focus owner component).• 4. The active Window (the frame or dialog which either

has the focus or owns the frame or dialog with the focus).• 5. Focus traversal.• 6. Focus traversal cycle.• 7. Focus cycle root.• 8. Focus traversal policy.

Page 56: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

56

• The general model for focus traversal is based on a set of events which are defined in the WindowEvent and FocusEvent classes.

• The model can be explained with a simple scenario:

• Suppose an application has two windows, each containing a component.

Page 57: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

57

• Let AWindow contain AComponent and BWindow contain BComponent.

• Suppose that AComponent currently has the focus and the user clicks on BComponent.

• This is the full sequence of focus events which is generated and handled when this action is taken:

Page 58: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

58

• 1. AComponent will receive a FocusEvent.FOCUS_LOST event.• 2. AWindow will receive a WindowEvent.WINDOW_LOST_FOCUS

event.• 3. AWindow will receive a

WindowEvent.WINDOW_DEACTIVATED event.• • 4. BWindow will receive a WindowEvent.WINDOW_ACTIVATED

event.• 5. BWindow will receive a

WindowEvent.WINDOW_GAINED_FOCUS event.• 6. BComponent will receive a FocusEvent.FOCUS_GAINED event.

Page 59: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

59

• Notice that this pattern is symmetrical. • When writing custom code to deal with focus,

it’s necessary to be aware of and make use of this pattern.

• Overlooking a step can lead to problems.

Page 60: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

60

• When running code you may also experience unexpected delays in the change of focus from one component to another.

• Such problems can be caused by the following situation:

• An instruction is issued for a component to gain focus, but the component that currently has it has not yet released it.

Page 61: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

61

• Sometimes these problems can be fixed simply by re-ordering the instructions in your code.

• In complex applications the solution may not be so straightforward.

Page 62: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

62

• This unit has provided an introduction to the topic of focus.

• Enough information was given to explain generally what is happening in the example program, MiscFocus.

Page 63: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

63

• Additional information was given in order to suggest some of the machinery that supports the functionality of MiscFocus, and to suggest how programs with more complex focus behavior could be developed.

• If desired, the Java API documentation and the Java tutorial should be consulted for the details on how to use focus more fully.

Page 64: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

64

27.3 Assignment

• Do not forget that there is an assignment for this unit, unlike the other units following Unit 23.

• Note the due date in the syllabus for this last, stand-alone assignment before the final project.

• The purpose of the project is to keep you from getting slothful before starting on the final project.

Page 65: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

65

• Finding a solution to the Unit 27 assignment should get you started on ideas you will need to use in doing the final project.

• A screen shot of the assignment’s output is given on the next overhead.

• This is followed by a brief description of what it includes.

Page 66: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

66

Page 67: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

67

Unit 27 AssignmentGrading check-off checklist

• 1. Your solution to this problem should have a grid layout containing four sets of elements consisting of a label, a button, and a text field. 10 pts.

• 2. In your application, focus should be implemented in such a way that pressing the enter key in a text field causes the cursor to move to the next field. 10 pts.

• 3. Clicking a button in the application should cause the integer contents of the text field belonging to it to be added to the contents of the next text field. 10 pts.

Page 68: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

68

The End

Page 69: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

69

Page 70: Object-Oriented Programming (Java), Unit 27 Kirk Scott 1

70

• [?]• See the note at the end of the unit 29

powerpoint overheads about the three versions of WariV12