-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountTransactionLayout.java
More file actions
172 lines (141 loc) · 5.16 KB
/
AccountTransactionLayout.java
File metadata and controls
172 lines (141 loc) · 5.16 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
// This code gives the layout for the UI and
// demonstrates two ways of updating the data
// in a JTable.
// Another option to consider when using JTable is
// creating your own data model by overriding
// AbstractTableModel. You might use this option
// if data for table was coming from say a DB.
// One example: http://www.java2s.com/Code/Java/Swing-JFC/CreatingsimpleJTableusingAbstractTableModel.htm
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import com.mysql.jdbc.ResultSetMetaData;
public class AccountTransactionLayout extends JFrame {
private Manager manage;
private JTable table;
private DefaultTableModel dtm;
private String[] columnNames = {"Account ID",
"Account Name",
"Balance"};
private String[][] data;
public AccountTransactionLayout() {
try {
manage = new Manager();
refreshTable(manage.refreshTable());
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null, e1.toString(), "Database connection failed.", JOptionPane.ERROR_MESSAGE);
}
Container contentPane = getContentPane();
contentPane.setLayout(new GridBagLayout());
dtm = new DefaultTableModel(data,columnNames);
table = new JTable(dtm);
// The default size of a JTable is something like
// 450 X 400.
Dimension smallerSize = new Dimension(450, 50);
table.setPreferredScrollableViewportSize(smallerSize );
JScrollPane scrollPaneForTable = new JScrollPane(table);
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 2;
constraints.gridheight = 1;
constraints.weightx = 1;
constraints.weighty = 1;
constraints.insets = new Insets(4, 4, 4, 4);
constraints.fill = GridBagConstraints.BOTH;
contentPane.add(scrollPaneForTable,constraints);
constraints.gridx = 0;
// constraints.gridy = 1;
constraints.weighty = 0;
constraints.gridy = GridBagConstraints.RELATIVE;
constraints.insets = new Insets(2, 4, 2, 4);
constraints.fill = GridBagConstraints.NONE;
constraints.gridwidth = 1;
constraints.anchor = GridBagConstraints.NORTHEAST;
JLabel toLabel = new JLabel("From:");
contentPane.add(toLabel,constraints);
constraints.gridx = 1;
constraints.anchor = GridBagConstraints.NORTHWEST;
JTextField fromField = new JTextField("",8);
// Workaround, because of: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4247013
fromField.setMinimumSize(fromField.getPreferredSize());
contentPane.add(fromField,constraints);
constraints.gridx = 0;
// constraints.gridy = 2;
constraints.anchor = GridBagConstraints.NORTHEAST;
JLabel fromLabel = new JLabel("To:");
contentPane.add(fromLabel,constraints);
constraints.gridx = 1;
// constraints.gridy = 2;
constraints.anchor = GridBagConstraints.NORTHWEST;
JTextField toField = new JTextField("",8);
toField.setMinimumSize(toField.getPreferredSize());
contentPane.add(toField,constraints);
constraints.gridx = 0;
// constraints.gridy = 2;
constraints.anchor = GridBagConstraints.NORTHEAST;
JLabel amountLabel = new JLabel("Amount:");
contentPane.add(amountLabel,constraints);
constraints.gridx = 1;
// constraints.gridy = 2;
constraints.anchor = GridBagConstraints.NORTHWEST;
JTextField amountField = new JTextField("",8);
amountField.setMinimumSize(amountField.getPreferredSize());
contentPane.add(amountField,constraints);
constraints.gridx = 0;
// constraints.gridy = 2;
constraints.anchor = GridBagConstraints.NORTHEAST;
JButton clearButton = new JButton("Clear");
contentPane.add(clearButton,constraints);
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fromField.setText("");
toField.setText("");
amountField.setText("");
}
});
constraints.gridx = 1;
// constraints.gridy = 2;
constraints.anchor = GridBagConstraints.NORTHWEST;
JButton transferButton = new JButton("Transfer");
transferButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
manage.transfer(Integer.parseInt(fromField.getText()), Integer.parseInt(toField.getText()), Double.parseDouble(amountField.getText()));
//table.setModel(new DefaultTableModel(newData,columnNames));
refreshTable(manage.refreshTable());
reallyrefreshthetable();
}
// catch data-type errors
catch(NumberFormatException err){
JOptionPane.showMessageDialog(null, err.toString(), "Input type error.", JOptionPane.ERROR_MESSAGE);
}
}
});
contentPane.add(transferButton,constraints);
}
public void refreshTable(ArrayList tabledata){
data = new String[3][3];
int j = 0;
for (int i = 0; i < tabledata.size(); i++) {
data[j][i%3] = (String) tabledata.get(i);
if (i % 3 == 2)
j++;
}
}
public void reallyrefreshthetable(){
dtm.setDataVector(data, columnNames);
}
public static void main(String[] args) {
JFrame frame = new AccountTransactionLayout();
frame.pack();
frame.setVisible(true);
}
}