-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGTable.java
More file actions
406 lines (347 loc) · 12.5 KB
/
GTable.java
File metadata and controls
406 lines (347 loc) · 12.5 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Date;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
public class GTable extends JFrame {
private JPanel contentPane;
// *******************************************************************************
private JTextField textField; //Textfield for Add Task and Edit Task GUI
private JTextField textField_1; //Textfield for Add Task and Edit Task GUI
JTextArea textEditTask; // for Edit Task method
JTextField textEditCatagory; // for Edit Task method
// DataBase Connection Class
private ToDoQueries tdq;
String[] columnNames = { "[ ]", "Task", "Category", "Date", "ID", "USER" };
Object[][] data = {};
final DefaultTableModel dtm = new DefaultTableModel(data, columnNames) {
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
return Boolean.class;
case 1:
return String.class;
case 2:
return String.class;
case 3:
return String.class;
default:
return String.class;
}
}
};
final JTable table = new JTable(dtm) {
// Got this code from Stackoverflow to make the GTable data non-editable
private static final long serialVersionUID = 1L;
// This function makes only the first column editable
// The rest of the columns are not editable.
public boolean isCellEditable(int row, int column) {
return false;
};
};
public void runTable() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
// Run the program
GTable frame = new GTable(tdq);
frame.setVisible(true);
WindowLogin login = new WindowLogin();
String username = login.getUsername();
frame.setTitle(username + "'s ToDo List");
ArrayList<ToDo> newData = tdq.getTodoList();
frame.addData(newData);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GTable(final ToDoQueries tdq) {
// set the todo queries
this.tdq = tdq;
// Make Sure it Exits on close
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set size of the new window
setBounds(100, 100, 600, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
// ContentPane for our GTable
setContentPane(contentPane);
contentPane.setLayout(null);
getContentPane().setBackground(new Color(204, 204, 255));
// Set Values for the table
// Sets the font for the table
table.setFont(new Font("Serif", Font.PLAIN, 20));
// Sets how tall the rows should be
table.setRowHeight(table.getRowHeight() + 20);
// Allows the user to select rows
table.setRowSelectionAllowed(true);
// Make sure the JTable takes up the entire scrollpane
table.setFillsViewportHeight(true);
table.setOpaque(false);
// Prevents the user from reordering the columns
table.getTableHeader().setReorderingAllowed(false);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
int column = table.getSelectedColumn();
int currentRow = table.convertRowIndexToModel(table.getSelectedRow());
if (column == 0) {
if (dtm.getValueAt(currentRow, 0).equals(true)) {
dtm.setValueAt(false, currentRow, 0);
tdq.markDone(currentRow + 1);
// marked Not Done by clicking checkbox
JOptionPane.showMessageDialog(null, "Task Marked Incomplete", "Done Window",
JOptionPane.INFORMATION_MESSAGE);
} else {
dtm.setValueAt(true, currentRow, 0);
tdq.markDone(currentRow + 1);
// marked Done by clicking checkbox
JOptionPane.showMessageDialog(null, "Task Marked Done", "Done Window",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
});
// Add Button - add the task
JButton btnAdd = new JButton("Add");
// Clicking the Mouse causes a dialog box to open
btnAdd.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
addTaskGui();
}
});
btnAdd.setBounds(10, 11, 100, 40);
contentPane.add(btnAdd);
// Edit Button - Edit the task
JButton btnEdit = new JButton("Edit");
// Clicking the Mouse causes a dialog box to open
btnEdit.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
if (table.getSelectedRow() != -1) {
editTaskGui();
} else
JOptionPane.showMessageDialog(null, "No Task Selected", "Edit Window", JOptionPane.WARNING_MESSAGE);
}
});
btnEdit.setBounds(120, 11, 100, 40);
contentPane.add(btnEdit);
// Done Button - Marks the task as done
JButton btnMarkDone = new JButton("Done");
btnMarkDone.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
if (table.getSelectedRow() != -1) {
int currentRow = table.convertRowIndexToModel(table.getSelectedRow());
if (dtm.getValueAt(currentRow, 0).equals(true)) {
dtm.setValueAt(false, currentRow, 0);
// Database markNotDone correctly working
JOptionPane.showMessageDialog(null, "Task Marked Incomplete", "Done Window",
JOptionPane.INFORMATION_MESSAGE);
tdq.markDone(currentRow+1);
} else {
dtm.setValueAt(true, currentRow, 0);
// Database markDone correctly working
JOptionPane.showMessageDialog(null, "Task Marked Done", "Done Window",
JOptionPane.INFORMATION_MESSAGE);
tdq.markDone(currentRow+1);
}
} else
JOptionPane.showMessageDialog(null, "No Task Selected", "Done Window", JOptionPane.WARNING_MESSAGE);
}
});
btnMarkDone.setBounds(230, 11, 100, 40);
contentPane.add(btnMarkDone);
// This makes sure that the GTable is scrollable
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 75, 565, 350);
contentPane.add(scrollPane);
scrollPane.setViewportView(table);
// Set Widths for the columns
table.getColumnModel().getColumn(0).setMaxWidth(25);
table.getColumnModel().getColumn(1).setPreferredWidth(200);
table.getColumnModel().getColumn(2).setPreferredWidth(50);
table.getColumnModel().getColumn(3).setPreferredWidth(50);
table.getColumnModel().getColumn(4).setPreferredWidth(0);
table.getColumnModel().getColumn(5).setPreferredWidth(0);
table.getColumnModel().getColumn(5).setMaxWidth(0);
table.getColumnModel().getColumn(4).setMaxWidth(0);
table.removeColumn(table.getColumnModel().getColumn(5));
table.removeColumn(table.getColumnModel().getColumn(4));
table.setForeground(Color.orange);
table.setBackground(Color.GRAY);
}
public void addTaskGui() {
final JFrame frameAdd = new JFrame("Add Task");
frameAdd.setSize(500, 300);
frameAdd.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frameAdd.setVisible(true);
// =====================Creating all necessary object===============
setLayout(null);
contentPane = new JPanel();
contentPane.setLayout(null);
frameAdd.add(contentPane);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(31, 48, 394, 25);
contentPane.add(scrollPane);
textField = new JTextField();
scrollPane.setViewportView(textField);
textField.setColumns(10);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(31, 127, 394, 25);
contentPane.add(scrollPane_1);
textField_1 = new JTextField();
scrollPane_1.setViewportView(textField_1);
textField_1.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) { // add using the
// ENTER button
dtm.addRow(new Object[] { false, textField.getText(), textField_1.getText(), "05-16-2016" });
// calling function to save into database from GUI
tdq.addTask(textField.getText(), textField_1.getText());
// added to Database
frameAdd.dispose();
}
}
});
textField_1.setColumns(10);
JLabel lblAddTask = new JLabel("Add Task");
lblAddTask.setBounds(31, 11, 88, 26);
lblAddTask.setFont(new Font("Arial", Font.PLAIN, 16));
contentPane.add(lblAddTask);
JLabel lblAddCategory = new JLabel("Add Category");
lblAddCategory.setBounds(31, 84, 138, 32);
lblAddCategory.setFont(new Font("Arial", Font.PLAIN, 16));
contentPane.add(lblAddCategory);
JButton btnCancel = new JButton("Cancel");
btnCancel.setBounds(80, 180, 107, 35);
contentPane.add(btnCancel);
JButton btnSave = new JButton("Save");
btnSave.setBounds(260, 180, 107, 35);
contentPane.add(btnSave);
// Cancel button's action
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frameAdd.dispose();
}
});
// Save button's action
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// add using clicking the Add button
dtm.addRow(new Object[] { false, textField.getText(), textField_1.getText(), "05-16-2016" });
// call function to save into database from GUI
tdq.addTask(textField.getText(), textField_1.getText());
// data added to Database
frameAdd.dispose();
}
});
}
public void editTaskGui() {
final int currentRow = table.convertRowIndexToModel(table.getSelectedRow());
final JFrame frameEdit = new JFrame("Edit Task" + currentRow);
frameEdit.setSize(500, 300);
frameEdit.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frameEdit.setVisible(true);
String Task = (String) table.getValueAt(currentRow, 1);
String Category = (String) table.getValueAt(currentRow, 2);
// ====================Creating all necessary object===============
setLayout(null);
contentPane = new JPanel();
contentPane.setLayout(null);
frameEdit.add(contentPane);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(31, 48, 394, 25);
contentPane.add(scrollPane);
textField = new JTextField(Task);
scrollPane.setViewportView(textField);
textField.setColumns(10);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(31, 127, 394, 25);
contentPane.add(scrollPane_1);
textField_1 = new JTextField(Category);
scrollPane_1.setViewportView(textField_1);
textField_1.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
dtm.setValueAt(textField.getText(), currentRow, 1);
dtm.setValueAt(textField_1.getText(), currentRow, 2);
// Edit Task in Database from GUI
tdq.editTask(currentRow + 1, textField.getText());
frameEdit.dispose();
}
}
});
textField_1.setColumns(10);
JLabel lblAddTask = new JLabel("Edit Task");
lblAddTask.setBounds(31, 11, 88, 26);
lblAddTask.setFont(new Font("Arial", Font.PLAIN, 16));
contentPane.add(lblAddTask);
JLabel lblAddCategory = new JLabel("Edit Category");
lblAddCategory.setBounds(31, 84, 138, 32);
lblAddCategory.setFont(new Font("Arial", Font.PLAIN, 16));
contentPane.add(lblAddCategory);
JButton btnCancel = new JButton("Cancel");
btnCancel.setBounds(80, 180, 107, 35);
contentPane.add(btnCancel);
JButton btnSave = new JButton("Save");
btnSave.setBounds(260, 180, 107, 35);
contentPane.add(btnSave);
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frameEdit.dispose();
}
});
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dtm.setValueAt(textField.getText(), currentRow, 1);
dtm.setValueAt(textField_1.getText(), currentRow, 2);
// calling function to edit into database
tdq.editTask(currentRow + 1, textField.getText());
frameEdit.dispose();
}
});
}
public final void addData(ArrayList<ToDo> data) {
for (ToDo item : data) {
boolean isDone = item.isTaskDone();
String task = item.getTask();
String category = item.getCategory();
Date date = item.getDateAdded();
Object[] toAdd = { isDone, task, category, date };
// NOTE: Code for Debugging
// System.out.println("data added");
dtm.addRow(toAdd);
}
}
public DefaultTableModel getModel() {
return dtm;
}
public static void clearTable(JTable table) {
DefaultTableModel dtm = new DefaultTableModel();
table.setModel(dtm);
}
}