Download presentation
Presentation is loading. Please wait.
Published byPearl Thomas Modified over 9 years ago
1
Object-Oriented Programming (Java), Unit 28 Kirk Scott 1
2
Actions, Multicasting, and Text Areas 28.1 Actions and Multicasting 28.2 Text Areas 2
3
28.1 Actions and Multicasting 3
4
MiscAction This program shows how to set up an action so that the key combination CTRL+S has the same effect as clicking the Swap button. This is done by creating an action object and attaching it to both the button and the keystroke combination. This makes it possible for more than one event, a button click or a keystroke for example, to trigger the same listener. 4
5
It is only necessary to make changes in the panel class in order to accomplish this. This solution to handling keyboard input is more general than the ClickKey example given previously. A screenshot of the application is shown below. There is no change in the appearance of this example from the previous example. 5
6
6
7
The following UML diagram is not a diagram of the complete application. It shows the structure of the subset of the application which is new or different from previous examples. 7
8
8
9
The code is shown for that part of the application where there are changes or new elements. The parts of the MiscActionPanel() constructor which are different from the panel constructor in the previous example are given on the next overhead. 9
10
public MiscActionPanel() { … Action swapActionObject = new SwapAction("Swap"); JButton myButton = new JButton(swapActionObject); JPanel buttonPanel = new JPanel(); buttonPanel.add(myButton); add(buttonPanel); InputMap inmap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); inmap.put(KeyStroke.getKeyStroke("ctrl S"), "registerswap"); ActionMap actmap = getActionMap(); actmap.put("registerswap", swapActionObject); } 10
11
The class SwapAction is a new inner class of the MiscActionPanel class. The code for SwapAction is given on the following overhead. 11
12
private class SwapAction extends AbstractAction { public SwapAction(String name) { putValue(Action.NAME, name); putValue(Action.SHORT_DESCRIPTION, "Swap register contents."); } public void actionPerformed(ActionEvent event) { registerA.swapRegisterContents(registerB); } 12
13
MiscAction is a standalone example. The elements in it that support responsiveness to the keyboard are not retained in future examples. Keep in mind the point of the example: More than one event can trigger the same action. In other words, more than one different event can cause the same actionPerformed() method to be executed. 13
14
MiscMulti This program illustrates the idea of multicasting. In brief, this means many listeners for one event. In this example each text field has its own listener which listens for a single event, the clicking of the clear button. This is the converse of the previous example. 14
15
In the previous example, different events triggered the same listener. In this example one event triggers multiple listeners. This is referred to as multicasting. 15
16
The screenshot given on the following overhead shows the result of clicking the clear button. Both text fields have their values set to 00000000. 16
17
17
18
The following UML diagram is not a diagram of the complete application. It shows the structure of the subset of the application which is new or different from previous examples. It contains the class FieldButtonActionListener. The class name is meant to suggest that this is the listener for the button which affects the contents of the text fields. 18
19
19
20
On the following overhead the parts of the MiscMultiPanel() constructor which are different from the panel constructor in the previous example are given. Notice in particular that the button is passed to each of the registers when they are constructed. Each register can be affected by code in the button listener. 20
21
public MiscMultiPanel() { JButton myButton2 = new JButton("Clear"); registerA = new MiscMultiRegister("11110000", myButton2); registerB = new MiscMultiRegister("00001111", myButton2); … JPanel buttonPanel2 = new JPanel(); buttonPanel2.add(myButton2); add(buttonPanel2); } 21
22
The following overhead shows the MiscMultiRegister() constructor. In it an action listener is created and added to the button which is passed in as a parameter. Keep in mind that two registers are constructed in the application, so construction of a listener happens twice, once for each register. However, in both cases it is the same button that is passed in. 22
23
public MiscMultiRegister(String stringIn, JButton buttonIn) { registerByte = new MiscMultiByte(stringIn); myField = new JTextField(stringIn, 8); TextFieldListener myListener = new TextFieldListener(); myField.addActionListener(myListener); FieldButtonActionListener myActionListener = new FieldButtonActionListener(); buttonIn.addActionListener(myActionListener); } 23
24
The next code that will be shown is the listener class for the clear button. It is an inner class of the MiscMultiRegister class. The listener makes calls on registerByte and myField. Those are the objects that belong to a register, first being part of the model, the second being part of the view of the application. 24
25
There is just one button but there are two listeners. Each time a register is constructed, a listener is constructed. The same button is attached to two different register class listeners. 25
26
Each of the instances of the register class have a registerByte and a myField instance variable. When the button is clicked, both listeners are triggered 26
27
The listener code contains this call: registerByte.setByteToThisString(MiscMultiByte.junk); For the one listener, this applies to the registerByte instance variable of the instance of the outer class, MiscMultiRegister, that the listener is attached to. For the other listener, this applies to the registerByte of the MiscMultiRegister that it is attached to. 27
28
Similarly, the listener code also contains this call: myField.setText(registerByte.getStringFromByte()); Just like with the registerByte, for one of the listeners, this applies to the myField instance variable of the register it is attached to. For the other listener, it applies to the myField instance variable of the register it is attached to. 28
29
The complete code for the listener is given on the next overhead. 29
30
private class FieldButtonActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { registerByte.setByteToThisString(MiscMultiByte.junk); myField.setText(registerByte.getStringFromByte()); } 30
31
The listener code makes use of a static variable, MiscMultiByte.junk. This shows the declaration of the variable junk in the MiscMultiByte class. This provides the value in the listener which clears the register by setting their contents to all 0’s. public static final String junk = "00000000"; 31
32
MiscMulti is a standalone example. The elements in it that support a clear button with multicasting are not retained in future examples. 32
33
28.2 Text Areas The last unit reviewed the idea of registers on a chip and introduced them into the example applications. This section reviews the concept of memory on a chip and introduces a representation of it into the example applications. 33
34
In a computer, memory is the place where programs and data are stored for use by the CPU. Memory is measured in bytes, not bits. It may also be measured in words, where a word is some constant multiple of bytes. 34
35
Memory is addressable. This means that in order, beginning at 0, the words in memory are numbered. This makes it possible for machine language code to refer to locations in the program or to the locations which contain the values of program variables. 35
36
In the example programs, the contents of memory are held in an array of words where each word object contains 4 bytes. The bytes in memory are the same as the bytes in registers—they are sequences of 8 of the characters 0 and 1. 36
37
Memory is represented visually using a JTextArea which has scroll bars. A programming project based on this example would require addressability of memory. The next two example programs have elements which touch on this. 37
38
The first illustrates loading a program into memory. The second illustrates moving a byte to location 0 in memory. From that it is possible to infer how to move values to any location in memory. 38
39
MiscMemory This program illustrates the implementation of machine memory using a class, and representing it visually using a text area with scroll bars. The functionality of loading a file into the memory is implemented through a menu option. There is no explicit handling of focus in this example. The automatic traversal of components using the tab key is in effect. 39
40
However, once the user clicks on the text area and places the cursor there, the text area has focus. Until it loses focus, all keystrokes will be interpreted as text being entered into it. The keystrokes will not be interpreted as commands. 40
41
A key listener implemented as an inner class of a panel can be added to a text area. The text area will automatically receive keystrokes as input, but it is also possible to define actions for them at the same time in the listener code. This is not done in this example, but it is worth knowing that this is possible. 41
42
The screenshot following the next overhead shows the menu options. It also shows the application with the file sumtenV1.txt loaded into it. This is the example machine language program that can be run in a complete machine simulation The memory can hold 256 words. sumtenV1.txt is only 14 words long. 42
43
The application is written so that each word in memory appears on a separate line in the text area. It is also written so that the memory following the loaded program is filled with 0’s. When the machine language program is initially loaded, the text area scrolls to the bottom. The screenshot shows the area after manually scrolling back up to the top. 43
44
44
45
The UML diagram for this application is relatively complete, but it does not show every last detail. The purpose of the diagram is to give an overall impression of the application which emphasizes the key points. Notice that the MiscMemoryMemory class is shown as having an array, where this is given as another class labeled (Array) in the diagram. 45
46
In the code, the array containing the memory is not constructed using the syntax new Arrary(). It is constructed using the [] notation. Even though the name of the Array class doesn’t appear in the code, the array is shown in the UML diagram in order to bring out the fact that an array of words is used to implement memory 46
47
47
48
The code for MiscMemory is not listed here because it would take up eight pages. It is much more practical to download the code separately or to look at it online or through an editor. The UML diagram provides a guide to the places to look for important aspects of the implementation. 48
49
At this final stage, the example programs are cumulative. All of the elements of this example are retained in the next example. The application is approaching its final state. 49
50
MiscRegAndMem This program puts together registers and memory. It combines features of the MiscLabel and MiscMemory examples. It also implements an interaction between the registers and memory. Clicking the swap button causes the values in the registers to be swapped and it also causes the value in register A to be placed in offset 0, that is, address 0, of memory. 50
51
When the application is started the text area is completely empty. The registers are initialized as follows: Register A contains 11110000. Register B contains 00001111. The application still has a menu option to load a file like sumtenV1.txt into memory. The screenshot does not illustrate this. 51
52
The screenshot is given on the next overhead. It illustrates the functionality of clicking the Swap button in this version of the application. The values of registers A and B are exchanged. The value of register A is put at address 0 of memory. The rest of memory is filled with 0’s. 52
53
53
54
This example has the most extensive layout characteristics of any example so far. A fairly complete UML diagram is given because it is easier to see the overall structure of the application by looking at a diagram than by looking at code. 54
55
Note that all of the visual elements of the application appear in panels which are added to the content pane, and some of these panels have layouts of their own. The main panel has a grid layout. The UML diagram is given on the next overhead. 55
56
56
57
For all practical purposes at this point the example application is structurally complete. As with the last example, the code for this example is not shown because it’s too long. It is much more practical to download the code separately or to look at it online or through an editor. The UML diagram provides a guide to the places to look for important aspects of implementation. 57
58
The End 58
59
If I start doing unit 29 again, consider the handling of the different versions of Wari and consider what this random note was for: See the note at the end of the unit 29 powerpoint overheads about the three versions of WariV12 In the next unit a new menu option will be added which will cause multiple copies of the frame to be created. However, the contents of each of the frames will be the same and will include the functionality illustrated up to this point. 59
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.