-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankUI.java
More file actions
142 lines (104 loc) · 3.9 KB
/
Copy pathBankUI.java
File metadata and controls
142 lines (104 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Java core packages
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
// Java extension packages
import javax.swing.*;
public class BankUI extends JPanel {
private static final long serialVersionUID = 3417040053089159204L;
// label text for GUI
protected final static String names[] = { "Account number", "First name",
"Last name", "Address", "SocSec", "Balance", "GPA", "Title", "Transaction Amount" };
// GUI components; protected for future subclass access
protected JLabel labels[];
protected JTextField fields[];
protected JButton doTask1, doTask2;
protected JPanel innerPanelCenter, innerPanelSouth;
// number of text fields in GUI
protected int size;
// constants representing text fields in GUI
public static final int ACCOUNT = 0, FIRSTNAME = 1, LASTNAME = 2, ADDRESS = 3, SOCSEC = 4,
BALANCE = 5, GPA = 6, TITLE = 7, TRANSACTION = 8;
// Set up GUI. Constructor argument of 4 creates four rows
// of GUI components. Constructor argument of 5 (used in a
// later program) creates five rows of GUI components.
public BankUI(int mySize) {
size = mySize;
labels = new JLabel[size];
fields = new JTextField[size];
// create labels
for (int count = 0; count < labels.length; count++)
labels[count] = new JLabel(names[count]);
// create text fields
for (int count = 0; count < fields.length; count++)
fields[count] = new JTextField();
// set focus for text fields
for(int x=0; x < size; x++){
final int fieldSize = x;
fields[x].addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_ENTER)
if(fieldSize == (size-1))
doTask2.requestFocus();
else fields[fieldSize+1].requestFocus();
}
});
}
// create panel to lay out labels and fields
innerPanelCenter = new JPanel();
innerPanelCenter.setLayout(new GridLayout(size, 2));
// attach labels and fields to innerPanelCenter
for (int count = 0; count < size; count++) {
innerPanelCenter.add(labels[count]);
innerPanelCenter.add(fields[count]);
}
// create generic buttons; no labels or event handlers
doTask1 = new JButton();
doTask2 = new JButton();
// create panel to lay out buttons and attach buttons
innerPanelSouth = new JPanel();
innerPanelSouth.add(doTask1);
innerPanelSouth.add(doTask2);
// set layout of this container and attach panels to it
setLayout(new BorderLayout());
add(innerPanelCenter, BorderLayout.CENTER);
add(innerPanelSouth, BorderLayout.SOUTH);
// validate layout
validate(); // make share panels fit.
// pack(); // make all panels in constant sizes
} // end constructor
// return reference to generic task button doTask1
public JButton getDoTask1Button() {
return doTask1;
}
// return reference to generic task button doTask2
public JButton getDoTask2Button() {
return doTask2;
}
// return reference to fields array of JTextFields
public JTextField[] getFields() {
return fields;
}
// clear content of text fields
public void clearFields() {
for (int count = 0; count < size; count++)
fields[count].setText("");
}
// set text field values; throw IllegalArgumentException if
// incorrect number of Strings in argument
public void setFieldValues(String strings[])
throws IllegalArgumentException {
if (strings.length != size)
throw new IllegalArgumentException("There must be " + size
+ " Strings in the array");
for (int count = 0; count < size; count++)
fields[count].setText(strings[count]);
}
// get array of Strings with current text field contents
public String[] getFieldValues() {
String values[] = new String[size];
for (int count = 0; count < size; count++)
values[count] = fields[count].getText();
return values; //&values[0]
}
} // end class BankUI