-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJFrameDelete.java
More file actions
272 lines (251 loc) · 11.9 KB
/
JFrameDelete.java
File metadata and controls
272 lines (251 loc) · 11.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javadevs;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import javax.swing.JOptionPane;
/**
*
* @author A
*/
public class JFrameDelete extends javax.swing.JFrame {
LinkedList<LinkedList<String>> sublist = new LinkedList<>();
JFrameList list = new JFrameList();
/**
* Creates new form JFrameDelete
*/
public JFrameDelete() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jWhole = new javax.swing.JButton();
jRecord = new javax.swing.JButton();
jReturn = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
jLabel1.setText("Choose an option from below:");
jWhole.setText("Delete the whole base");
jWhole.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jWholeActionPerformed(evt);
}
});
jWhole.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jWholeKeyPressed(evt);
}
});
jRecord.setText("Delete a specific record");
jRecord.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRecordActionPerformed(evt);
}
});
jRecord.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jRecordKeyPressed(evt);
}
});
jReturn.setText("Return to basic menu");
jReturn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jReturnActionPerformed(evt);
}
});
jReturn.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jReturnKeyPressed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(117, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(100, 100, 100))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jRecord, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jWhole, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jReturn, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(128, 128, 128))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jWhole, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jRecord, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(22, 22, 22)
.addComponent(jReturn)
.addContainerGap(35, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jWholeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jWholeActionPerformed
// TODO add your handling code here:
sublist = list.getList();
if (sublist.isEmpty()) {
JOptionPane.showMessageDialog(null, "The DataBase is already empty");
} else {
String[] options = { "Yes", "No" };
int x = JOptionPane.showOptionDialog(null, "Are you sure you want to delete the whole base?", "Delete the base", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
if (x == 0){
sublist.clear();
list.setList(sublist);
}
}
}//GEN-LAST:event_jWholeActionPerformed
private void jRecordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRecordActionPerformed
// TODO add your handling code here:
sublist = list.getList();
int pos = -1;
boolean found = false;
int i = 0;
if (sublist.indexOf(sublist.get(0).getLast())== 0) {
JOptionPane.showMessageDialog(null, "The DataBase has no records");
} else {
String ans = JOptionPane.showInputDialog("Type the primary key of the record you want to delete");
while (i < sublist.get(0).size() & !found) {
if (ans.equals(sublist.get(0).get(i))) {
found = true;
pos = i;
}
i++;
}
if (pos == -1) {
String[] options = {"Yes", "No"};
int x = JOptionPane.showOptionDialog(null, "There is no such record.Do you want to try again?", "Record not found", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
if (x == 0) {
jRecordActionPerformed(evt);
}
} else {
for (int j = 0; j < sublist.size(); j++) {
sublist.get(j).remove(pos);
}
}
list.setList(sublist);
}
}//GEN-LAST:event_jRecordActionPerformed
private void jReturnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jReturnActionPerformed
// TODO add your handling code here:
JFrameDelete.this.setVisible(false);
new JFrameMenu().setVisible(true);
}//GEN-LAST:event_jReturnActionPerformed
private void jReturnKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jReturnKeyPressed
// TODO add your handling code here:
if (evt.getKeyCode()==KeyEvent.VK_ENTER){
JFrameDelete.this.setVisible(false);
new JFrameMenu().setVisible(true);
}
}//GEN-LAST:event_jReturnKeyPressed
private void jWholeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jWholeKeyPressed
// TODO add your handling code here:
sublist = list.getList();
if (evt.getKeyCode()==KeyEvent.VK_ENTER){
if (sublist.isEmpty()) {
JOptionPane.showMessageDialog(null, "The DataBase is already empty");
} else {
String[] options = { "Yes", "No" };
int x = JOptionPane.showOptionDialog(null, "Are you sure you want to delete the whole base?", "Delete the base", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
if (x == 0){
sublist.clear();
list.setList(sublist);
int restart = JOptionPane.showOptionDialog(null, "Do you want to create a new data base?", null, JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
if (restart == 0) {
JFrameDelete.this.setVisible(false);
new JFrame1().setVisible(true);
} else {
System.exit(0);
}
}
}
}
}//GEN-LAST:event_jWholeKeyPressed
private void jRecordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jRecordKeyPressed
// TODO add your handling code here:
sublist = list.getList();
if (evt.getKeyCode()==KeyEvent.VK_ENTER){
String ans = JOptionPane.showInputDialog("Type the primary key of the record you want to delete");
int pos = -1;
boolean found = false;
int i = 0;
while (i < sublist.get(0).size() & !found) {
if (ans.equals(sublist.get(0).get(i))) {
found = true;
pos = i;
}
i++;
}
if (pos == -1) {
String[] options = {"Yes", "No"};
int x = JOptionPane.showOptionDialog(null, "There is no such record.Do you want to try again?", "Record not found", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
if (x == 0) {
jRecordKeyPressed(evt);
}
} else {
for (int j = 0; j < sublist.size(); j++) {
sublist.get(j).remove(pos);
}
}
list.setList(sublist);
}
}//GEN-LAST:event_jRecordKeyPressed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(JFrameDelete.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JFrameDelete.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JFrameDelete.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JFrameDelete.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFrameDelete().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JButton jRecord;
private javax.swing.JButton jReturn;
private javax.swing.JButton jWhole;
// End of variables declaration//GEN-END:variables
}