-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisplayArtistsForm.java
148 lines (130 loc) · 5.31 KB
/
DisplayArtistsForm.java
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
/**
* @author Yannis Exidaridis <[email protected]>
* @brief display artists form
**/
package albums;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
*
* @author yannis
*/
public class DisplayArtistsForm extends javax.swing.JFrame {
private JButton jButtonBack;
private JButton jButtonDelete;
private JButton jButtonUpdate;
private JTable jTableData;
private JScrollPane TableScrollPane;
private Container container;
private ArtistsModel TableModel;
private final Database db;
private String ArtistName;
public DisplayArtistsForm(String SearchName, Integer NumberOfArtists) {
super();
db = new Database();
ResultDataForm(SearchName, NumberOfArtists);
}
private void ResultDataForm(String SearchName, Integer NumberOfArtists) {
setResizable(false);
// add events
jButtonBack = new JButton("Back");
jButtonBack.addActionListener((java.awt.event.ActionEvent evt) -> {
jButtonBackActionPerformed(evt);
});
jButtonDelete = new JButton("Delete");
jButtonDelete.addActionListener((java.awt.event.ActionEvent evt) -> {
jButtonDeleteActionPerformer(evt);
});
jButtonUpdate = new JButton("Update");
jButtonUpdate.addActionListener((java.awt.event.ActionEvent evt) -> {
jButtonUpdateActionPerformed(evt);
});
// count artists
ResultSet rs = db.SearchArtist(SearchName);
TableModel = new ArtistsModel(NumberOfArtists);
try {
while (rs.next()) {
Integer RowNumber = rs.getRow()-1;
TableModel.setValueAt(rs.getString(db.get_fld_artist()), RowNumber, 0);
TableModel.setOldValueAt(rs.getString(db.get_fld_artist()), RowNumber, 0);
}
} catch (SQLException ex) {
Logger.getLogger(DisplayArtistsForm.class.getName()).log(Level.SEVERE, null, ex);
}
// table creation
jTableData = new JTable(TableModel);
jTableData.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jTableData.getColumnModel().getColumn(0).setPreferredWidth(300);
// table row selection
jTableData.setCellSelectionEnabled(true);
// table row selection
jTableData.setCellSelectionEnabled(true);
ListSelectionModel cellSelectionModel = jTableData.getSelectionModel();
cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
int selectedRow = jTableData.getSelectedRow();
for (int i = 0; i < TableModel.getColumnCount(); i++) {
ArtistName = (String) TableModel.getValueAt(selectedRow, i);
}
}
});
jTableData.setPreferredScrollableViewportSize(new java.awt.Dimension(300, 200));
TableScrollPane = new JScrollPane(jTableData);
container = getContentPane();
container.setLayout(new FlowLayout(FlowLayout.CENTER));
// add everything to container
container.add(TableScrollPane);
container.add(jButtonBack);
container.add(jButtonDelete);
container.add(jButtonUpdate);
// draw form
setTitle("Artist(s)");
setLocation(new java.awt.Point(300, 200));
setSize(new java.awt.Dimension(400, 350));
}
/**
* @brief update action event
* @param evt
*/
private void jButtonUpdateActionPerformed(java.awt.event.ActionEvent evt) {
for (int i = 0; i < TableModel.getRowCount(); i++) {
db.UpdateArtist(TableModel.getOldValueAt(i, 0), (String) TableModel.getValueAt(i, 0));
}
db.Close();
JOptionPane.showMessageDialog(null, "Update was successful", "Info", JOptionPane.INFORMATION_MESSAGE);
}
/**
* @brief delete action event
* @param evt
*/
private void jButtonDeleteActionPerformer(java.awt.event.ActionEvent evt) {
if (ArtistName == null) {
JOptionPane.showMessageDialog(null, "Artist was not selected", "Error", JOptionPane.WARNING_MESSAGE);
} else {
if (db.DeleteArtist(ArtistName)) {
JOptionPane.showMessageDialog(null, "Artist was deleted", "Info", JOptionPane.INFORMATION_MESSAGE);
}
dispose();
}
}
/**
* @brief back action event
* @param evt
*/
private void jButtonBackActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
}